How to use method $_POST in php
index.php:
<form action="post.php" method="POST">
<input type="text" name="option_<?php echo $i ?>" value="" />
<input type="submit" name="btnSubmit" value="submit">
</form>
开发者_StackOverflow社区
The $i
variable is a sequence running from 1 to $n
.
post.php:
if(isset($_POST['btnSubmit']))
{
$option_name = ???;
}
How do I get the text that was posted for each option?
Why not use PHP arrays for this?
<form action="" method="POST">
<input type="text" name="option[]" value="" />
<input type="submit" name="btnSubmit" value="submit">
</form>
In the file it is posted to:
if(isset($_POST['btnSubmit']))
{
foreach ($_POST['option'] as $i => $value)
{
echo "Option $i is $value\n";
}
}
<form action="" method="POST">
<input type="text" name="option_<?php echo $i ?>" value="" />
<input type="submit" name="btnSubmit" value="submit">
</form>
this is your form
you may store it to a variable and use that variable in $_POST
$name= "option_".$id;
if(isset($_POST['btnSubmit']))
{
$option_name = $_POST[$name];
}
in the file post.php you can do
$option_name = $_POST['option_1'];
and then $option_name has the value of the input field with name option_1
if $i is a seuqennce, you could asve the last $i value in an hidden field as a counter:
//in index.php
for ($i = 0; $i < $max; $i++){
<input type="text" name="option_<?php echo $i ?>" value="" />
if ($i = $max -1){//it's the last iteration, save the counter
<input type="hidden" name="counter" value="$i">
}
}
//in post.php
$counter = $_POST['counter'];
$options = array();
for ($i = 0; $i < $counter+1; $i++){
$options[] = $_POST["option_$i"];
}
精彩评论