Link to home
Start Free TrialLog in
Avatar of philcp
philcp

asked on

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>";

}
?>
Avatar of higijj
higijj

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 ;-/
Avatar of philcp

ASKER

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

Avatar of philcp

ASKER

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?

Avatar of philcp

ASKER

I didn't get any satisfactory response.
ASKER CERTIFIED SOLUTION
Avatar of higijj
higijj

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.