简体   繁体   中英

how to store javascript variable into php

I want store clickede_id value into $id2[] give me some suggestions and also suggest some advance details

function yes(clicked_id)
    {

        var it1=clicked_id;

        alert(it1);

        var tt1=1;


        var tt2= "<?php echo($id2[var it1]); ?>";


        //var tt2=document.getElementById("idcheck").value;
        alert(tt2);
        var tt3=document.getElementById("idcheck1").value;
        //alert(tt3);

    }

When you develop a web application, you are creating tools to let client and server communicates (over the HTTP protocol). This communication is based on Requests and Responses.

The client send request to server and the server responds with a reponse. In your case, you choosed PHP as the server-side language that will create your responses as answers to client request. Thes responses are HTML (+ javascript). Anyways, the reponses are static stuff to be interpredted by the client.

So the code you have sent is seen by the browser as:

function yes(clicked_id)
    {

        var it1=clicked_id;

        alert(it1);

        var tt1=1;


        var tt2= 3; // or whatever value returned by php

        var tt2=document.getElementById("idcheck").value;



        var tt3=document.getElementById("idcheck1").value;
        // ajax call here

    }

When you say : store data from javascript to php (even if it doesnt look as a correct senetence), you mean sending data from client to server. It can be done via a classical Post request (via form submit with page refresh) or via ajax without page refresh.

For ajax, please to check jQuery documentation for $.ajax function (if you want to have a cross browser compatible solution), or XMLHTTPRequest object if you want raw javascript and do the cross-browser compatibility yourself.

PHP executes in the server and send the result to your browser to display. Your JS executes at this browser stage. What you need to understand is that PHP has already been finished it's execution when your JS gets a chance to execute. Trying to change something in PHP through the JS is trying to access the past.

But, the good news is, that you can adopt a model where you feed your JS through the PHP script (look at this echo "<script>var s = 'from php'</script>" ) and JS feeds your NEXT php execution. You can use ajax or direct page calling for this.

Probably you should read this question: How to pass data from Javascript to PHP and vice versa?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM