Solved

Passing Javascript variable to Php function?

Posted on 2001-06-16
7
254 Views
Last Modified: 2012-06-27
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>";

}
?>
0
Comment
Question by:philcp
  • 4
  • 3
7 Comments
 
LVL 2

Expert Comment

by:higijj
ID: 6199277
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
 
LVL 2

Expert Comment

by:higijj
ID: 6199278
other than that.. I don't think it is possible to directly do what you want to ;-/
0
 

Author Comment

by:philcp
ID: 6199301
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:philcp
ID: 6199414
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
 

Author Comment

by:philcp
ID: 6199700
I didn't get any satisfactory response.
0
 
LVL 2

Accepted Solution

by:
higijj earned 70 total points
ID: 6199927
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
 
LVL 2

Expert Comment

by:higijj
ID: 6199931
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now