Passing Javascript variable to Php function?

Hi there,

I'm developing a webpage that uses Javascript for its user interface and php to communicate with the database.

How is it that one pass a variable from javascript function to php function.

Let's say
<form name="TheForm">
<select name="choice" onChange="Do_Stuff(this.form);">
<option value="1">Choice 1
<option value="2">Choice 2

<script language="javascript">

function Do_Stuff(form) {
selectedOption = this.form.choice.value
Switch (selectedOption) {
Case 1:
/*SQL= "select Field01 from table01 where Field01 Like 'Y%'";
pass over to Php to process and be displayed on the page*/
Case 2:
/*differs from Case 1's sql statement, but the idea is the same */



function Take_From_DB() {
@include('adodb/'); // load code common to ADODB
ADOLoadCode('access'); // load Access code
$con = &ADONewConnection(); // create a connection
$con->Connect('CD'); // connect to MS-Access using DSN name
$recordSet = &$con->Execute($SQL);

/*echo " 
<form name = \"CW\">
<select name=\"theBOX\" size=\"15\" multiple >";*/
echo "<TABLE><TR><TD><form name=\"CW\" method=\"GET\" Action=\"\">
<Select name=\"theBOX\" size=\"15\" multiple >";

while(!$recordSet->EOF) {
$num = $recordSet->fields[0].' '.@$recordSet->fields[1];
echo "<option value>$num";

echo "</Select></TD><TD Valign=\"center\"><table><tr>
<input type=\"button\" value=\">> \" onClick=\"Run(this.form);\"></tr>
<tr align=\"center\"><input type=\"button\" value=\" > \"></tr>
<tr align=\"center\">
<input type=\"button\" value=\" < \"></tr>
<input type=\"button\" value=\" <<\"></tr>

Who is Participating?

if you have a form, with a <SELECT> in it..

each <OPTION> containing a SQL statement..

and you have a SUBMIT button..

when the user hit submit, it send that information to the ACTION="yourphpscript.php" of the <FORM> tag, agree?

What you do want to do here, is get rid of that submit button .. and on the onChange event of that SELECT, do a submit.. so it automatically submit the variable to the php script..

so for exemple:
<FORM ACTION="test.php" METHOD="post">

then.. you would just do the javascript that when onchange happen, you call submit or whatever simulate a SUBMIT button.. ( I don't know exactly what it is in javascript )

So doing this, would send the selected value to the test.php script.

So in your test.php, you simply check if $SQL is set.. cuz if it's not set, it means that this is the first time the user load the page and he didn't make any selection yet.. BUT if it IS SET, it means the user just selected an option .. so do what ever you want to do with that $SQL variable..
What I would suggest is .. on the onchange event, call the onsubmit event that reload the page with the proper variable..

and that page that self-load would be the php page..

so.. at the beginning of the php page, you would have something similar to:

if (isset($SQL)) {

   // do what ever you want to do with that variable

} else {

   // simply print the form


other than that.. I don't think it is possible to directly do what you want to ;-/
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

philcpAuthor Commented:
i understand that one will have a choice of executing different sql statements when the page is reload and then using the if (isset($SQL)) statement to print the wanted data on the page.

But how is it that one pass the sql statement from javascript to the php function? i still can't picture that

philcpAuthor Commented:
Erm the reason for me passing the jscript var over to php is to have the page reloaded with new SQL statements based on the option selected

This is how it is

I have a select option box where one can select any option

<select name="thebox" onchange="GetSQL();">
<Option value=1>A
<Option value=2>B
<Option value=3>C

<script lang="jscript">

function GetSQL() {
Switch (Option.value)
Case 1:
var jSQL="Select field1 from table1 where field1 like 'A%'";
Case 2:
var jSQL="Select field1 from table1 where field1 like 'B%'";
Case 3:
var jSQL="Select field1 from table1 where field1 like 'C%'";

//do something to reload the page and then passing the jSQL to $phpSQL statement to Php


1st, is this a good way of doing it?
2nd, if this is the only way, how could it be done, passing the jSQL to $phpSQL???

I know that jscript is client side and Php is server side... so this is a very bad move....

could anyone suggest a better way? or if its ok to continue with my method above, what is the better way of doing it?

philcpAuthor Commented:
I didn't get any satisfactory response.
Hope you better understand it this way ;-/

so in your GetSQL(); function, you would have to call the submit event of the form.. and you're on your way ;-)

Hope this is a little clearer!

.oO Higijj Oo.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.