Jump to content

Find out which POST variable = foo


bschultz

Recommended Posts

Step two of this project requires building the html form (dynamically) based on what the user has already selected.  I thought there was a way to do this in php, but my searching on Google lead me to jquery and ajax (of which I know neither of them!).

 

I want to have an array of options for a form select box.  Once the user selects the first select box, a second select box shows up with every option EXCEPT WHAT WAS SELECTED IN THE FIRST BOX...and repeats it's self until the array is empty (every array item has been selected).

 

Can this be done in PHP?

 

This is what I have so far...which does work...but it's only 2 select boxes...of 20!  I can only hope it can done simpler than this:

 

<?php
$fields = array('Number', 'Name', 'Picture', 'Position', 'Height', 'Weight', 'Year', 'Home', 'Last School');
$submitted = array("$_POST[column1]", "$_POST[column2]", "$_POST[column3]", "$_POST[column4]", "$_POST[column5]", "$_POST[column6]", "$_POST[column7]", "$_POST[column8]", "$_POST[column9]", "$_POST[column10]");
?>


<form name="form1" method="post" action="">
  Column 1 - <label>
        <select name="column1" id="column1">
          <option value="">Select The Value of Column 1</option> 
          <?php
          foreach ($fields as $field)
          { echo "<option value='$field'"; if ($_POST['column1'] == $field) { echo " selected"; } echo ">$field</option>\n"; }
	   ?>
          
        </select>
      </label><br>
<br>
      
      <?php
  if (isset($_POST['column1']))
  {
  echo 'Column 2 - <select name="column2" id="column2">';
  $column_array[] = $_POST['column1'];
  $array_diff = array_diff($fields,$column_array);
  
      if (isset($_POST['column2'])) 
  { echo "$_POST[column1]"; }
  else
  { foreach ($array_diff as $fields)
    { echo "<option value='$fields'"; if ($_POST['column2'] == $fields) { echo " selected"; } echo ">$fields</option>\n"; }
  }
  } echo "</select>";
  ?>
  
<br /><br />

  <label>
  <input type="submit" name="button" id="button" value="Submit">
  </label>
</form>

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.