Reputation: 95
So I'm trying to validate an Address form field and a P.O. Box form field. So far I have this:
if (empty($_POST["po"]) && empty($_POST["address"]))
{
$poErr = "Please enter a P.O. Number OR address";
$addressErr = "Please enter a P.O. Number OR address";
if (!preg_match("/[0-9]{1,}$/",$po))
{
$po = test_input($_POST["po"]);
$poErr = "Please enter only a number for P.O. Box.";
}
if (!preg_match("([0-9]{1,} [\s\S]*?)",$address))
{
$address = test_input($_POST["address"]);
$addressErr = "Address must be only letters, numbers or one of the following _ - . , : ' \"";
}
}
The test_input
just checks special chars.
What I want to do is see if P.O. Box and Address is empty, if it is, then I will tell it to display an error into html to enter a value for one field or the other, which turns out fine. What I want to do after that is to determine which one is filled, and then verify one and/or both fields if one, or both happen to be filled.
Here is my HTML where I want the "po" and "address" values to remain if there were to be another error on the form:
<div class="form-group">
<label class="control-label" for="address">Your Mailing Address **</label>
<input class="form-control" type="text" name="address" id="address" placeholder="Your Mailing Address" value="<?php echo $address; ?>">
<br /><span class="error"><?php echo $addressErr; ?></span>
</div><!--form group address-->
<div class="form-group">
<label class="control-label" for="po">Your P.O. Box (Number only) **</label>
<input class="form-control" type="text" name="po" id="po" placeholder="#" value="<?php echo $po; ?>">
<br /><span class="error"><?php echo $poErr; ?></span>
</div><!--form group address-->
Upvotes: 1
Views: 119
Reputation: 7776
Your condition is wrong. if both fields empty then you write check for individual fields.
if both fields empty then it will goes inside that condition. and also you want to keep value if error in form then change $po
to $_POST['po']
and $address
to $_POST['address']
$poErr = $addressErr = '';
if (empty($_POST["po"]) && empty($_POST["address"])) {
$poErr = "Please enter a P.O. Number OR address";
$addressErr = "Please enter a P.O. Number OR address";
} else {
if (!empty($_POST["po"]) && !preg_match("/[0-9]{1,}$/", $_POST["po"])) {
$po = test_input($_POST["po"]);
$poErr = "Please enter only a number for P.O. Box.";
}
if (!empty($_POST["address"]) && !preg_match("([0-9]{1,} [\s\S]*?)", $_POST["address"])) {
$address = test_input($_POST["address"]);
$addressErr = "Address must be only letters, numbers or one of the following _ - . , : ' \"";
}
}
After form submission you want keep value in form , please change below fields:-
<input class="form-control" type="text" name="address" id="address" placeholder="Your Mailing Address" value="<?php echo (isset($_POST["address"])) ? $_POST["address"] : ''; ?>">
<input class="form-control" type="text" name="po" id="po" placeholder="#" value="<?php echo (isset($_POST["po"])) ? $_POST["po"] : ''; ?>">
Upvotes: 0
Reputation: 3329
if both fields are empty it will be in first condition and if any of the fields is not empty it will be handled in else part like below.
if (empty($_POST["po"]) && empty($_POST["address"]))
{
$poErr = "Please enter a P.O. Number OR address";
$addressErr = "Please enter a P.O. Number OR address";
}else {
if (!preg_match("/[0-9]{1,}$/",$_POST["po"]))
{
$po = test_input($_POST["po"]);
$poErr = "Please enter only a number for P.O. Box.";
}
if (!preg_match("([0-9]{1,} [\s\S]*?)",$_POST["address"]))
{
$address = test_input($_POST["address"]);
$addressErr = "Address must be only letters, numbers or one of the following _ - . , : ' \"";
}
}
Upvotes: 1
Reputation: 81
you need to user
if($_POST["po"]=='')
this might help you
and you can also change the structure like
if ($_POST['po']=='' && $_POST['address']=='')
{
echo $poErr = "Please enter a P.O. Number OR address";
echo $addressErr = "Please enter a P.O. Number OR address";
}
else
{
if (!preg_match("/[0-9]{1,}$/",$po))
{
echo $po = test_input($_POST["po"]);
echo $poErr = "Please enter only a number for P.O. Box.";
}
if (!preg_match("([0-9]{1,} [\s\S]*?)",$address))
{
echo $address = test_input($_POST["address"]);
echo $addressErr = "Address must be only letters, numbers or one of the following _ - . , : ' \"";
}
}
you used &&
that means if both are empty then and then condition work. or else you need to use ||
. means if any one is empty then it works
Upvotes: 0