PDA

View Full Version : how can i pass vars from javascript to php?


questor
05-19-2004, 08:02 AM
I'm writing a page that uses javascript and php. I need to pass data between them without changing pages. How can I do that?

Specifically, I need to pass the 'choice' var into the php script so that I can write the sql query WHERE id = X, and X will equal the choice variable.

Here's my page:

<html>
<head>
<title>COG</title>
<link rel="stylesheet" type="text/css" href="main.css">

<script>
function show(i)
{
var k = document.getElementById(i);
k.style.visibility = "visible";
}

function hide(i)
{
var k = document.getElementById(i);
k.style.visibility = "hidden";
}

function secondMenu(choice)
{
if (choice=="ops")
{
var choiceName = "Operations"
var notChoice1 = "net"
var notChoice1Name = "Network"
var notChoice2 = "ecomm"
var notChoice2Name = "Ecommerce"
var notChoice3 = "brc"
var notChoice3Name = "BRC"
}
if (choice=="net")
{
var choiceName = "Network"
var notChoice1 = "ops"
var notChoice1Name = "Operations"
var notChoice2 = "ecomm"
var notChoice2Name = "Ecommerce"
var notChoice3 = "brc"
var notChoice3Name = "BRC"
}
if (choice=="ecom")
{
var choiceName = "Ecommerce"
var notChoice1 = "net"
var notChoice1Name = "Network"
var notChoice2 = "ops"
var notChoice2Name = "Operations"
var notChoice3 = "brc"
var notChoice3Name = "BRC"
}
if (choice=="brc")
{
var choiceName = "BRC"
var notChoice1 = "net"
var notChoice1Name = "Network"
var notChoice2 = "ecomm"
var notChoice2Name = "Ecommerce"
var notChoice3 = "ops"
var notChoice3Name = "Operations"
}
document.forms['new_ticket'].dept.options[0] = new Option(choiceName,choice);
document.forms['new_ticket'].dept.options[1] = new Option(notChoice1Name,notChoice1);
document.forms['new_ticket'].dept.options[2] = new Option(notChoice2Name,notChoice2);
document.forms['new_ticket'].dept.options[3] = new Option(notChoice3Name,notChoice3);
show('secondary_menu');
}

</script>

</head>
<body>

<div id="main_menu">
<div><h2>Menu One</h2></div>
<div style="cursor:hand" onmouseover="this.style.backgroundColor='#330000';" onmouseout="this.style.backgroundColor=''" onclick="secondMenu('ops');">Operations</div>
<div style="cursor:hand" onmouseover="this.style.backgroundColor='#330000';" onmouseout="this.style.backgroundColor=''" onclick="secondMenu('net');">Network</div>
<div style="cursor:hand" onmouseover="this.style.backgroundColor='#330000';" onmouseout="this.style.backgroundColor=''" onclick="secondMenu('ecom');">Ecommerce</div>
<div style="cursor:hand" onmouseover="this.style.backgroundColor='#330000';" onmouseout="this.style.backgroundColor=''" onclick="secondMenu('brc');">BRC</div>
</div>

<div id="secondary_menu">
<div><h2>Menu Two</h2></div>
<div style="cursor:hand" onmouseover="this.style.backgroundColor='#330000';" onmouseout="this.style.backgroundColor=''" onclick="show('add_ticket');hide('edit_ticket');hide('edit_fyi');hide('view_report');">Add Ticket</div>
<div style="cursor:hand" onmouseover="this.style.backgroundColor='#330000';" onmouseout="this.style.backgroundColor=''" onclick="hide('add_ticket');show('edit_ticket');hide('edit_fyi');hide('view_report');">Edit Ticket</div>
<div style="cursor:hand" onmouseover="this.style.backgroundColor='#330000';" onmouseout="this.style.backgroundColor=''" onclick="hide('add_ticket');hide('edit_ticket');show('edit_fyi');hide('view_report');">Edit FYI</div>
<div style="cursor:hand" onmouseover="this.style.backgroundColor='#330000';" onmouseout="this.style.backgroundColor=''" onclick="hide('add_ticket');hide('edit_ticket');hide('edit_fyi');show('view_report');">View Report</div>
</div>

<div id="add_ticket">
<div><h2>Add A Ticket</h2></div>
<form name="new_ticket" action="add_ticket.php" method="post">
<div>Ticket Number:<br><input type="text" name="ticket_number" size=7></div>
<div>Severity:<br><select name="sev" size=1>
<option value="5">5</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="2">2</option>
<option value="1">1</option>
<option value="C">Critical</option>
</select></div>
<div>Description:<br><textarea name="task" rows=3 cols=50></textarea></div>
<div>Deptartment:<br><select name="dept" size=1>

</select></div>
<div><br><input type="submit" value="Add Ticket"></div>
</form>
</div>

<div id="edit_ticket">
</div>

