开发者

How can I POST a form where name field is generated from a loop?

开发者 https://www.devze.com 2023-03-14 12:10 出处:网络
I\'m so close!! I can\'t figure out how to post these values.Here\'s what I\'ve got: <form action=\"process.php\" method=\"post\">

I'm so close!!

I can't figure out how to post these values. Here's what I've got:

<form action="process.php" method="post">
  <?php 
  foreach (array_combine($UndefinedEvents, $EventDates) as $event=>$dates){
  echo "This Event does not have a Timeline associated with it: " .$event . " on ".$dates. '<br>';
  echo "Choose a Timeline:<br>";
  ?>
  <?php echo "<select name=".$Event开发者_StackOverflowID[$i].">"; ?>
  <option selected = "selected"></option>
  <?php foreach (array_combine($TimelineID, $UserTimelines) as $temptimelineID=>$timeline){ 
        echo "<option value=".$temptimelineID."> ".$timeline. "</option>";

  }
  echo " </select><br><br>";
  $i = $i+1;
  }
  ?>
  <input type="submit" />
  </form>

There's a lot happening above, but the markup is giving me what I want. Here's an example of what the above looks like:

<form action="processGoogle.php" method="post">
  This Event does not have a Timeline associated with it: 
  First Event on 2011-07-01 00:00:00
  <br>
  Choose a Timeline:
  <br>
  <select name=3576> //THIS is $eventID
    <option selected = "selected">    </option>
    <option value=257> Timeline One   </option>
    <option value=258> Timeline Two   </option>
    <option value=259> Timeline Three </option> 
  </select>
  <br>
  <br>
  This Event does not have a Timeline associated with it: 
  Next Event on 2011-06-30 00:00:00
  <br>
  Choose a Timeline:
  <br>
  <select name=3573>//THIS is $eventID
    <option selected = "selected">    </option>
    <option value=257> Timeline One   </option>
    <option value=258> Timeline Two   </option>
    <option value=259> Timeline Three </option> 
  </select>
  <br>
  <br>
  ...
  <input type="submit">
</form>

What I need to do is post the value for $EventID and $temptimelineID.

What do I need to do to pass that information in post, and what do I need to have in my process.php form to read it?

Thanks for any help!


Looks like you have variable $_POST. Try

$x = array_keys($_POST);
foreach($x as $y) {
echo $y ." = ". $_POST[$y]."<br/>";
}


To just see the values of your $_POST superglobal, it would be easier to simply write:

print "<pre>"; var_dump($_POST); print "</pre>";

That said, there are a couple of ways that you can pass which fields are machine generated.

Option 1) Give the fields a unique prefix, such as:

<select name="timeline_3576"> //THIS is $eventID

Then one simply has to look through the $_POST values for the appropriate prefix, explode the string on the underscore and take the second value. Bob's your uncle.

Option 2) Build an array and pass that in a hidden field.

<select name="3576"> //code snipped
<select name="9999"> //code snipped
<input type="hidden" name="timelines" value="3576,9999">
0

精彩评论

暂无评论...
验证码 换一张
取 消