...

View Full Version : Help: is it possible to pass a javascript variable into php contained in javascript?



seaalley
06-10-2004, 02:49 AM
example code:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<script language="JavaScript">

function chgStudent(form) {
var newIndex = form.term.selectedIndex;
var termID = form.term.options[newIndex].value;
//document.getElementById("stchange").innerHTML = "test asdf";
document.getElementById("stchange").innerHTML = "<?php echo 'termNum='" + termID + "; ?>"; //is it correct like this ?
}


</script>
<body>
<form name= "form02">
<select name=term onChange="chgStudent(this.form)">
<option value="1">james</option>
<option value="2">alvin</option>
<option value="3">krishna</option>
</select>
</form>

<table>
<tr>
<td id="stchange">
<select>
<option value="01">Select term first</option>
</select>
</td>
</tr>
</table>

</body>
</html>

I try to pass javascript variables to php using cookie. Strange is the cookie I write in javascript can't be read by php.
Any body help?

Spookster
06-10-2004, 03:40 AM
I think you need to read our rules before creating anymore threads.

No crossposting

Crossposted at: http://www.codingforums.com/showthread.php?t=40069

glenngv
06-10-2004, 03:48 AM
You might be confused with the difference between server-side and client-side scripting (http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=569). Server-side codes are processed in the server generating HTML and javascript on the client.

What do you really want to accomplish with this?

document.getElementById("stchange").innerHTML = "<?php echo 'termNum='" + termID + "; ?>"; //is it correct like this ?

seaalley
06-10-2004, 07:05 PM
In this example, I pick the course(only course 1 or course 2), than the term select will auto load the term time from database. In this way, I don't use url variable to call the server again, so it's faster. Now I want to get the students name from database if I pick a term(now the term is a variable).
Here is the code it works on select courses and terms.


<tr>
<td>Course:</td>
<td>

<select name="courseID" onChange="chgTerm(this.form)">
<option value="1">MMI</option>
<option value="2">IP</option>
</select>

<script language="JavaScript">
function chgTerm(form) {
var newIndex = form.courseID.selectedIndex;
if (form.courseID.options[newIndex].value ==2){
document.getElementById("term01").innerHTML = '<?php echo "<select name=term onChange=chgStudent(this.form) >"; $comboResult=mysql_query ("SELECT termID, term FROM term_a WHERE courseID =2 ORDER BY termID"); $combo_num_rows = mysql_num_rows ($comboResult); for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); list($x1, $x2) = split ("[,]", $row[1]); $x3 = $x1." 30".$x2; $xTime = strtotime($x3); if ($xTime > time()) {echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
} else if (form.courseID.options[newIndex].value ==1){
document.getElementById("term01").innerHTML = '<?php echo "<select name=term onChange=chgStudent(this.form) >"; $comboResult=mysql_query ("SELECT termID, term FROM term_a WHERE courseID =1 ORDER BY termID"); $combo_num_rows = mysql_num_rows ($comboResult); for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); list($x1, $x2) = split ("[,]", $row[1]); $x3 = $x1." 30".$x2; $xTime = strtotime($x3); if ($xTime > time()) { echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
}
}

function chgStudent(form) {
var newIndex = form.term.selectedIndex;
var termID = form.term.options[newIndex].value;
document.getElementById("stchange").innerHTML = '<?php echo "<select name=stuChosen>"; $stResult=mysql_query ("SELECT studentID, name FROM studentS_a WHERE termID = ' + termID + ' ORDER BY name"); $st_num_rows = mysql_num_rows ($stResult); for ($i=0; $i < $st_num_rows; $i++) {$row = mysql_fetch_row ($stResult); echo "<option value=$row[0]>$row[1]</option>";} echo "</select>"; ?>'; //here termID is a variable that I want to pass.

}

</script>

</td>
</tr>
<tr>
<td>Term:</td>

<?
include ("db_info.php");

$db_conn2 = mysql_connect($hostName, $userName, $password);
mysql_select_db ($database);
?>
<td id=term01>
<? echo "<select name=term onChange=chgStudent(this.form) >";

$comboQuery = "SELECT termID, term FROM term_a WHERE courseID =1 ORDER BY termID;";
$comboResult = mysql_query ($comboQuery);
$combo_num_rows = mysql_num_rows ($comboResult);

for ($i=0; $i < $combo_num_rows; $i++)
{
$row = mysql_fetch_row ($comboResult);
list($x1, $x2) = split ("[,]", $row[1]);
$x3 = $x1." 30".$x2;
$xTime = strtotime($x3);
if ($xTime > time()) {
echo "<option value=$row[0]>$row[1]</option>";
}
}

