(Very urgent)how to assign the value of the variable in javascript function into php variable

ajax , db , java , javascript , php , xml Cambodia
  • 13 years ago

    Dear all

    Could anyone can tell me or give some example for me to get the value from javascrip function and assing it into php variable.

    Thank in advance

  • 13 years ago
    <SCRIPT language='javascript' type="text/javascript">
    function check(count){
        val=document.getElementById("name").value=count;
        //alert(val);
        }
    </script>
    <body onload="return check('<?= $row['firstName']?>');">
    <input type="name" id="name" >

    this will print the value of name in text box












  • 13 years ago
    You can't really take a value in Javascript and directly assign it to a PHP variable.  This stems from the fact that PHP code executes on the server while JavaScript executes in the client's web browser.  PHP can easily pass information to JavaScript.  All you need for that is something like this:


    <script language="javascript">
    var jsvar = <?PHP echo $phpvar; ?>;
    </script>






    This would pass the contents of PHP variable $phpvar to javascript variable jsvar.  Going the other way is more complex - the Javascript code executes independantly of the PHP code.  You can use an XmlHTTPRequest object to execute another php script on the server and pass that script your information.  I use this JavaScript include file - it helps make your code cross-browser compatible and it is very easy to use. 

    http://www.scss.com.au/family/andrew/webdesign/xmlhttprequest/

    Here is some javascript code that passes jsvar to a php page:


    var xml = new XMLHttpRequest();

    if (xml)
    {
    xml.onreadystatechange = function() // need a callback function to process the reply
    {
    if (xml.readyState == 4)
    {
    if (xml.status != 200) // anything other than a 200 is very very bad...
    {
    // error
    alert("Error: AJAX Query Failed");
    }
    else
    {
    //success
    var str = xml.responseText; // get the data
    jsvar2 = str;

    // do something with the PHP script output

    }
    }
    };
    xml.open('GET', "callback.php?v=" + jsvar); // we're not posting anything, so we use get
    xml.send(null); // and again, we're not posting anything, but you have to send something regardless (well, in this case we're actually sending nothing, but you know what I mean...)
    }
    else
    {
    alert("Error: failed to create XMLHttpRequest object");
    }


































    This is callback.php:


    if (isset($_GET['v']))
    {
    $phpvar = $_GET['v'];

    // do stuff

    echo $phpvar2;
    }












    This code sends jsvar to the server (look at xml.open in the JS section) and loads it into phpvar, then sends phpvar back and loads it into jsvar2.  This process is actually called Ajax (don't ask me what it stands for...).  It can be used to dynamically query a database and transfer information without reloading the whole page. 


















  • 11 years ago
    One other possible solution could be store value in some hidden field on page and access and initialize the php variable upon post back.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“C++: an octopus made by nailing extra legs onto a dog.” - Steve Taylor