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
</select>
</form>

<script language="javascript">

function Do_Stuff(form) {
var SQL; //TO BE PASSED TO PHP
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 */
default:
break;
}
}

</script>

<?php

function Take_From_DB() {
@include('adodb/adodb.inc.php'); // 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
/*ACCEPTS VAR SQL; FROM JSCRIPT AND STORE INTO $SQL*/
$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";
$recordSet->MoveNext();
}

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>
<tr>
<input type=\"button\" value=\" <<\"></tr>
</table>
</TD>
</TR>
</TABLE>
</form>";

}
?>
philcpAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
higijjConnect With a Mentor Commented:
Well..

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">
<SELECT NAME="SQL">
<OPTION VALUE="SELECT * FROM tbl_1">Table #1</OPTION>
<OPTION VALUE="SELECT * FROM tbl_2">Table #2</OPTION>
<OPTION VALUE="SELECT * FROM tbl_3">Table #3</OPTION>
</SELECT>
<FORM>

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..
0
 
higijjCommented:
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

}


0
 
higijjCommented:
other than that.. I don't think it is possible to directly do what you want to ;-/
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
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

0
 
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?

0
 
philcpAuthor Commented:
I didn't get any satisfactory response.
0
 
higijjCommented:
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.
0
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.