编辑:
这似乎有效
if ( empty($_POST['currentamount']) && empty($_POST['nextamount']) )
{ $error_msg ="- You didn't order any tickets.<br>"; }
编辑:
这不起作用:(
if(strlen($_POST['currentamount']) OR strlen($_POST['nextamount']) == 0)
{ $error_msg ="- You didn't order any tickets.<br>"; }
我有一个联系表格,人们可以订购门票,无论是下一个节目还是现在。 现在我想确保他们至少填写一个节目日期。
Contact.php
<?
if($_SERVER['REQUEST_METHOD']=="POST")
{
if(strlen($_POST['name2']) == 0)
{ $error_msg ="- Please, provide us with your name.<br>"; }
if(!empty($error_msg))
{
//Een van de velden werd niet goed ingevuld
echo "<b>Your message can't be send due to the following reason:</b> <br><br>";
echo $error_msg;
echo "<br>Click on <a href='javascript:history.back(1)'>Go back</a> and provide us with your name.<br><br>";
}
else
{
$recipient = "************"; //Het email adres van de persoon die vragen moet ontvangen.
$subject = "**********"; //Subject van de mail.
$header = "From: " . $_POST['uwemail'] . "\n";
$mail_body = "Contact script werd op " . date("d-m-Y") . " om " . date("H:i") . " uur uitgevoerd.\n";
*****MORE email text********
mail($recipient, $subject, $mail_body, $header);
print "<b>IMPORTANT!</b>";
*****MORE print text********
print "<br><br>We are looking forward to meet you.";
}
{
$recip = $_SESSION['email']; //Het email adres van de persoon die vragen moet ontvangen.
$subjec = "******"; //Subject van de mail.
$head = "From: " . $_POST['uwemail'] . "\n";
$mail_body2 = "Dear". $_POST['name2'] . "\n";
mail($recip, $subjec, $mail_body2, $head);
}
}
else
{
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST" name="contact">
<div class="row uniform 50%">
<div class="6u 12u(mobilep)">
Your personal card number
<input type="text" name="card2" id="card" value="<?php echo $_SESSION['username']; ?>" placeholder="Card Number" readonly/>
</div>
<div class="6u 12u(mobilep)">
<input type="text" name="name2" id="name" value="<?php echo $_SESSION['realName']; ?>" placeholder="Your name" readonly/>
</div>
</div>
<div class="row uniform 50%">
<div class="6u 12u(narrower)">
Order tickets for our current show: Smile
</div>
</div>
<div class="row uniform 50%">
<div class="4u 12u(narrower)">
<input type="text" name="currentshow" id="currentshow" value="Current show: Smile" placeholder="" readonly/>
</div>
<div class="4u 12u(narrower)">
<select name="date1">
<option>Select a date</option>
<option>02 Octobre 2016 - 20h</option>
<option>03 Octobre 2016 - 20h</option>
<option>04 Octobre 2016 - 20h</option>
</select>
</div>
<div class="2u 12u(narrower)">
<input type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' name="currentamount" id="currentamount" value="" placeholder="Amount" />
</div>
</div>
<div class="row uniform 50%">
<div class="6u 12u(narrower)">
Order tickets for our next show: Sand
</div>
</div>
<div class="row uniform 50%">
<div class="4u 12u(narrower)">
<input type="text" name="nextshow" id="nextshow" value="Next show: Sand" placeholder="" readonly/>
</div>
<div class="4u 12u(narrower)">
<select name="date2">
<option>Select a date</option>
<option>20 Octobre 2016 - 20h</option>
<option>21 Octobre 2016 - 20h</option>
<option>23 Octobre 2016 - 20h</option>
</select>
</div>
<div class="2u 12u(narrower)">
<input type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' name="nextamount" id="nextamount" value="" placeholder="Amount" />
</div>
</div>
<div class="row uniform 50%">
<div class="6u 12u(mobilep)">
<input type="hidden" id="currentprice" value="10" />
</div>
<div class="6u 12u(mobilep)">
<input type="hidden" id="nextprice1" value="10" placeholder="" />
</div>
</div>
<div class="6u 12u(mobile)">
<input name="uwemail" placeholder="Email" type="hidden" value="noreply@mazzinitheatreproductions.com"/>
</div>
<div class="6u 12u(mobilep)">
Total price.(In EUR)
<input type="text" name="total2" id="total" value="" readonly/>
</div>
</div>
<div class="row uniform">
<div class="12u">
<ul class="actions align-center">
<li><input type="submit" name="submit"value="Place Order"/></li>
</ul>
</div>
</div>
</form>
<?php
}
?>
此代码不再需要存在,因为它已由服务器自动填写。
if(strlen($_POST['name2']) == 0)
{ $error_msg ="- Please, provide us with your name.<br>"; }
答案 0 :(得分:1)
首先,您要检查日期字段是否为空,然后您需要确保日期格式正确(xx / xx / xxxx),然后您需要验证日期以确保它是正确的日期
像这样:
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if ($_POST['date'] == '') {
$error_msg = 'Field is empty';
} else {
list($dd, $mm, $yy) = explode('/', $_POST['date']);
$start = $yy . '-' . $mm . '-' . $dd . ' 00:00:00';
if ($mm == '' || $dd == '' || $yy == '') {
$error_msg = 'Date is not in a valid format';
} else {
if (!checkdate($mm, $dd, $yy)) {
$error_msg = 'Date is not a valid date';
}
}
}
if (!empty($error_msg)) {
// Continue
}
}
答案 1 :(得分:1)
您可以在处理表单提交之初检查您所需的条件。将任何错误存储在数组中很方便。如果在表单验证过程中遇到多个问题,它可能会为您的用户提供更好的体验,让您一次性呈现所有这些,而不是修复一件事,再次提交表单并遇到不同的错误。
if (!$_POST['date1'] && !$_POST['date2']) {
$errors['form'] = 'You must select at least one show date.';
}
if ($_POST['currentamount'] && !$_POST['date1']) {
$errors['current'] = 'You must select a date.';
}
if ($_POST['nextamount'] && !$_POST['date2']) {
$errors['next'] = 'You must select a date.';
}
您可以根据需要添加更多条件来验证(正确的日期等)。检查完所有条件后,检查$errors
变量以查看是否遇到任何错误。如果它为空,则可以继续处理提交的数据,如果没有,则可以使用错误数组中的值来提示用户在表单上发生错误的特定位置。