Reputation: 182
I am pretty new to PHP and I can't seem to find what I am looking for. I want the error to show on the page where the form is filled out. I want a "Please enter name" error to show if it's blank in the $showerror part of the form page.
This is what I have so far...
<form method="post" action="process.php">
<table width="667">
<td colspan="2"><?php echo $showerror; ?></td>
<tr>
<td>Full Name:*</td>
<td><input class="text" name="name" placeholder="Ex. John Smith"></td>
<tr>
<td>E-mail:*</td>
<td><input class="text" name="email" placeholder="Ex. [email protected]"></td>
<tr>
<td>Type of site:*</td>
<td>Business<input name="multioption[]" type="radio" value="Business" class="check" /> Portfolio<input name="multioption[]" type="radio" value="Portfolio" class="check" /> Forum<input name="multioption[]" type="radio" value="Forum" class="check" /> Blog<input name="multioption[]" type="radio" value="Blog" class="check" /></td>
<tr>
<td>Anti-Spam: (2)+2=?*</td>
<td><input name="human" placeholder="What is it?"></td>
</table>
<input id="submit" name="submit" type="submit" value="Submit"></form>
Then this is what my process.php looks like... I am not sure how to code the error part.
<?php
$name = isset($_POST['name']) ? $_POST['name'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$human = isset($_POST['human']) ? $_POST['human'] : '';
$submit = isset($_POST['submit']) ? true : false;
$multioption = isset($_POST['multioption'])
? implode(', ', $_POST['multioption'])
: 'No multioption option selected.';
$from = 'From: Testing Form';
$to = '[email protected]';
$subject = 'Testing Form';
$body =
"Name: $name\n
E-Mail: $email\n
Multi Options: $multioption\n";
if ($submit && $human == '4') {
mail ($to, $subject, $body, $from);
print ("Thank you. We have received your inquiry.");
}
else {
echo "We have detected you are a robot!.";
}
?>
Upvotes: 0
Views: 178
Reputation: 20473
You need to place PHP syntax between PHP tags, for example, this line:
<td colspan="2">$showerror</td>
becomes like this:
<td colspan="2"><?php echo $showerror; ?></td>
If you're completely new to PHP, you can start learning from some tutorial websites, here's a good one
EDIT:
You can set $showerror in the PHP page, this could be a long list of "if conditions" for each form field, but I will show you a small/simple example for the full-name $_POST['name']
, it will be like this:
$showerror = '';
if(!empty($_POST['name'])) {// enter here if fullname is set
if(strlen($_POST['name']) >= 6 && strlen($_POST['name']) <= 12) {// enter here if fullname length is between 6-12 characters
// You can do more validation by using "if conditions" here if you would like, or keep it empty if you think fullname is correct
} else {// enter here if fullname is NOT between 6-12 characters
$showerror = 'Full name must be 6-12 characters';
}
} else {// enter here if fullname is not set
$showerror = 'Please enter your full name';
}
Upvotes: 2
Reputation: 428
Try this.. you have to put both the code in same file. and can check if the request is post the only read the php. and then you can put the value in $sho
Upvotes: 0
Reputation: 74
Try this,
<?php
if(isset($showerror))
echo $showerror;
else
echo '';
?>
You will get an error message Undefined variable
if you do not have a code in validating the $showerror
variable on the first compilation of the page
Upvotes: 0
Reputation: 194
compiler only parse content as php which is written between <?php ?>
tag so use
<td colspan="2"><?php echo $showerror; ?></td>
or
<td colspan="2"><?= $showerror ?></td>
Upvotes: 2