Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    example code:
    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?
    Last edited by liorean; 06-10-2004 at 07:06 PM. Reason: [code]

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    I think you need to read our rules before creating anymore threads.

    No crossposting

    Crossposted at: http://www.codingforums.com/showthread.php?t=40069
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,961
    Thanks
    0
    Thanked 236 Times in 233 Posts
    You might be confused with the difference between server-side and client-side scripting. 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 ?

  • #4
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    In this example, it works. this is not about client/server side problem.

    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.

    Code:
    	<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>
    Last edited by liorean; 06-10-2004 at 07:06 PM. Reason: Please, use [code]!

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,961
    Thanks
    0
    Thanked 236 Times in 233 Posts
    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?

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

  • #6
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    here is the successful example link.

    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>

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,961
    Thanks
    0
    Thanked 236 Times in 233 Posts
    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. You will get an idea how to generate javascript array from the server from here, it is asp not php but you will get the idea.

  • #8
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •