我想输入名称和标记。当输入的标记小于或等于100时,输入的名称和标记将在我单击提交按钮时存储在关联数组中,它应该请求我输入另一个名称和标记。但是如果我输入一个大于100的标记丢弃输入的名称,当我单击提交按钮时,php脚本应该显示我之前输入的所有名称和标记。这就是我所做的,但我没有得到预期的结果。请帮忙。我的代码:
<?php
if(isset($_POST['lname']) && isset($_POST['marks'])){
$name = $_POST['lname'];
$marks = $_POST['marks'];
$lists = array($name => $marks);
foreach($lists as $name => $marks){
echo $name . '<br>';
echo $marks;
}
}
?>
<form action = "<?php echo $_SERVER['PHP_SELF'];?>" method = "POST">
Name:<br>
<input type = "text" name = "lname"><br><br>
Marks:<br>
<input type = "text" name = "marks"><br><br>
<input type = "submit" value = "Submit">
</form>
答案 0 :(得分:0)
您正在发布name
,而您的服务器端正在寻找lname
答案 1 :(得分:0)
您可以使用会话来实现您想要的功能,请参阅以下代码:
<?php
session_start();
if(isset($_POST['lname']) && isset($_POST['marks'])){
if($_POST['marks'] > 100) {
$_SESSION['info'][] = array($_POST['lname'] => $_POST['marks']);
}
}
if(isset($_SESSION['info'])) {
for($i = 0; $i < count($_SESSION['info']); $i++) {
foreach($_SESSION['info'][$i] as $name => $marks){
echo '<p>' . $name . '<br>';
echo $marks . '</p>';
}
}
}
?>
<form action = "<?php echo $_SERVER['PHP_SELF'];?>" method = "POST">
Name:<br>
<input type = "text" name = "lname"><br><br>
Marks:<br>
<input type = "text" name = "marks"><br><br>
<input type = "submit" value = "Submit">
</form>
您应该阅读一些关于会话的文档: