0

I want to insert a php statement into a jQuery String to update the user profile dynamically.

Here is my not working code so far:

jQueryVarWP("#kreis1").append("<option value=\""+array_list[i]+"\"<?php selected("+array_list[i]+", get_the_author_meta( \"kreis\", \$user->ID ) );?> >"+array_list[i]+"</option>");

My problem is somewhere here:

"<?php selected("+array_list[i]+", get_the_author_meta( \"kreis\", \$user->ID ) );?>

Maybe someone can help me to build this string.

2
  • Hi. You should have asked something like this on StackOverflow. This is completely out of scope here. Mar 1, 2017 at 11:54
  • maybe for you, but this is still something i do in wordpress, so it is a wordpress issue ... in case you have nothing to say, just be quite =>
    – Minttea
    Mar 1, 2017 at 11:56

1 Answer 1

0

You might want to try to use wp_localize_script() to allow your javascript to use data that are normally only available using php. Here is the link in the codex that will help you do what you need:

https://codex.wordpress.org/Function_Reference/wp_localize_script

Another way is to use a php to output your entire javascript code (in a php file, that is, and not a js file). That way you can use php the way you would normally do.

12
  • "Another way is to use a php to output your entire javascript code (in a php file, that is, and not a js file). That way you can use php the way you would normally do." --> any example here for me?
    – Minttea
    Mar 1, 2017 at 12:59
  • something like this: <?php $foo = 'bar'; echo '<script type="text/javascript">document.getElementById("test").innerHTML = " ' . $foo . ' ";</script>'; ?>
    – ajie
    Mar 1, 2017 at 13:44
  • tried to echo the whole js without using the needed php, but now it says Parse error: syntax error, unexpected ''); ' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' in C:\xampp\htdocs\nuts\test.php on line 125
    – Minttea
    Mar 1, 2017 at 13:48
  • line 125 </script>';
    – Minttea
    Mar 1, 2017 at 13:49
  • Watch for characters that might need escaping.
    – ajie
    Mar 1, 2017 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.