Jump to content

Code works fine on its own, but not when put inside a table


Vel

Recommended Posts

Not sure if this belongs in this forum or the PHP forum, but as it's the HTML table that breaks it I think it belongs here. I have a form with 2 drop down boxes, one for regions, one for constellations. When the region box is updated it loads the constellation box with all the constellations from that region using Ajax. Unformatted it works perfectly fine, but it's messy so I put it into a table to tidy things up. However 2 things are broken.

 

Firstly, the Ajax code no longer works, it's called correctly (I put an alert into the script to test and that comes up fine) but the constellations selection box doesn't get updated as it should.

 

Secondly, when the page loads it checks the users current location and sets them as the initial $selectedRegion and $selectedConstellation. These are being loaded by the page correctly as they are outputted as a message to the user, but they aren't being used by the table properly as the Region box shows the default option and the Constellation box is empty.

 

The Ajax code in the header is:

<script type="text/javascript">
function funcAjax(str) {
alert("AJAX script called");
if (str=="") {
	document.getElementById("ajaxResult").innerHTML="<select name=\"selectedConstellation\"><option value=\"\">Select Region First</option></select>";
	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("ajaxResult").innerHTML=xmlhttp.responseText;
    	}
  	}
xmlhttp.open("GET","scripts/constellation.php?r="+str+"&p=i",true); //Call script to update drop down box
xmlhttp.send();
}
</script>

 

The code in question is:

<?php
		//Display information on where the character is and the location selected for fleets to be displayed
		echo "<p>Welcome " . $_SESSION['charName'] . ", you are currently in " . $_SESSION['charConstellation'] . ".<br>";
		echo "Currently displaying fleets from " . $selectedConstellation . ", " . $selectedRegion . ".</p>";

		//Display table with form for updating the location selected for fleets to be displayed
		echo "<table class=\"selected\">";
			echo "<form name=\"updateLocation\" method=\"post\" action=\"indextest2.php\">";
			echo "<tr>";
				echo "<td><b>Change Region:</b></td>";
				echo "<td><select name=\"selectedRegion\" onchange=\"javascript:funcAjax(this.value);\">";
				$sqlLoadRegions = "SELECT * FROM regions ORDER BY region";
				$queryLoadRegions = mysql_query($sqlLoadRegions);
				while($rowLoadRegions = mysql_fetch_array($queryLoadRegions)) { //Load all the regions into the drop down menu
					$region = $rowLoadRegions['region'];
					$value = $rowLoadRegions['value'];
					if("$value" == "$selectedRegion") //Test to see if the region being loaded is the current region the user is in
						echo "<option value=\"$value\" selected=\"selected\">$region</option>";
					else
						echo "<option value=\"$value\">$region</option>";
				}
				echo "</select></td>";
			echo "</tr>";
			echo "<tr>";
				echo "<td><b>Change Constellation:</b></td>";
				$table = "constellations" . strtolower($selectedRegion);
				$sqlLoadConstellations = "SELECT * FROM `$table` ORDER BY constellation";
				$queryLoadConstellations = mysql_query($sqlLoadConstellations);
				echo "<td id=\"ajaxUpdate\"><select name=\"selectedConstellation\">"; //Ajax update area
				while($rowLoadConstellations = mysql_fetch_array($queryLoadConstellations)) { //Load all the constellations into the drop down menu
				$constellation = $rowLoadConstellations['constellation'];
				$value = $rowLoadConstellations['value'];
				if("$value" == "$selectedConstellation") //Test to see if the constellation being loaded is the current region the user is in
					echo "<option value=\"$value\" selected=\"selected\">$constellation</option>";
				else
					echo "<option value=\"$value\">$constellation</option>";
				}
				echo "</select></td>"; //End of Ajax update area
			echo "</tr>";
			echo "<tr>";
				echo"<td colspan=\"2\"><input name=\"submit\" type=\"submit\" value=\"Display Fleets\"></td>";
			echo "</tr>";
			echo "</form>"; //End of form to update location selected
		echo "</table>";

 

Can someone help me get this working in a table and explain why it works out of a table but not in a table?

Link to comment
Share on other sites

It's actually just the Ajax not working. The first problem is a coding one, not the tables stopping it form working. How do I get the ajax working again from within the table?

 

EDIT: Damnit, was just a typo. All working now. Can someone please delete this thread?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.