Jump to content

Sending Multiple Values To A Function That Creates An Arry.


mckenzie

Recommended Posts

Hi all,How do i go about structuring my Javascript function to expect multiple calls from 11 different drop down boxes.Each drop down box has an onchange event which sends 'this.value' to that function. What I want is for that function to collect those values in an array.Do i have to declare 11 seperate parameters?Below is one of the drop down's.

switch ($qa) 	{//----------------------------------------------------------------------------------------------------------------------------------------------------------------1	case ("GK"):				echo "<td>";						echo "<select id='ddowna' onchange='myfunc(this.value)'>";				while($row = mysql_fetch_array($resultgk))				  {				 echo "<option value='" . $row['id'] . "'>" . $row['surname'] . "</option>";				  }				echo "</select>";				echo "</td>";		break;

Below is the piss poor function which grabs one value, but no more than one:

function myfunc(blah){var po = blah;var myCars = [po];for(i=0;i<myCars.length;i++)document.write(myCars[i]);	}

Link to comment
Share on other sites

Hi, isn't this a javascript question?nevermind, you could just use a global variable for the JSvar myvalues = new Array();and then inside your functionmyvalues.push(po);

Link to comment
Share on other sites

Hi, isn't this a javascript question?nevermind, you could just use a global variable for the JSvar myvalues = new Array();and then inside your functionmyvalues.push(po);
Yeh sorry about that, as I read through my post it did occur to me that this was more Jscript related.Bit of a newby but is there a way of preventing .push() from outputting the array length afterward and just storing the value? Or is there a better method. I'm looking at splice()Thanks for the advice.
Link to comment
Share on other sites

  • 2 weeks later...

Ah yes, thanks for that Justsomeguy. Feeling slightly embarrassed about that. It works perfectly now.I have come across another problem with my code that is related. I have a drop down box populated via a mysql db. There is an onchange event attached to it that calls a javascript function. This javascript function then creates an object via Ajax and replaces the <div> (just a label) below the first drop down with another drop down. This second drop down box also has an onchange event which calls a function that replaces both drop (a div within a div, within a div!) down boxes with an HTML label and a button. The Button has an onclick event that calls another jscript function that replaces the label and button with the previous code, showing only one drop down box (as from the start). My problem is that when the user clicks on the change button and the first drop down box appears, it is not populated with anything!I know its probably got something to do with the way I’ve echo out the for loop in "Replace label and button with first drop down's code".I would preferably want to avoid reloading the page.any ideas?The mainbody of the page:

<form>	<fieldset id='pitchform'>				<legend id='pitchlegend'>4-4-2</legend>					<label id= 'pitchgklabel'>GOALKEEPER</label>						<div id = 'pitch_div_c'>							<div id ='pitch_div_b'>																			<select  id='gkddteams' name='GK' onchange='gk_teams_dd_pitch(this.value,this.name)'>											 									<?php																		for ($i=0; $i < $num_rows; $i++)									  {									$id = mysql_result($resultgk,$i,0);									$name = mysql_result($resultgk,$i,1);									echo "<option value='" .$id. "'>" .$name. "</option>";									  }									?>								</select>																				<div id='pitch_div_a'><label>A list of available Goalkeepers will appear <u>here</u> once you've chosen a team to pick 	from.</label></div>							</div>						</div>																	</fieldset></form>

Javascript functions

var xmlhttp;var lineup = new Array();function gk_teams_dd_pitch(pitch_team_choice,pitch_team_pos_name){xmlhttp=GetXmlHttpObject();if (xmlhttp==null)  {  alert ("Browser does not support HTTP Request");  return;  }  			if (pitch_team_pos_name == "GK")			{									var url="getplayerlist.php";				url=url+"?q="+pitch_team_choice+"&qa="+pitch_team_pos_name;				url=url+"&sid="+Math.random();				alert (url);				xmlhttp.onreadystatechange=stateChanged;				xmlhttp.open("GET",url,true);				xmlhttp.send(null);			}					else if (pitch_team_pos_name == "RB")					{						var url="getplayerlist.php";						url=url+"?q="+pitch_team_choice+"&qa="+pitch_position;						url=url+"&sid="+Math.random();						alert (url);						xmlhttp.onreadystatechange=stateChanged;						xmlhttp.open("GET",url,true);						xmlhttp.send(null);									}	}//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------function stateChanged(){if (xmlhttp.readyState==4){document.getElementById("pitch_div_a").innerHTML=xmlhttp.responseText;}}//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------function gk_teams_dd_pitch_chosen(pitch_player_choice,pitch_team_pos_name_chosen){xmlhttp=GetXmlHttpObject();if (xmlhttp==null)  {  alert ("Browser does not support HTTP Request");  return;  }	if (pitch_team_pos_name_chosen == "GK") 	{			var url="chooseplayer.php";			url=url+"?r="+pitch_player_choice+"&qa="+pitch_team_pos_name_chosen;			url=url+"&sid="+Math.random();			alert (url);			xmlhttp.onreadystatechange=stateChanged_chosen;			xmlhttp.open("GET",url,true);			xmlhttp.send(null);		}	else if (pitch_team_pos_name_chosen == "NOTGK")		{				var urla="changechoice.php";				urla=urla+"?r="+pitch_player_choice+"&qa="+pitch_team_pos_name_chosen;				urla=urla+"&sid="+Math.random();				alert (urla);				xmlhttp.onreadystatechange=stateChanged_change;				xmlhttp.open("GET",urla,true);				xmlhttp.send(null);		}	}//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------function stateChanged_chosen(){if (xmlhttp.readyState==4){document.getElementById("pitch_div_b").innerHTML=xmlhttp.responseText;}}//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------function stateChanged_change(){if (xmlhttp.readyState==4){document.getElementById("pitch_div_c").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;}//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

First Checkbox appear onchange

echo "<select id='gkddteamstwo' name='GK' onchange='gk_teams_dd_pitch_chosen(this.value,this.name)'>";				while($row = mysql_fetch_array($resultgk))				  {				 echo "<option value='" . $row['pl_id'] . "'>" . $row['surname'] . "</option>";				  }				echo "</select>";								echo "</td>";				echo "<label id='confirmposlabel3'>Please choose a Goalkeeper</label>";

Replace both drop down with label and button

echo	"<label id='confirmposlabel'>Your GoalKeeper is:</label>			<label id='confirmposlabel2'>$r</label>			<input id='button2' type='button' method='post' name='NOTGK' value='Change' onclick='gk_teams_dd_pitch_chosen(this.value,this.name)'/>";

Replace label and button with first drop down's code.

echo	"<div id = 'pitch_div_c'>";						echo	"<div id ='pitch_div_b'>";																	echo	"<select  id='gkddteams' name='GK' onchange='gk_teams_dd_pitch(this.value,this.name)'>";									 	 													echo	"<?php";																	for ($i=0; $i < $num_rows; $i++);						echo	"{";						echo	'"{$id = mysql_result($resultgk,$i,0);"';						echo	'"$name = mysql_result($resultgk,$i,1);"';															echo	"echo '<option value=''.$id.'>'.$name.''</option>'';'";						echo	"}";						echo	"?>";						echo 	"</select>";																	echo	"<div id='pitch_div_a'><label>A list of available Goalkeepers will appear <u>here</u> once you have chosen a team to pick from.</label></div>							</div>";

I apologise for the poor layout of the code.

Link to comment
Share on other sites

If this is the code to create the dropdown that is empty:

echo "<select id='gkddteamstwo' name='GK' onchange='gk_teams_dd_pitch_chosen(this.value,this.name)'>";				while($row = mysql_fetch_array($resultgk))				  {				 echo "<option value='" . $row['pl_id'] . "'>" . $row['surname'] . "</option>";				  }				echo "</select>";

Then I would check whatever query you're running there to make sure it's returning the results you expected. It also looks like you output a </td> tag after that, but I don't see anything that matches up with.

Link to comment
Share on other sites

Sorry, just posted snippets of my code, probably not the best idea.So the problem is that when the user clicks on the change button and the first drop down box appears, it is not populated with anything!So the first drop down is displayed on the page as so:

<?php$con = mysql_connect("127.0.0.1","root","");if (!$con)  {  die('Could not connect: ' . mysql_error());  }	   mysql_select_db("webproject", $con);$sql = 	("SELECT id,name FROM teams");$resultgk = mysql_query ($sql, $con);$num_rows = mysql_num_rows($resultgk);mysql_close($con);?><form>	<fieldset id='pitchform'>				<legend id='pitchlegend'>4-4-2</legend>					<label id= 'pitchgklabel'>GOALKEEPER</label>						<div id = 'pitch_div_c'>							<div id ='pitch_div_b'>																			<select  id='gkddteams' name='GK' onchange='gk_teams_dd_pitch(this.value,this.name)'>											 									<?php																		for ($i=0; $i < $num_rows; $i++)									  {									$id = mysql_result($resultgk,$i,0);									$name = mysql_result($resultgk,$i,1);									echo "<option value='" .$id. "'>" .$name. "</option>";									  }									?>								</select>																				<div id='pitch_div_a'><label>A list of available Goalkeepers will appear <u>here</u> once you've chosen a team to pick 	from.</label></div>							</div>						</div>																	</fieldset></form>

With the onchange activated goes to the following js code:

var xmlhttp;var lineup = new Array();function gk_teams_dd_pitch(pitch_team_choice,pitch_team_pos_name){xmlhttp=GetXmlHttpObject();if (xmlhttp==null)  {  alert ("Browser does not support HTTP Request");  return;  }  			if (pitch_team_pos_name == "GK")			{									var url="getplayerlist.php";				url=url+"?q="+pitch_team_choice+"&qa="+pitch_team_pos_name;				url=url+"&sid="+Math.random();				alert (url);				xmlhttp.onreadystatechange=stateChanged;				xmlhttp.open("GET",url,true);				xmlhttp.send(null);			}					else if (pitch_team_pos_name == "RB")					{						var url="getplayerlist.php";						url=url+"?q="+pitch_team_choice+"&qa="+pitch_position;						url=url+"&sid="+Math.random();						alert (url);						xmlhttp.onreadystatechange=stateChanged;						xmlhttp.open("GET",url,true);						xmlhttp.send(null);									}	}//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------function stateChanged(){if (xmlhttp.readyState==4){document.getElementById("pitch_div_a").innerHTML=xmlhttp.responseText;}}//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------function gk_teams_dd_pitch_chosen(pitch_player_choice,pitch_team_pos_name_chosen){xmlhttp=GetXmlHttpObject();if (xmlhttp==null)  {  alert ("Browser does not support HTTP Request");  return;  }	if (pitch_team_pos_name_chosen == "GK") 	{			var url="chooseplayer.php";			url=url+"?r="+pitch_player_choice+"&qa="+pitch_team_pos_name_chosen;			url=url+"&sid="+Math.random();			alert (url);			xmlhttp.onreadystatechange=stateChanged_chosen;			xmlhttp.open("GET",url,true);			xmlhttp.send(null);		}	else if (pitch_team_pos_name_chosen == "NOTGK")		{				var urla="changechoice.php";				urla=urla+"?r="+pitch_player_choice+"&qa="+pitch_team_pos_name_chosen;				urla=urla+"&sid="+Math.random();				alert (urla);				xmlhttp.onreadystatechange=stateChanged_change;				xmlhttp.open("GET",urla,true);				xmlhttp.send(null);		}	}//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------function stateChanged_chosen(){if (xmlhttp.readyState==4){document.getElementById("pitch_div_b").innerHTML=xmlhttp.responseText;}}//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------function stateChanged_change(){if (xmlhttp.readyState==4){document.getElementById("pitch_div_c").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;}//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This then displays the second drop-down box below the first dorp down, as shown below:

<?php$qa=$_GET["qa"];$q=$_GET["q"];	$con = mysql_connect('127.0.0.1', 'root', '');		if (!$con) {			die('Could not connect: ' . mysql_error());		}				mysql_select_db("webproject", $con);		$sqla= "SELECT pl.surname, pl.position_id, t.id, pl.pl_id FROM players AS pl JOIN position AS po ON po.p_id = pl.position_id JOIN teams AS t ON t.id = pl.team_id WHERE t.id = '".$q."' AND pl.position_id = '1'";$resultgk = mysql_query($sqla);	switch ($qa) 	{//----------------------------------------------------------------------------------------------------------------------------------------------------------------1	case ("GK"):echo "<td>";									echo "<select id='gkddteamstwo' name='GK' onchange='gk_teams_dd_pitch_chosen(this.value,this.name)'>";				while($row = mysql_fetch_array($resultgk))				  {				 echo "<option value='" . $row['pl_id'] . "'>" . $row['surname'] . "</option>";				  }				echo "</select>";								echo "</td>";				echo "<label id='confirmposlabel3'>Please choose a Goalkeeper</label>";						break;			default:				echo "I don't know how....but you messed up!";						break;				}			mysql_close($con);?>

The second drop down box then has an onchange to remove both drop down boxes and replace with this:

<?php$qa=$_GET["qa"];$r=$_GET["r"];switch ($r){case ("7"):	echo	"<label id='confirmposlabel'>Your GoalKeeper is:</label>			<label id='confirmposlabel2'>$r</label>			<input id='button2' type='button' method='post' name='NOTGK' value='Change' onclick='gk_teams_dd_pitch_chosen(this.value,this.name)'/>";	break;default:	echo	"<table>			<tr>			<td>			You messed up!			</td>			</tr>			</table>";}?>

The button in the above code has an onchange event pointing to the function gk_teams_dd_pitch_chosen, which goes full circle and replaces the first drop down bos code.

<?php$qa=$_GET["qa"];$r=$_GET["r"];switch ($r){case ("Change"):												echo	"<div id = 'pitch_div_c'>";						echo	"<div id ='pitch_div_b'>";																	echo	"<select  id='gkddteams' name='GK' onchange='gk_teams_dd_pitch(this.value,this.name)'>";									 	 																							for ($i=0; $i < $num_rows; $i++);								{						echo	"$id = mysql_result($resultgk,$i,0)";						echo	"$name = mysql_result($resultgk,$i,1)";															echo 	"<option value='".$id."'>".$name."</option>";								}												echo 	"</select>";																	echo	"<div id='pitch_div_a'><label>A list of available Goalkeepers will appear <u>here</u> once you've chosen a team to pick from.</label></div>								</div>";						echo	"</div>";																								break;default:	echo	"<table>			<tr>			<td>			You Messed Up!			</td>			</tr>			</table>";}?>

sorry to have described it again, my mistake for leaving code out at the start.

Link to comment
Share on other sites

If the dropdown is empty it's the same thing, whatever you're getting from the database is empty. You may be getting an error, you're not checking for those. If mysql_query returns false then there was an error with the query:$resultgk = mysql_query($sqla) or exit(mysql_error());

Link to comment
Share on other sites

If the dropdown is empty it's the same thing, whatever you're getting from the database is empty. You may be getting an error, you're not checking for those. If mysql_query returns false then there was an error with the query:$resultgk = mysql_query($sqla) or exit(mysql_error());
I've viewed the source of the page when i've returned to the first drop down box via the button and it shows the database values. Just not on the actual page.Haven't implemented the mysql_error yet. Will let you know how i get on.
Link to comment
Share on other sites

What a great little tool Firebug is! Amazing!I've amended the code that replaces the div with the id of 'pitch_div_c'. See below.

<?php$qa=$_GET["qa"];$r=$_GET["r"];$con = mysql_connect("127.0.0.1","root","");if (!$con)  {  die('Could not connect: ' . mysql_error());  }	   mysql_select_db("webproject", $con);$sqla = ("SELECT id,name FROM teams");$resultgka = mysql_query ($sqla, $con) or exit (mysql_error());$num_rows = mysql_num_rows($resultgka);switch ($r){case ("Change"):																		echo	"<div id ='pitch_div_b'>";																	echo	"<select  id='gkddteams' name='GK' onchange='gk_teams_dd_pitch(this.value,this.name)'>";									 	 																							for ($i=0; $i < $num_rows; $i++);								{								$ida = mysql_result($resultgka,$i,0);								$namea = mysql_result($resultgka,$i,1);															echo 	"<option value='".$ida."'>".$namea."</option>";								}												echo 	"</select>";																	echo	"<div id='pitch_div_a'><label>A list of available Goalkeepers will appear <u>here</u> once you've chosen a team to pick from.</label></div>								</div>";																														break;default:	echo	"<table>			<tr>			<td>			You Messed Up!			</td>			</tr>			</table>";}?>

The GET function is returning this message displayed in Firebug:

<div id ='pitch_div_b'><select  id='gkddteams' name='GK' onchange='gk_teams_dd_pitch(this.value,this.name)'><br /><b>Warning</b>:  mysql_result() [<a href='function.mysql-result'>function.mysql-result</a>]: Unable to jump to row 3 on MySQL result index 3 in <b>C:\Program Files\xampp\htdocs\Web_Project\changechoice.php</b> on line <b>28</b><br /><br /><b>Warning</b>:  mysql_result() [<a href='function.mysql-result'>function.mysql-result</a>]: Unable to jump to row 3 on MySQL result index 3 in <b>C:\Program Files\xampp\htdocs\Web_Project\changechoice.php</b> on line <b>29</b><br /><option value=''></option></select><div id='pitch_div_a'><label>A list of available Goalkeepers will appear <u>here</u> once you've chosen a team to pick from.</label></div>								</div>

any ideas? I've done some investigating but no one answer is conclusive.

Link to comment
Share on other sites

If i comment out $num_rows = mysql_num_rows($resultgk)I can get the following output:

<div id ='pitch_div_b'><select  id='gkddteams' name='GK' onchange='gk_teams_dd_pitch(this.value,this.name)'><option value='1'>Aston Villa</option></select><div id='pitch_div_a'><label>A list of available Goalkeepers will appear <u>here</u> once you've chosen a team to pick from.</label></div>								</div>

Sadly it only displays one of the 3 options.I'm getting closer but need a bit of help.

Link to comment
Share on other sites

The original error:Unable to jump to row 3 on MySQL resultsounds like there are less than 3 rows in the result. You can loop through all of the rows using a while loop without a counter like this:

while ($row = mysql_fetch_assoc($resultgka)){  echo "<option value='".$row['id']."'>".$row['name']."</option>";}

In doing that I noticed a problem. This was your original for loop:

for ($i=0; $i < $num_rows; $i++);{  $ida = mysql_result($resultgka,$i,0);  $namea = mysql_result($resultgka,$i,1);  echo "<option value='".$ida."'>".$namea."</option>";}

The end of your for statement has a semicolon:for ($i=0; $i < $num_rows; $i++);That ends the loop. That's the same thing as doing this:for ($i=0; $i < $num_rows; $i++) {}It just loops but does nothing, it just increments $i until it's no longer less than $num_rows. So then it tries to execute the code after that and since $i is set to $num_rows + 1, you get the error that it tries to access a row that doesn't exist (index 3 in the error message would actually be the fourth row, not the third row; the first row is index 0). If you removed that semicolon the for loop would work like you want it to.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...