Margate
Margate

Reputation: 421

PHP POST validation error removes all entries

I am trying to build a form and have run into some difficulty. When the form is posted if there is a validation error all of the entered data is removed! Is there any way to keep the date in the form? I am also trying to send the form data to a page named contact-engine.php I can send it by changing the form action to action="contact-engine.php" but then my validation does not work! I'm basically stuck and have run out of ideas after several hours of trying to figure this out. Code is as follows:

<?php
// define variables and set to empty values
$addDateErr = $nameErr = $emailErr = $subjectErr = $messageErr = $questionErr = "";
    $addDate = $name = $email = $subject = $message = $question = "";

 if ($_SERVER["REQUEST_METHOD"] == "POST")
 {

 //Date
   if (empty($_POST["addDate"]))
   {$comment = "";}
   else
   {$comment = test_input($_POST["addDate"]);}

 //Name
    if (empty($_POST["name"]))
    {$nameErr = "Name is required";}
    else
    {
    $name = test_input($_POST["name"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$name))
    {
    $nameErr = "Only letters and white space allowed";
    }
    }

//Email
if (empty($_POST["email"]))
{$emailErr = "Email is required";}
else
{
$email = test_input($_POST["email"]);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr = "Invalid email format";
}
}

//Subject
if (empty($_POST["subject"]))
{$comment = "";}
else
{$comment = test_input($_POST["subject"]);}

//Message
if (empty($_POST["message"]))
{$messageErr = "A message is required";}
else
{$comment = test_input($_POST["message"]);}

//Question
if (empty($_POST["question"]))
{$comment = "";}
else
{$comment = test_input($_POST["question"]);}

}
function test_input($data)
{
 $data = trim($data);
 $data = stripslashes($data);
 $data = htmlspecialchars($data);
 return $data;
}
?>



<form method="post" id="form" action="
<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p><span class="error">* required field.</span></p><br />

<!--Date--><div class="contact-font">
    Date Photography needed (if necessary)<br />
    <input type="text" name="addDate" id="datepicker" size="25" value="
<?php echo $addDate;?>">
    </div>

<!--Name--><div class="contact-font" style=" margin-top: 20px;">
    <span class="asterix">* </span>Name:<br />
    <input type="text" name="name" class="border" size="25"><span class="error">
<?php echo $nameErr;?></span>
</div>

<!--Email--><div class="contact-font" style=" margin-top: 20px;">
    <span class="asterix">* </span>Email:<br />
    <input type="text" name="email" class="border" size="25">
<span class="error"><?php echo $emailErr;?></span>
</div>

<!--Subject--><div class="contact-font" style=" margin-top: 20px;">
    Subject:<br />
    <input type="text" name="subject" class="border" size="25"<?php echo $subject;?>">
</div>

    <!--Message--><div class="contact-font" style=" margin-top: 20px;">
    <span class="asterix">* </span>Message:<br />
    <textarea cols="40" rows="10" name="message" class="border"></textarea>
<span class="error"><?php echo $messageErr;?></span>
</div><br />

<p id="info-req">How did you find about Darren Morton Photography?</p>
<!--Question--><select id="marketing" name="question"<?php echo $question;?>">
    <option value="----------" style="color:black">----------</option>
    <option value="Web Search" style="color:black">Google Search</option>
    <option value="Web Search" style="color:black">Social Media</option>
    <option value="Word of mouth" style="color:black">Word of mouth</option>
    <option value="Other" style="color:black">Other</option>
</select><br /><br />

<div>
    <input type="submit" value="Send" id="submit">
</div>
</form>

Upvotes: 3

Views: 229

Answers (2)

Panama Jack
Panama Jack

Reputation: 24448

You should be able to do something like this. Leave the from action as you had before not your contact-engine.php file. Then see if the $_POST is set and then it will fill the input with the recent value. Example

<input type="text" name="name" class="border" size="25" value="<?php if(isset($_POST['name'])) { echo $_POST['name']; } ?>"><span class="error">
<?php echo $nameErr;?></span>

Then you can do that for your other inputs.

Upvotes: 3

Paul Facklam
Paul Facklam

Reputation: 1633

You can use sessions for both problems. Just google for it.

Upvotes: 3

Related Questions