nshaw Posted January 19, 2015 Share Posted January 19, 2015 (edited) I hope I can explain what is happening. I have created two forms in PHP. The first 'almost' works, i.e. it shows the data. But I have two problems - 1) the second pulldown menu is always empty and 2) $value from the first pulldown menu ALWAYS equals the last entry thus the last 'if' in the function subdomains ($domains) is always called (but still empty). The code may explain this better than me: <!DOCTYPE html> <html> <body> <!-- processDomains.php is this file - it calls itself (for testing purposes so I can see what is happening) --> <form action="processDomains.php" method="post"> <?php // create the domains array (there are actually several entries in the array but I cut it down for testing) $domains = array (1 => 'Decommission', 'Migration'); echo "Select Domain:"; echo "<br>"; // Make the domain pull-down menu - this displays correctly echo '<select name="domain">'; foreach ($domains as $key => $value) { echo "<option value=\"$key\">$value</option>\n"; } echo '</select>'; // input doesn't matter what is 'submitted', always goes to last $value echo '<input type="submit" name="submit" value="Submit">'; // call function subdomains subdomains ($value); function subdomains ($domains) { // define values for each array - each array contains available choices for the subdomain pulldown menu $migration = array (1 => 'Application Migration', 'Application Patch', 'Application Upgrade'); $decommission = array (1 => 'Applications', 'Servers', 'Storage'); if ($domains === 'Migration') { echo "Select subdomain:"; echo "<br>"; // Make the Migration pull-down menu echo '<select name="migration">'; foreach ($migration as $key => $value) { echo "<option value=\"$key\">$value</option>\n"; } echo '</select>'; } else if ($domains === 'Decommission') { /* === * since 'Decommission' is the last entry in the 'Domains' pulldown list, $value ALWAYS equals * 'Decommission' and $domains equals $value. So this menu SHOULD work but is always * empty. Thus, two problems - the pulldown menu is always empty and $value isn't based * upon user input. */ echo "Select subdomain:"; // this prints so I know I'm in 'Decommission (I eliminated the echo "$domain" to show I'm always coming here)' echo "<br>"; // Make the 'Decommission' pull-down menu echo '<select name="decommission">'; foreach ($decommission as $key => $value) { echo "<option value=\"$key\">$value</option>\n"; } echo '</select>'; echo '<input type="submit" name="submit" value="Submit">' ) // end of 'if-else' } // end of function 'subdomain' ?> </form> </body> </html> Let me say thank you in advance and I appreciate the help! I know I'm doing something (or more than one thing) wrong and I hope someone can tell me what it is. Best Regards! Edited January 20, 2015 by mac_gyver code tags around posted code please Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 19, 2015 Share Posted January 19, 2015 1 - please wrap your code properly in tags 2 - I believe you have either a rarely used syntax in your array definitions or a completely wrong syntax. Usually it is done: $array = array(1=>'first entry',2=>'second entry',3=>'third entry'); This could be a problem for your process. Also - turn on php error checking - it could point out errors that may occur. PS - you could utilize single quotes inside your double-quoted strings to make it easier to type and read. Quote Link to comment Share on other sites More sharing options...
nshaw Posted January 19, 2015 Author Share Posted January 19, 2015 Thank you, ginergm, My apologies for not wrapping my code using proper tags. I'll try your PS for php error checking. The format I'm using for my array is from an older book so it may be an issue; I'll change it and see what happens. Thank you! Quote Link to comment Share on other sites More sharing options...
nshaw Posted January 19, 2015 Author Share Posted January 19, 2015 I'm using the NetBeans IDE v.8.0.2 and it has errors on by default. If I make an error in my code, it shows me where the error is and says what the error is. Right now, I'm not getting any errors; however, changing the format of my array did solve the problem of an empty pulldown list. THANK YOU! Once I get the function to accept the user input from the first pulldown menu then I'm set! :-) Again - thank you! Quote Link to comment Share on other sites More sharing options...
Barand Posted January 20, 2015 Share Posted January 20, 2015 2 - I believe you have either a rarely used syntax in your array definitions or a completely wrong syntax. Usually it is done: $array = array(1=>'first entry',2=>'second entry',3=>'third entry'); The array code is perfectly valid. If you want an indexed array to start at 1 instead of 0 $array = array( 1 => "a", "b", "c", "d", ); echo '<pre>',print_r($array, true),'</pre>'; RESULT Array ( [1] => a [2] => b [3] => c [4] => d ) See PHP manual http://php.net/manual/en/language.types.array.php example #5 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 20, 2015 Share Posted January 20, 2015 Another day; another bit of knowledge. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted January 20, 2015 Share Posted January 20, 2015 if your select/option menu(s), inside the subdomains() function, are supposed to be dependent on the value that is submitted from the domain select/option menu, the current code is missing how browsers and web servers (http requests and responses) work. when your page gets requested by the browser, the domain select/option menu is output to the browser. the next step would be for the visitor to select one of the options and submit the form. this will cause the page to be requested again with $_POST['domain'] containing the value of the option that was selected. it's then up to the code to detect that a form was submitted and take the $_POST['domain'] value and use it the way it wants. you need a specific section of form processing code, and any form porcessing code should be near the start of your file and be before you output any of the html on the page (i.e. before the <!DOCTYPE html> tag.) lastly, if this code is just determining what to display on the page, your form(s) should use method='get', not post. Quote Link to comment Share on other sites More sharing options...
nshaw Posted January 20, 2015 Author Share Posted January 20, 2015 Thank you, mac_gyver! Your post helped me solve the problem (there were a couple). I changed 'post' to 'get' and inside the function subdomains, I created a variable called $value = $_REQUEST['domain']. This returns the numeric value of each domain so I created a quick conversion to the proper string and all works! I also stopped passing a variable to the function and, instead, created an empty string variable called $domains. Again, I want to thank everyone for their help and assistance - I couldn't have done it without your help. Now, one quick question - I'm signed in but I cannot select the 'Like This' button, ever. Why is that? I'd like to give credit to those who have helped me. Best Regards! Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted January 21, 2015 Share Posted January 21, 2015 it's likely (pun intended) that you cannot like things until you are a full member (which i believe currently starts at 10 posts.) Quote Link to comment Share on other sites More sharing options...
nshaw Posted January 21, 2015 Author Share Posted January 21, 2015 Well then, I'm 4 posts away... Again, thanks to all for your help! I couldn't have done it without you and, in the future, I will remember to bound my code properly with tags (thanks to whoever did it for me). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.