Solved

Passing Javascript variable to Php function?

Posted on 2001-06-16
7
262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

635 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