Skip to content Skip to sidebar Skip to footer

Js <---> Php Communication

I need to, using JS get some values returned by a PHP script. How to achieve that?

Solution 1:

<scripttype="text/javascript">var string = <?phpecho$variable; ?>;
</script>

or AJAX

Solution 2:

Solution 3:

You can create a js file with PHP.

For exemple

// My script php
var js_var = '<?phpecho"toto"?>';

or use an AJAX framework and JSON

Solution 4:

What's the method you're using to communicate with PHP? If you're using AJAX you might want to checkout jQuery or a library of some sort and use JSON for communication.

Using these methods, like using getJSON, you can easily access the variables returned by the PHP-script.

Please provide some more details to your question so we can answer your question in more detail and provide some examples.

Solution 5:

You can assign php values to JavaScript like so:

var x = <?phpecho$varible ; ?>;

Post a Comment for "Js <---> Php Communication"