Jump to content

Using Element Attributes In Sql Statment.


mckenzie
 Share

Recommended Posts

Situation:I have a Form containing several dropdown boxes. One of the drop-down boxes is populated via mysql db and has an ONCHANGE JavaScript function within it. The ONCHANGE Jscript func creates another dropdown box in place of a <div> (see the brilliant php+mysql+Ajax tutorials). It is also populated with records depending on what the user chose in the first drop down.My problem:I want the second drop down box to populate depending on the user choice in the first drop down box and the NAME of the first drop down box.i.e. SELECT name FROM players where team = [1st drop down choice] AND (SELECT position FROM players) = [1st drop down box ‘NAME’]How would I go about making the <select> attribute into a variable? Any tutorials on how to do this?Mainbody

<?php$con = mysql_connect("127.0.0.1","root","");if (!$con)  {  die('Could not connect: ' . mysql_error());  }	   mysql_select_db("webproject", $con);  $resultgk = mysql_query("SELECT name FROM teams");  //Goalkeeper and Form setupecho "<form valign='top' action='welcome.php' method='post'>				<fieldset id='fsa'>				<legend id='lega'>4-4-2</legend>					<table border='2' width='1120'>							<tr>								<td>									<tr>										<td><label id='lbla'> </label></td>										<td><label id='lbla'> </label></td>										<td><label id='lbla'> </label></td>										<td><label id='lbla'>Goalkeeper</label>																				<select id='ddowna' align='center' onchange='showUser(this.value)'>";										 while($row = mysql_fetch_array($resultgk))  { echo "<option value='" . $row['name'] . "'>" . $row['name'] . "</option>";  }echo "</select>";echo "<div id='txtHint'><p id='paramain'>Once you've chosen the team from which you want to select your Goalkeeper, a list of available Goalkeepers will appear in place of this text.</p></div>";echo"</td></tr>												</table>		</fieldset>		</form>";mysql_close($con);?> 

selectplayer.js

var xmlhttp;function showPlayer(str){xmlhttp=GetXmlHttpObject();if (xmlhttp==null)  {  alert ("Browser does not support HTTP Request");  return;  }var url="getplayer.php";url=url+"?q="+str;url=url+"&sid="+Math.random();xmlhttp.onreadystatechange=stateChanged;xmlhttp.open("GET",url,true);xmlhttp.send(null);}function stateChanged(){if (xmlhttp.readyState==4){document.getElementById("txtHint").innerHTML=xmlhttp.responseText;}}function GetXmlHttpObject(){if (window.XMLHttpRequest)  {  // code for IE7+, Firefox, Chrome, Opera, Safari  return new XMLHttpRequest();  }if (window.ActiveXObject)  {  // code for IE6, IE5  return new ActiveXObject("Microsoft.XMLHTTP");  }return null;}

getplayer.php

<?php$q=$_GET["q"];$con = mysql_connect('127.0.0.1', 'root', '');if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("webproject", $con);$sql=("SELECT surname FROM players WHERE team_id ='".$q."'");$resultgk = mysql_query($sql);echo "<td>																				<select id='ddowna' align='left'>";while($row = mysql_fetch_array($resultgk))  { echo "<option value='" . $row['surname'] . "'>" . $row['surname'] . "</option>";  }echo	"</select></td>";mysql_close($con);?> 

Link to comment
Share on other sites

  • 1 month later...

Hi JSG,Sorry for unbelievably late reply. I'm now sending the 2 variables to the showplayer func

$pos = "Goalkeeper";<select id='ddowna' name='$pos' align='center' onchange='showPlayer(this.value,$pos)'>";

As you've probably guessed i'm not a frequent coder. I've attempted to change the .js as below. (The alert was just to see what i'm sending to the php. )Are there any examples that you know of in this area so i can read up on it. The $pos variable is just getting lost.

function showPlayer(team,position){xmlhttp=GetXmlHttpObject();if (xmlhttp==null)  {  alert ("Browser does not support HTTP Request");  return;  }var url="getplayer.php";url=url+"?q="+team+"&pos="+position;//url=url+"&sid="+Math.random();alert (url);xmlhttp.onreadystatechange=stateChanged;xmlhttp.open("GET",url,true);xmlhttp.send(null);}

Thanks in advance for any help given.

Link to comment
Share on other sites

If you're printing that value using PHP you need to quote the value, Javascript sees it as a string value so it needs to be quoted. Or, you can use this.name instead of printing the name. So if you want to print the name just add quotes around it for java script:echo "<select id='ddowna' name='$pos' align='center' onchange='showPlayer(this.value,\"$pos\")'>";

Link to comment
Share on other sites

Yeah, you just need to get the value to Javascript. Since it's a string, it needs quotes. It's the difference between this:showPlayer(this.value, this is the name)and this:showPlayer(this.value, "this is the name")The first one is not valid Javascript. Even if the name was a single word, Javascript would see that as a variable name, not a string value.

Link to comment
Share on other sites

Ah ok brilliant thanks.Just for my knowledge bank, why does this.value not need the quotes?
Because it's not a string. It's a variable.var HW = "Hello World"HW is a variable, Hello World is a stringStrings need quotes so that they're not confused with variable names. You might need to read the W3Schools Javascript tutorial and understand better how information is used and stored in programs.this.value is a little more complex than a variable, but it's basically the same thing. It's more like a variable within a variable, commonly known as an object. The "this" keyword refers to the element that's firing the event handler. "value" refers to the text that the user has put into the input element. Edited by Ingolme
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...