Jump to content

Two Input Forms - One Based Upon Input to the Other


nshaw

Recommended Posts

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 by mac_gyver
code tags around posted code please
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. :happy-04:

 

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!

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.