开发者

How do I assign javascript variable to php variable? With out submitting the form

hi friends can some one tell me How do I assign javascript variable to php variable? I am actually getting a value through javascript and I want the javascript values to be assigned to php variables.开发者_如何学运维 With out submitting the form.


Unless you are doing something really esoteric (such as running server side JS and PHP):

The PHP runs, generates a response, sends it to the browser. If that response includes JavaScript then that JS will then be executed.

You can't get data from the JS back to the PHP without issuing a new HTTP request.

This could be done by setting document.location, adding an <img> element to the document with the data passed via the src attribute (in both cases including the data in a query string, posting a <form> to an <iframe>, using an XMLHttpRequest object and a host of other methods.

It really depends on what you want to achieve.


You don't, directly.

You have to use AJAX, but beware that is far more complicated than "just assigning it".


Use the jQuery selector to do that. OR use a hidden form if you don't want the user to see it.

For example, do this.

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

After that, just use jQuery selector

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

That's about it, not much problems!


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


You can assign PHP variable to a JavaScript one:

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

But you can't do this backwards - it is a matter of that php is run BEFORE javascript. ;]


Best way will be to send the javascript variable value to the server asynchronously before the form is submitted using AJAX, but be aware that you will only be able to bring back a variable or store the data server side via a db or text file.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