我想将表单的所有字段设置为" required"。我知道已经有类似的问题有许多好的答案,但不幸的是我找不到我的代码的问题。
我需要的是仅在用户将某些字段留空时才显示错误消息,但是如果我在表单中回答所有问题则显示错误"糟糕...所有字段都是必需的&#34 ;。有人可以帮助我吗?
以下是我的尝试:
session_start();
if(isset($_POST['submit']))
{
$_SESSION['q1'] = $_POST['q1'];
$_SESSION['q2'] = $_POST['q2'];
$_SESSION['q3'] = $_POST['q3'];
$_SESSION['q4'] = implode(',', $_POST['birthplace']);
$_SESSION['q5'] = implode(',', $_POST['countrylived']);
$q1 = mysql_real_escape_string($_SESSION['q1']);
$q2 = mysql_real_escape_string($_SESSION['q2']);
$q3 = mysql_real_escape_string($_SESSION['q3']);
$q4 = mysql_real_escape_string($_SESSION['q4']);
$q5 = mysql_real_escape_string($_SESSION['q5']);
$required = array('q1', 'q2', 'q3', 'q4', 'q5');
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if ($error) {
echo "Oooops!! It seems you did not answer all the questions. Please not that all fields are required.";
}
else {
$query="INSERT INTO test (q1, q2, q3, q4, q5) VALUES ('$q1', '$q2', '$q3', '" . $q4 . "', '" . $q5 . "')";
mysql_query($query) or die (mysql_error() );
header('Location: Thankyou.php');
exit;
}
}
?>
编辑:这是我的html表单的一部分。 (抱歉,它包含很多问题所以我只选择了与q1-5相关的部分)
<ul id="page_1">
<div class="meter"><span style="width: 20%">Step 1</span></div>
<fieldset id = "Form1"> <h3> <legend>Please answer the following questions carefully in order to help us providing you with the best recommendation</legend> </h3>
<fieldset id = "q1"> <legend class="Q1"></legend>
<span> What is your gender?</span>
<div class="fieldset content">
<p>
<Input type = 'radio' Name ='q1' value = 'male'>Male
<Input type = 'radio' Name ='q1' value = 'female'>Female
</p>
</div>
</fieldset>
<fieldset id = "q2"> <legend class="Q2"></legend>
<span> What is your age?</span>
<div class="fieldset content">
<p>
<Input type = 'radio' Name = 'q2' value = 'under 18'>Under 18
<Input type = 'radio' Name = 'q2' value = '18-30'>18-30
<Input type = 'radio' Name = 'q2' value = '31-40'>31-40
<Input type = 'radio' Name = 'q2' value = '41-60'>41-60
<Input type = 'radio' Name = 'q2' value = 'over 60'>Over 60
</p>
</div>
</fieldset>
<fieldset id = "q3"> <legend class="Q3"></legend>
<span> 3. What is your marital status?</span>
<div class="fieldset content">
<p>
<Input type = 'radio' Name = 'q3' value = 'married'>Married or domestic partnership
<Input type = 'radio' Name = 'q3' value = 'single'>Single
<Input type = 'radio' Name = 'q3' value = 'would rather not say'>Would rather not say
<p>
</div>
</fieldset>
<fieldset id = "q4"> <legend class="Q4"></legend>
<span> 4. Where were you born?</span>
<div class="fieldset content">
<p>
<?php
mysql_connect("localhost", "user", "Mypass") or die("Connection Failed");
mysql_select_db("imdb")or die("Connection Failed");
$query = "SELECT * FROM Country";
$result = mysql_query($query);
?>
<select name="birthplace[]" multiple="multiple" width="200px" size="10px">
<?php while ($line = mysql_fetch_array($result)) {
?>
<option value="<?php echo $line['country'];?>"> <?php echo $line['country'];?>
</option>
<?php
}
?>
</select>
</p>
</div>
</fieldset>
<fieldset id = "q5"> <legend class="Q5"></legend>
<span> 5. Please select the country/countries where you have lived there at least for 5 years?</span>
<div class="fieldset content">
// THE SAME PROCEDURE AS Q4 //
......
.....
<input type="submit" name="submit" value="Submit" class="button">
</fieldset>
</ul>
</form>
</div>
</body>
</html>
答案 0 :(得分:1)
你的问题是你的代码检查的变量总是空的:注意你的表单中看起来好像没有$ _POST ['q4']或$ _POST ['q5']。
在您的检查器中,尝试测试您已分配的会话字段:
$required = array('q1', 'q2', 'q3', 'q4', 'q5');
$error = false;
foreach($required as $field) {
if (empty($_SESSION[$field])) {
$error = true;
}
}
或者,您可能想要检查您的出生地和国家/地区的字段。如果它们不是数组,那么implode函数将不会返回任何内容。
如果这不起作用,请添加:
if(empty($_SESSION['q1'])) {
echo "q1 is empty!";
}
if(empty($_SESSION['q2'])) {
echo "q2 is empty!";
}
if(empty($_SESSION['q3'])) {
echo "q3 is empty!";
}
if(empty($_SESSION['q4'])) {
echo "q4 is empty!";
}
if(empty($_SESSION['q5'])) {
echo "q5 is empty!";
}
找出问题所在的字段。
答案 1 :(得分:1)
最有可能某些字段为空。 在提交表单后,通过显示所有表单字段来检查是否已填写所有表单字段。
只需在PHP代码的顶部添加var_dump($_POST);exit;
并发布结果。