kvnirvana Posted May 5, 2010 Share Posted May 5, 2010 <html> <head> <script type="text/javascript"> function showUser(str) { if (str=="") { document.getElementById("kom").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("kom").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","getusernu.php?q="+str,true); xmlhttp.send(""); } </script> <script type="text/javascript"> <?php echo "var id = '".$id."';"; ?> </script> <?php if (isset($_POST['submit'])) { search(); //call the search function }else{ show_form(); //call the show form function }//end if /*------------------------------------------------------------------------ show the search form ------------------------------------------------------------------------*/ function show_form() { //call the dropdown function which creates an html string to build a select box for each element $pr = dropdown('pr','beha'); $navn = dropdown('navn','beha'); $om = dropdown('om','beha'); $id=$_GET["id"]; echo "<form> Bedøm {$_GET['be']}, {$_GET['navn']} <p> <tr> <td>Vælg kategori:</td><td>$pr </td> </tr> </select> </form> <br /> <div id='kom'>ffff</div>"; } function dropdown($field, $table) { //initialize variables $oHTML = ''; $result = ''; //check to see if the field is passed correctly if (($field == "")||($table == "")) { die("No column or table specified to create drop down from!"); } $sql = "select distinct($field) from $table"; //call the db function and run the query $result = conn($sql); //if no results are found to create a drop down return a textbox if ((!$result) ||(mysql_num_rows($result)==0)) { $oHTML .= "<input type='text' name='$field' value='' size='15'>"; }elseif (($result)&&(mysql_num_rows($result)>0)){ //build the select box out of the results $oHTML .= "<select name='$field' onchange='showUser()'>\n<option value='all'>All</option>\n"; while ($rows = mysql_fetch_array($result)) { $oHTML .= "<option value='".$rows[$field]."'>".$rows[$field]."</option>\n"; } $oHTML .= "</select>\n"; } //send the value back to the calling code return $oHTML; }//end function This page retrieves an id from the previous page based upon a mysql query, but I can’t seem to send the id to the next page, how do I do that. Please help me Quote Link to comment Share on other sites More sharing options...
F1Fan Posted May 6, 2010 Share Posted May 6, 2010 First, PLEASE post your code in code tags. Just put the ID in a hidden input field. Quote Link to comment Share on other sites More sharing options...
kvnirvana Posted May 6, 2010 Author Share Posted May 6, 2010 <html> <head> <script type="text/javascript"> function showUser(str) { if (str=="") { document.getElementById("kom").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("kom").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","getusernu.php?q="+str,true); xmlhttp.send(""); } </script> <script type="text/javascript"> <?php echo "var id = '".$id."';"; ?> </script> <?php if (isset($_POST['submit'])) { search(); //call the search function }else{ show_form(); //call the show form function }//end if /*------------------------------------------------------------------------ show the search form ------------------------------------------------------------------------*/ function show_form() { //call the dropdown function which creates an html string to build a select box for each element $pr = dropdown('pr','beha'); $navn = dropdown('navn','beha'); $om = dropdown('om','beha'); $id=$_GET["id"]; echo "<form> Bedøm {$_GET['be']}, {$_GET['navn']} <p> <tr> <td>Vælg kategori:</td><td>$pr </td> </tr> </select> </form> <br /> <div id='kom'>ffff</div>"; } function dropdown($field, $table) { //initialize variables $oHTML = ''; $result = ''; //check to see if the field is passed correctly if (($field == "")||($table == "")) { die("No column or table specified to create drop down from!"); } $sql = "select distinct($field) from $table"; //call the db function and run the query $result = conn($sql); //if no results are found to create a drop down return a textbox if ((!$result) ||(mysql_num_rows($result)==0)) { $oHTML .= "<input type='text' name='$field' value='' size='15'>"; }elseif (($result)&&(mysql_num_rows($result)>0)){ //build the select box out of the results $oHTML .= "<select name='$field' onchange='showUser()'>\n<option value='all'>All</option>\n"; while ($rows = mysql_fetch_array($result)) { $oHTML .= "<option value='".$rows[$field]."'>".$rows[$field]."</option>\n"; } $oHTML .= "</select>\n"; } //send the value back to the calling code return $oHTML; }//end function I tried to place a hidden field but still I can't get it to pass the id value from mysql Where should I place a hidden input field, and don't i need to write something in ajax to get the hidden field? I don't know if it matters but here is the page which opens when you select somthing from the drop down <?php echo "<td> Bedøm {$_GET['beha']}, {$_GET['navn']} </td>"; $con = mysql_connect('localhost', '***', '****'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("***", $con); $sql="select * from behandlere WHERE id='".$q."'"; $result = mysql_query($sql); echo "<table border='1'> <tr> <th>navn</th> <th>om</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['navn'] . "</td>"; echo "<td>" . $row['om'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> Quote Link to comment Share on other sites More sharing options...
F1Fan Posted May 6, 2010 Share Posted May 6, 2010 OK, I don't see how some of your code is working, but I THINK what you're asking is for this: $oHTML .= "<select name='$field' onchange='showUser(this.value)'>\n<option value='all'>All</option>\n"; Quote Link to comment Share on other sites More sharing options...
kvnirvana Posted May 6, 2010 Author Share Posted May 6, 2010 I still can get it to pass values. when I post echo $_GET['beha']; on the page which opens when onchange on the other page it says "Undefined index: beha" Quote Link to comment Share on other sites More sharing options...
F1Fan Posted May 6, 2010 Share Posted May 6, 2010 'beha' is your table name, not your input name. Also, if you're referring to what you use to get it in the PHP part of Ajax, then you use $_GET['q'] because of this line: xmlhttp.open("GET","getusernu.php?q="+str,true); Quote Link to comment Share on other sites More sharing options...
kvnirvana Posted May 7, 2010 Author Share Posted May 7, 2010 ok, so how do I get it to pass the value navn so when I type echo $_GET['navn']; on the page that opens on onchange it displays 'navn'? Should I pass it through xmlhttp.open("GET","getusernu.php?q="+str,true); so it would be somthing like this xmlhttp.open("GET","getusernu.php?q="+str,true&navn= $_GET['navn']); or what should I do, I really can't figure it out. Quote Link to comment Share on other sites More sharing options...
F1Fan Posted May 7, 2010 Share Posted May 7, 2010 OK, you're getting there. You can pass your variables via get, and you are already passing one variable. In this line: xmlhttp.open("GET","getusernu.php?q="+str,true); you are sending the JavaScript "str" variable in the first page to the "getusernu.php" file. That file receives the "str" variable as the PHP variable "$_GET['q']". To add other variables, you need to add "&variableToReceiveAs=variableToSend" to the end of the URL string, one for each variable you want sent. So, to add a PHP variable, you would do it this way: xmlhttp.open("GET","getusernu.php?q="+str+"&variableToReceiveAs=<?php echo $variableToSend; ?>",true); What you typed with the $_GET['navn'] doesn't make sense, because that's a variable that you would receive, not send. So, if you are wanting to send the selected variable from a <select> to the "getusernu.php", then you need to pass the variable into the JavaScript function, and finally pass it to the "getusernu.php" file. So, let's cover those three steps. Part 1, get the selected variable into the JavaScript function. Start by adding "this.value" to the arguments passed into the function, like this: <select name='selectName' onchange='showUser(this.value);'> Then accept that into the JavaScript function, like this: function showUser(selectedValue){ Part 2, send the variable to the PHP file: xmlhttp.open("GET","getusernu.php?sentValue="+selectedValue,true); Part 3, accept the sent variable into the PHP file: echo $_GET['sentValue']; Finally, here's an example of how to pass two variables: Part 1: echo "<select name='selectName' onchange='showUser(this.value, \"$somePHPvar\");'>"; function showUser(selectedValue, varFromPHP){ Part 2: xmlhttp.open("GET","getusernu.php?sentValue="+selectedValue+"&secondValue="+varFromPHP,true); Part 3: echo $_GET['sentValue'].'<br>'; echo $_GET['secondValue']; Quote Link to comment Share on other sites More sharing options...
kvnirvana Posted May 7, 2010 Author Share Posted May 7, 2010 Thank you so much for trying to explain me how it works :0) Unfortunally I still can't get it to work when I try to pass the value like you said xmlhttp.open("GET","getusernu.php?q="+str+"&variableToReceiveAs=<?php echo $navn; ?>",true); The page which opens doesn't show anything even though it should. I use $_GET['navn'] on the first page because it is a result of another page. Anyway here is my code for the page with ajax (I changed it a bit) <html> <head> <script type="text/javascript"> function showUser(str) { if (str=="") { document.getElementById("kom").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("kom").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","getusernu.php?q="+str,true); xmlhttp.send(""); } </script> <?php if (isset($_POST['submit'])) { search(); //call the search function }else{ show_form(); //call the show form function }//end if /*------------------------------------------------------------------------ show the search form ------------------------------------------------------------------------*/ function show_form() { //call the dropdown function which creates an html string to build a select box for each element $pr = dropdown('pr','beha'); $navn = dropdown('navn','beha'); $om = dropdown('om,'beha'); $id=$_GET["id"]; echo "<form> Bedøm {$_GET['behan']}, {$_GET['navn']} <p> <tr> <td>Vælg kategori:</td><td>$pr</td> </tr> </select> </form> <br /> <div id='kom'>ffff</div>"; } function dropdown($field, $table) { //initialize variables $oHTML = ''; $result = ''; //check to see if the field is passed correctly if (($field == "")||($table == "")) { die("No column or table specified to create drop down from!"); } $sql = "select distinct($field) from $table"; //call the db function and run the query $result = conn($sql); //if no results are found to create a drop down return a textbox if ((!$result) ||(mysql_num_rows($result)==0)) { $oHTML .= "<input type='text' name='$field' value='' size='15'>"; }elseif (($result)&&(mysql_num_rows($result)>0)){ //build the select box out of the results $oHTML .= "<select name='$field' onchange='showUser(this.value)'>\n<option value='all'>All</option>\n"; while ($rows = mysql_fetch_array($result)) { $oHTML .= "<option value='".$rows[$field]."'>".$rows[$field]."</option>\n"; } $oHTML .= "</select>\n"; } //send the value back to the calling code return $oHTML; }//end function /*------------------------------------------------------------------------ database connection function ------------------------------------------------------------------------*/ function conn($sql) { $username = "****"; $pwd = "*****"; $host = "*****"; $dbname = "*****"; //echo "commnecing connection to local db<br>"; if (!($conn=mysql_connect($host, $username, $pwd))) { printf("error connecting to DB by user = $username and pwd=$pwd"); exit; } $db3=mysql_select_db($dbname,$conn) or die("Unable to connect to local database"); $result = mysql_query($sql) or die ("Can't connect because ". mysql_error()); return $result; }//end function ?> This is the page which should display the value <?php // Make a MySQL Connection mysql_connect("localhost", "****", "****") or die(mysql_error()); mysql_select_db("*****") or die(mysql_error()); echo $_GET['navn']; echo $_GET['q']; ?> Quote Link to comment Share on other sites More sharing options...
F1Fan Posted May 7, 2010 Share Posted May 7, 2010 xmlhttp.open("GET","getusernu.php?q="+str+"&variableToReceiveAs=<?php echo $navn; ?>",true); Try it this way, if you already have $_GET['navn']. xmlhttp.open("GET","getusernu.php?q="+str+"&variableToReceiveAs=<?php echo $_GET['navn']; ?>",true); Quote Link to comment Share on other sites More sharing options...
kvnirvana Posted May 7, 2010 Author Share Posted May 7, 2010 ok now the page opens, but still when I type echo $_GET['navn']; on the page that opens it says Undefined index: navn Quote Link to comment Share on other sites More sharing options...
F1Fan Posted May 8, 2010 Share Posted May 8, 2010 Seriously? What is the PHP url that you are pointing to now? If it is this: xmlhttp.open("GET","getusernu.php?q="+str+"&variableToReceiveAs=<?php echo $navn; ?>",true); you don't see the problem?? $_GET['q'] and $_GET['variableToReceiveAs'] are the only ones you're passing. Quote Link to comment Share on other sites More sharing options...
kvnirvana Posted May 9, 2010 Author Share Posted May 9, 2010 It works :=) Thank you so much, i'm new to this coding stuff, so i'm still learning :=) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.