Php 如何在下一页显示输入信息?
我有一个html文件,其中包含以下代码:Php 如何在下一页显示输入信息?,php,html,Php,Html,我有一个html文件,其中包含以下代码: <input type="button" id="styles" value="Add More" onClick="addRow('data')" /> <form action="mail.php" method="post"> <table id="data"><tr><td> <select name="selectorfirst" id="styles">
<input type="button" id="styles" value="Add More" onClick="addRow('data')" />
<form action="mail.php" method="post">
<table id="data"><tr><td>
<select name="selectorfirst" id="styles">
<option>Yellow</option>
<option>Green</option>
<option>Red</option>
</select>
</td>
<td>
<select name="selectorsecond" id="styles">
<option>375</option>
<option>1000</option>
<option>5000</option>
</select>
</td></tr></table>
<input id="styles" type="submit" value="Submit" />
</form>
按下按钮后,我需要在下一页上发布这些表单中选择的值。我尝试了下面的代码,但不起作用
<? $selectorfirst = $_POST['selectorfirst'];
$selectorsecond = $_POST['selectorsecond']; ?>
<table id="data">
<?php foreach($selector as $key => $value) { ?>
<tr>
<td ><?php echo $a+1; ?></td>
<td>
<input type="text" id="styles" name="selectorfirst[$key]" value="<?php echo $selectorfirst[$key]; ?>">
</td>
<td>
<input type="text" id="styles" name="selectorsecond[$key]" value="<?php echo $selectorsecond[$key]; ?>">
</td>
</tr><?php } ?></table>
$selector
为空或null
foreach需要$selector
来保存值,否则它将无法执行
您需要以数组的形式提交数据,该数组可能如下所示:
array(3) {
[0]=> array(2) {
["first"]=> string(6) "Yellow"
["second"]=> string(3) "375"
}
[1]=> array(2) {
["first"]=> string(5) "Green"
["second"]=> string(4) "1000"
}
[2]=> array(2) {
["first"]=> string(3) "Red"
["second"]=> string(4) "5000"
}
}
<td>
<select name="selector[0][first]" id="styles">
<option>Yellow</option>
<option>Green</option>
<option>Red</option>
</select>
</td>
<td>
<select name="selector[0][second]" id="styles">
<option>375</option>
<option>1000</option>
<option>5000</option>
</select>
</td>
每个数组元素都包含一个表行的数据。它们同样由具有第一个和第二个值的数组表示。在html中,它看起来像这样:
array(3) {
[0]=> array(2) {
["first"]=> string(6) "Yellow"
["second"]=> string(3) "375"
}
[1]=> array(2) {
["first"]=> string(5) "Green"
["second"]=> string(4) "1000"
}
[2]=> array(2) {
["first"]=> string(3) "Red"
["second"]=> string(4) "5000"
}
}
<td>
<select name="selector[0][first]" id="styles">
<option>Yellow</option>
<option>Green</option>
<option>Red</option>
</select>
</td>
<td>
<select name="selector[0][second]" id="styles">
<option>375</option>
<option>1000</option>
<option>5000</option>
</select>
</td>
简而言之,它获取表的第一行,替换name
标记的索引,并将其作为新行添加到表中
然后在mail.php中,获取$\u POST['selector']
并像这样循环:
<?php
$selector = $_POST['selector'];
?>
<table id="data">
<?php foreach($selector as $key => $value) { ?>
<tr>
<td ><?php echo $key; ?></td>
<td>
<input type="text" id="styles" name="selectorfirst[$key]" value="<?php echo $value['first']; ?>">
</td>
<td>
<input type="text" id="styles" name="selectorsecond[$key]" value="<?php echo $value['second']; ?>">
</td>
</tr>
<?php } ?>
</table>
我是否应该将$selector设置为数组,并将$selectorfirst和$selectorsecond放在其中?我不太懂php。然而,$\u POST['selectorfirst']
应该包含一个值,稍后在foreach方法中,您尝试使用$selectorfirst[$key]
并将$selectorfirst
视为一个数组,但它是一个字符串变量;不,谢谢。现在我有另一个问题,当我在最后一页上得到这些值时,它没有正确显示。我只看到替换字符(html�;),而不是我以前在表单中选择的实际值。这是一个单独的问题,但是您应该查看html编码字符。这里似乎就是这样。您能否选择答案并关闭此问题?这个单独的答案可能会对你有所帮助。