【发布时间】:2018-11-04 14:46:25
【问题描述】:
我的if(isset($_POST['submit'])) 代码有点问题。我想要的是在单击表单的提交按钮时运行 processForm() 函数。问题是,当我包含if(isset($_POST['submit'])) 函数时,当我单击提交按钮时,processFunction() 根本不运行。但是当我包含if( !isset($_POST['submit'])) 时,processForm() 函数运行......为什么这是和你能帮我解决这个问题吗
<html>
<head>
<title>Membership Form</title>
<style type="text/css">
.error { background: #d33; color: white; padding: 0.2em; }
</style>
</head>
<body>
<?php
if ( isset($_POST["submit"])) { //if submit input has value
processForm();//calls the processForm function
}
function processForm(){
$compulsaryForm = array("firstName" , "lastName", "password" , "passwordRetype");
$missingFields = array();
foreach ($compulsaryForm as $input) { //loop
if (!isset($_POST[$input])) {
$missingFields[] = $input;
} // forget the else part cause we mainly want to avoid the warning msg
}
if ($missingFields) {
echo "<p>there are missingFields</p>";
}
}
?>
<form action="htmlForms2.php" method="post">
*name:<input type="text" name="firstName"> <br><br>
*last name: <input type="text" name="lastName"> <br><br>
*password<input type="password" name="password"> <br><br>
*retype password<input type="password" name="passwordRetype"><br><br>
male:<input type="radio" name="sex"><br>
female:<input type="radio" name="sex"><br>
favourite food:<select name = "favourite">
<option value="select">select</option>
<option value="rice">rice</option>
<option value="beans">beans</option>
</select><br>
do you want to recieve news letter?<input type="checkbox" name="newsLetter"><br>
Any comments? :<input type="text" name="comments"><br>
<input type="reset" name="reset">
<input type="submit" name="submit" value="submit">
</form>
【问题讨论】:
-
显示的代码应该可以工作(就 isset 而言)。请在此
if之前执行var_dump($_POST);以查看其中的内容。 -
旁注:你需要为你的收音机“sex”设置值。
-
你的函数定义是在你第一次调用函数之后。尝试将函数定义移到
if-then之前。否则 Jeff 是正确的 - 它应该工作 -
你可以 var_dump 的 $_POST 吗,你也可以用 !empty 代替 isset
-
旁注: 这个
if (!isset($_POST[$input]))不会像你期望的那样工作。像“”这样的空名字将被“设置”。你可能想检查empty()。