J. DKYSR J. DKYSR - 6 months ago 19
PHP Question

Check if one of the 2 input fields is filled in

EDIT:

This seems to work

if ( empty($_POST['currentamount']) && empty($_POST['nextamount']) )
{ $error_msg ="- You didn't order any tickets.<br>"; }





EDIT:

This doesn't work :(

if(strlen($_POST['currentamount']) OR strlen($_POST['nextamount']) == 0)
{ $error_msg ="- You didn't order any tickets.<br>"; }





I have a contact form where people can order tickets, either for the next show or the current.
Now I want to make sure they fill in atleast one show date.

Contact form





  • How can I send them an error if they didn't fill in atleast one of the two 'amount' fields?

  • Is it possible that if they fill in one 'amount' field it NEEDS to have a selected date aswell?






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
}
?>





This code doesn't need to be there anymore, since it's auto filled in by the server.

if(strlen($_POST['name2']) == 0)
{ $error_msg ="- Please, provide us with your name.<br>"; }

Answer

You can check for your required conditions at the beginning of handling your form submission. It is convenient to store any errors in an array. If there are multiple problems encountered during form validation, it may provide a better experience to your user to be presented with all of them at once, rather than fixing one thing, submitting the form again and encountering a different error.

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.';
}

You can add more conditions to validate (proper dates, etc.) as you see fit. After checking all of your conditions, check the $errors variable to see if any errors were encountered. If it's empty, you can proceed with processing the submitted data, and if not, you can use the values in the error array to prompt the user in the specific place on your form where the error occurred.