0

我有这样的代码:我想知道单击提交按钮后如何获取文本框的值,因为在我当前的代码中,它只给了我最后一个 $key 而不是 all 。谢谢

<form>
foreach($array as $key => $values){
echo "<input type='text' name='title' value='$key'/>";
}
<input type='submit' name='submit' value='submit'/>
</form>
<?php
if(isset($_POST[''])){
    //get the result of the textbox
    $title = $_POST['title'];

}
?>
4

4 回答 4

3

如果您希望所有值都使用数组表示法,则[]在元素名称的末尾放置一个,然后您将获得一个值数组。

<form>
foreach($array as $key => $values){
echo "<input type='text' name='title[]' value='$key'/>";
}
<input type='submit' name='submit' value='submit'/>
</form>

...

<?php
    //get the result of the textbox
    $titles = $_POST['title'];
    foreach ($titles as $title){
        echo $title;
    }

?>
于 2013-06-14T04:27:54.450 回答
0

您的测试对if(isset($_POST[''])){您的情况没有帮助。您需要执行以下操作。

if (isset($_POST['title')) {
    $title = $_POST['title'];
}
于 2013-06-14T04:25:30.753 回答
0
<form action="" method="post">

<?php 
$array=array("a","b","c","d" );
foreach($array as $key => $values){
echo "<input type='text' name='title[]' value='$key'/>";
}
?> 

<input type='submit' name='submit' value='submit'/> 
</form>
<?php
if(isset($_POST)){ ECHO "<pre>"; print_r($_POST)}
于 2013-06-14T04:30:51.393 回答
0
you should use the name as array

foreach($array as $key => $values){
echo "<input type='text' name='title[]' value='$key'/>";
}

<?php
    //result of the textbox
    $titles = $_POST['title'];
    print_r($titles);
?>
于 2013-06-14T04:42:30.473 回答