Jump to content

Find out which POST variable = foo


bschultz

Recommended Posts

Is there a function to determine which $_POST variable = foo?

 

I have a form with 20 elements...and need to know which one equals another variable that's set in the script...and don't want to write a 400 layer deep if statement.

 

Thanks!

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/268617-find-out-which-post-variable-foo/
Share on other sites

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>

You will have to use AJAX for that. jQuery is easy to pick up, but if you don't want to spend the time to learn it, hire someone to do it for you.

 

However, rather than doing all those select boxes, I would use a jQuery sortable list.

http://docs.jquery.com/UI/Sortable

http://jqueryui.com/demos/sortable/

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.