Jump to content

Problem with an If Statement


The.Pr0fess0r

Recommended Posts

Hello, here is my situation...

 

I have a dropdown box that is being populated from a table in my database. What I want to do is to only show certain fields in the form below based on what is selected in this box.

 

I am using some JavaScript in other areas of my website to do just this...the only difference is in the other areas I have my dropdown box data hardcoded.

 

Here is the JavaScript I am using:

<script>
//*********************************************
// Function that Shows an HTML element
//*********************************************
function showDiv(divID)
{
var div = document.getElementById(divID);
div.style.display = ""; //display div
}

//*********************************************
// Function that Hides an HTML element
//*********************************************
function hideDiv(divID)
{
var div = document.getElementById(divID);
div.style.display = "none"; // hide
}
//*****************************************************************************
// Function that Hides all the Div elements in the select menu Value
//*****************************************************************************
function hideAllDivs()
{
//Loop through the seclect menu values and hide all
var species = document.getElementById("species");
for (var i=0; i<=species.options.length -1; i++)
{
hideDiv(species.options[i].value);
}
}
//*********************************************
// Main function that calls others to toggle divs
//*********************************************
function toggle(showID)
{
hideAllDivs(); // Hide all
showDiv(showID); // Show the one we asked for

}


</script>	

 

Here is the code I am using for my dropdown box:

						<?

					$sql = "SELECT species FROM animal_species";

					// Echo

					echo "<SELECT name=\"species\">";

					$result = mysql_query($sql);

					// Loop to get and echo the results as options

					while ($row = mysql_fetch_assoc($result)) {

					echo '<option value="'.$row["species"].'">'
						.$row["species"].'</option>';

					}

					echo "</SELECT>";

					?>

 

OK, so I thought I would just add an if statement saying that if "x" is selected then show this div.

 

Here is the code I have:

						<?

					if ($species = "Whitetailed Deer" OR $species = "Elk") {
						echo ShowDiv(1);
						}
					else {
						echo ShowDiv(2);
						}

					?>

 

But when I view this page I get an error for a call to an undefined function ShowDiv().

 

From my other code that is working I have to add this statement:

onchange="toggle(this.options[this.options.selectedIndex].value)

but i'm not sure where to add it.

 

Any help would be greatly appreciated.

 

Link to comment
https://forums.phpfreaks.com/topic/174479-problem-with-an-if-statement/
Share on other sites

Archived

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

×
×
  • 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.