faqts : Computers : Programming : Languages : PHP : Not Quite PHP : Javascript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

86 of 417 people (21%) answered Yes
Recently 2 of 10 people (20%) answered Yes

Entry

What's the easiest way to pass variables between PHP and Javascript?

Jan 31st, 2000 17:18
Nathan Wallace, Richard Lynch


The easiest way i've found to pass hidden variables is to use 

<INPUT TYPE="hidden" NAME="foo" VALUE="default"> 

in a form and then use the javascript to modify the form variable.

Then the data generated by the javascript will be passed to the server.

You could always change the submit button to a image and provide that as
thelink off the page, and the user would never need to know.

Here is an example of how to create a Javascript array from PHP:

    $numrows = mysql_numrows($result);
    echo "<SCRIPT LANGUAGE=JavaScript>\n";
    echo "var states[$numrows];\n";
    while (list($id, $state) = mysql_fetch_row($result)){
      echo "states[$id] = '$state';\n";
    }
    echo "</SCRIPT LANGUAGE=JavaScript>\n";