echo "</select>";
?>
</td>
</tr>
<tr>
<td>Student Chosen:</td>
<td id="stChange">
<select name="stuChosen">
<option>Please select Course and Term First</option>
</select>


</td>
</tr>

glenngv
06-11-2004, 03:04 AM
You are really confused with how the server-side codes are processed by the server and the client-side codes by the browser.

You can't execute a server-side code unless you send an HTTP request again to the server by way of submitting the form. All the server-side codes are processed in the server, so by the time the HTML and javascript codes are sent to the browser, all the server-side codes have been executed. To verify this, view the generated HTML source (View->Source from the browser menu) to see that server-side codes are gone and check the content of the chgStudent() function where you want to pass the ID of the selected option item to the SELECT query. Can you see the SELECT query? :rolleyes:

Please try to read the link I posted for you to be enlightened.

seaalley
06-11-2004, 11:04 PM
http://netpub.cstudies.ubc.ca/internship/test.php
when you choose course(courseID only 1 and 2), the term will change too(using php pull the data from database).
the real code is following. of course, in client side if you view source code in IE, php code is already translated to html. I want to know if it is possible to pass variables to php before it is translated to html. Because I want to select students according to the termID(this is the javascript variable which I want to pass it to php).

<?
include("db_info.php");
$db_conn = mysql_connect ($hostName, $userName, $password);
mysql_select_db ($database);
if (! $db_conn)
{ echo "Database Connection Error. Please Try Again Later."; }
?>

<html>
<head>
<title>ADD INTERNSHIP</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<table align="center" class="new_tbl">
<form name="form02" action="addInternship.php" method="post">
<tr>
<td>Course:</td>
<td>
<select name="courseID" onChange="chgTerm(this.form)">
<option value="1">MMI</option>
<option value="2">IP</option>
</select>

<script language="JavaScript">
function chgTerm(form) {
var newIndex = form.courseID.selectedIndex;
if (form.courseID.options[newIndex].value ==2){
document.getElementById("term01").innerHTML = '<?php echo "<select name=term onChange=chgStudent(this.form) >"; $comboResult=mysql_query ("SELECT termID, term FROM term_a WHERE courseID =2 ORDER BY termID"); $combo_num_rows = mysql_num_rows ($comboResult); for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";} echo "</select>"; ?>';
} else if (form.courseID.options[newIndex].value ==1){
document.getElementById("term01").innerHTML = '<?php echo "<select name=term onChange=chgStudent(this.form) >"; $comboResult=mysql_query ("SELECT termID, term FROM term_a WHERE courseID =1 ORDER BY termID"); $combo_num_rows = mysql_num_rows ($comboResult); for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";} echo "</select>"; ?>';
}
}
</script>
</td>
</tr>
<tr>
<td>Term:</td>

<?
include ("db_info.php");

$db_conn2 = mysql_connect($hostName, $userName, $password);
mysql_select_db ($database);
?>
<td id=term01>
<? echo "<select name=term>";

$comboQuery = "SELECT termID, term FROM term_a WHERE courseID =1 ORDER BY termID;";
$comboResult = mysql_query ($comboQuery);
$combo_num_rows = mysql_num_rows ($comboResult);

for ($i=0; $i < $combo_num_rows; $i++)
{
$row = mysql_fetch_row ($comboResult);
echo "<option value=$row[0]>$row[1]</option>";
}

echo "</select>";
?>
</td>
<?
//mysql_close ($db_conn2);
?>
</tr>
<tr>
<td>Student Chosen:</td>
<td id="stChange"><input type="text" name="stuChosen">
</td>
</tr>
<table align="center" class="button_tbl">
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Submit">
<input type="reset" value="Reset"></td>
</tr>
</table>
</form>
</table>

</body>
</html>

glenngv
06-14-2004, 03:21 AM
As I said, you need to submit the page to the server in order for the client-side data to be passed to the server.

<form name="form02" action="addInternship.php" method="post">
...
<select name="term" onChange="this.form.action='test.php';this.form.submit()">
...
<input type="submit" name="submit" value="Submit" onclick="this.form.action='addInternship.php'">
</form>

Then in test.php, retrieve the selected term and select the corresponding records from the database according to the selected term and then display the html again.

If you don't want the page to refresh as a term is selected, you can select all the data from the database and then store them in a javascript array. Then when the selected item changed, javascript will pull the corresponding data from the array. What you need is a triple-combo box script (http://www.javascriptkit.com/script/script2/triplecombo.shtml). You will get an idea how to generate javascript array from the server from here (http://www.atgconsulting.com/triplelist.asp), it is asp not php but you will get the idea.

seaalley
06-14-2004, 08:55 PM
Thanks. I solved this problem using a stupid way, make a loop and list all termID instead of variable, then use switch selection to choose.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum