How to assign javascript variable to php variable? No form submission

Hi friends, can someone tell me. How to assign javascript variable to php variable? I actually get the value through javascript, and I want the javascript values ​​to be bound to php variables. Without submitting a form.

+4
source share
6 answers

Use the jQuery selector for this. OR use a hidden form if you do not want the user to see it.

For example, do it.

<input type="hidden" name="YOUR_OWN_UNIQUE_NAME" id="UNIQUE_ID" value > 

After that, just use the jQuery selector

 $("#UNIQUE_ID").val("VALUE") 

What about it, not so many problems!

0
source

If you are not doing something truly esoteric (e.g. server side JS and PHP):

PHP works, generates a response, sends it to the browser. If this answer includes JavaScript, then this JS will be executed.

You cannot get data from JS back to PHP without requesting a new HTTP request.

This can be done by setting document.location , adding an <img> element to the document with the data passed through the src attribute (in both cases, including data in the query string, placing <form> to a <iframe> using the XMLHttpRequest object and many others methods.

It really depends on what you want to achieve.

+7
source

You are not doing this directly.

You must use AJAX , but be careful, it is much more complicated than just assigning it.

+2
source

You need to send an AJAX request containing the information to a separate PHP script that processes the data.

0
source

You can assign a PHP variable to JavaScript:

 var some = <?php echo $other; ?> 

But you cannot do it backward - it is a matter of what php runs before javascript .;]

0
source

The best way would be to send the javascript variable value to the server asynchronously before the form is submitted using AJAX, but keep in mind that you can return the variable or store the data server via db or text file.

what do you want to do with the data that is sent to the server?

0
source

All Articles