The.Pr0fess0r Posted September 16, 2009 Share Posted September 16, 2009 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 More sharing options...
MadTechie Posted September 16, 2009 Share Posted September 16, 2009 you can't call a Javascript function as if it was a PHP function, PHP builds the page, Javascript runs on the page. Link to comment https://forums.phpfreaks.com/topic/174479-problem-with-an-if-statement/#findComment-919603 Share on other sites More sharing options...
The.Pr0fess0r Posted September 16, 2009 Author Share Posted September 16, 2009 OK, that makes sense. How would I go about doing this then? Would I need to hard code the options in an HTML <select> tag...or is there a better way of doing this? Link to comment https://forums.phpfreaks.com/topic/174479-problem-with-an-if-statement/#findComment-919611 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.