0

i need to append some php codes in a < script > in a php file. my codes works Properly, but i think that's mistake! i hear somethings about ajax and etc... its my codes mistake? if yes what is should to do? thank you so much

<script>
jQuery(document).ready( function() {
var button = jQuery('.button');
var sample = jQuery('.sample');
var geturi = '<?php echo get_template_directory_uri(); ?>';
var category = '< ?php foreach ( $terms as $term ){ echo '<option value="'. $term->slug .'">'. $term->name .'</option>'; } ?>';
button.live( 'click', function() {
sample.append('<li><div>'+ geturi + '/img.png' +'</div><select>'+ category +'</select><?php _e( 'sample', 'sample' ); ?></li> and more...');
});
</script>
ShahePars
  • 135
  • 3
  • 16
  • There's nothing inherently wrong with your code. It's a little unsightly - but that's mostly due to the spaghetti code caused by PHP in general. You could use AJAX to retrieve your options, but the point seems a little redundant if they are static for all users. – Rory McCrossan Nov 22 '16 at 16:29
  • 1
    Also `live()` was deprecated a *long* time ago. I'd suggest you update the version of jQuery you're using an change it to `on()` instead – Rory McCrossan Nov 22 '16 at 16:30
  • noting wrong but I use to load a separate js file included the necessary values like template address and ... it avoid spaghetti code. at least its only one or two file in the project. – Saeed.Gh Nov 22 '16 at 18:37
  • @RoryMcCrossan how to use ajax in my way? can you explain more, thank you, i read some articles about jquery ajax, and i found some functions like $.ajax(), $.get(), $.post(), $.load() , can you please explain me , what should to do – ShahePars Nov 24 '16 at 14:35
  • @S.Gholizadeh You mean? $.load() load a .js file or .php file? and if i use load() i should use append() too? or just load()? button.live( 'click' , function() { sample.load('sample file') }) ? – ShahePars Nov 24 '16 at 14:40
  • no I mean to avoid spaghetti code I have a script file like resources.js which includes template address and other useful variable which should pass from php, it can be use ajax to get an array from php and above other js files which uses it. – Saeed.Gh Nov 24 '16 at 14:46

0 Answers0