<div id="edit_fyi">
<div><h2>Edit the FYI</h2></div>
<form name="edit_fyi" action="edit_fyi.php" method="post">
<textarea name="fyi" rows=3 cols=50>
<?
$connect = mysql_connect("localhost","webuser","ecomm");
mysql_select_db("cog");
$result = mysql_query("SELECT text FROM fyi WHERE id=1");
$text = mysql_result($result,0);
echo $text;
mysql_close();
?>
</textarea>
<div><br><input type="submit" value="Update FYI"></div>
</form>
</div>

<div id="view_report">
</div>

</body>
</html>

Kor
05-19-2004, 09:07 AM
as far as I know thare is no biunivoque relationship between client-side and server-side languages.

Thus to pass values
1. from client-side to server-side - only submit action (send values to server)
2. from server-side to client side - only by creating a new HTML+script (or only an external script) file.

sad69
05-19-2004, 05:39 PM
So what's the choice variable? Like "ops" or something? So do you want id to be a number or a word like "ops"?

Secondly, I don't think you understand your code or how/when PHP works. Open up this page in your browser. You'll find that your textarea has the text for the id=1. This is because you've already run the query on the server side, and it's now on the client-side (where you no longer have access to the database or PHP). If you don't believe me, check out the view source and you'll see that there's no PHP code there. Therefore there's no way to spark an event to run that PHP MySQL query to update your textarea.

What you need is to somehow make your choice variable get into a form element, probably a hidden element. Then the submit button should be pressed to submit the form data to the PHP file that will reload the textarea. I would think that you'd have two iFrames perhaps, one encapsulating the first form, and one encapsulating the second form.

If that's not enough of a hint, let me know.

Good luck,
Sadiq.

seaalley
06-10-2004, 02:06 AM
try use document.getElementById("some_ID").innerHTML = "HTML code"; or "<?php PHP code and echo 'HTML code' ... ?>";

but I still don't know how to pass javascript variabless into php which is contained in javascript.

seaalley
06-14-2004, 09:03 PM
http://netpub.cstudies.ubc.ca/internship/test.php
code:
<?
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 width="400" align="center" class="new_tbl">
<form name="form02" action="addInternship.php" method="post">
<tr>

<td width="120">Course:</td>

<td width="280">
<select name="courseID" onChange="chgTerm(this.form); chgStudent(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>"; ?>';
}
}

function chgStudent(form){
var newIndex = form.term.selectedIndex;
var termID = form.term.options[newIndex].value;
/*if (termID ==5){
document.getElementById("stChange").innerHTML = '555';
}*/
switch (parseInt(termID)) {
case 0:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 0 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
case 1:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 1 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
case 2:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 2 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
case 3:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 3 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
case 4:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 4 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
case 5:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 5 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
case 6:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 6 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
case 7:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 7 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
case 8:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 8 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
case 9:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 9 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
case 10:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 10 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
case 11:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 11 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
case 12:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 12 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
case 13:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 13 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
case 14:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 14 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
case 15:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 15 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
case 16:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 16 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
case 17:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 17 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
case 18:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 18 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
case 19:
document.getElementById("stChange").innerHTML = '<?php echo "<select name=stuChosen>"; $comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = 19 ORDER BY name"); $combo_num_rows = mysql_num_rows ($comboResult); if ($combo_num_rows<=0) {echo "<option value=0>No students in this term</option>";} else { for ($i=0; $i < $combo_num_rows; $i++) {$row = mysql_fetch_row ($comboResult); echo "<option value=$row[0]>$row[1]</option>";}} echo "</select>"; ?>';
break;
default:
if (termID >= 20)
document.getElementById("stChange").innerHTML = "This web site is too old, please ask a programmer to fix here";
else
document.getElementById("stChange").innerHTML = "error";


}
}


</script>
</td>
</tr>
<tr>

<td width="120">Term:</td>

<?
include ("db_info.php");

$db_conn2 = mysql_connect($hostName, $userName, $password);
mysql_select_db ($database);
?>

<td width="280" 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);

global $global_x;
$global_x = 9999;

for ($i=0; $i < $combo_num_rows; $i++)
{

$row = mysql_fetch_row ($comboResult);
echo "<option value=$row[0]>$row[1]</option>";
if ($global_x >= $row[0]) {
$global_x = $row[0];
}
}

echo "</select>";
?> </td>
<?
//mysql_close ($db_conn2);
?>
</tr>
<tr>
<td width="120">Student Chosen:</td>
<td width="280" id=stChange>
<?php echo "<select name=stuChosen>";
$comboResult=mysql_query ("SELECT studentID, name FROM students_a WHERE termID = $global_x ORDER BY name");
$combo_num_rows = mysql_num_rows ($comboResult);
if ($combo_num_rows<=0)
{
echo "<option>Please Select Term first:</option><option value=0>No students in this term</option>";
}
else
{
for ($i=0; $i < $combo_num_rows;
$i++) {$row = mysql_fetch_row ($comboResult);
echo "<option value=$row[0]>$row[1]</option>";
}
} echo "</select>";
mysql_close ($db_conn2);
?>
</td>
</tr>

</form>
</table>

</body>
</html>