Jump to content

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

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.