0

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!.";
    }
?>
3
  • Are you sure you're not looking for a Javascript solution? Commented Jun 24, 2013 at 9:15
  • this $showerror is filled when and where? Commented Jun 24, 2013 at 9:16
  • Is javascript easier and more efficient? Commented Jun 25, 2013 at 2:25

4 Answers 4

2

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';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Whether or not I enter or do not enter a "name" in the form it just says "Thank you. We have received your inquiry." as long as I enter the anti-spam
@user2515606 Seems like you're pretty new to PHP. Kind of hard to explain every bit of code in few comments. I suggest you start learning PHP from this website: link
2

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>

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.