Jump to content

[SOLVED] Applying form confirmation message - my way is a screw up!


iconicCreator

Recommended Posts

Okay, still taking baby steps with PHP, this forum has been an enormous help and resource and I wanna thank everyone!  ;D

 

Now, I have created a form, the problem is I cannot get the confirmation message to appear after the user has submitted the form.

 

This is the problem:

 

When I put the PHP code and the validation message in a separate PHP page it works perfectly, the problem with this method is I do-not know how to add the validation when the form script is in a separate page.

 

But when I add the script and the XHTML code on the same page, I can add the validation but the confirmation message appears when the form loads in the browser with the form - before the user has even submitted it.

 

How can I add confirmation message to this form after the user has submitted it?

 

Complete code without confirmation page.

 

Any help or ideas will be appreciated.

 

<?php
$post = array("firstName" => "","lastName" => "","Email" => "","phoneNumber" => "","comments" => "");
$errors = $post;
if (isset($_POST["send"]))
{
  foreach ($post as $k => $v)
  $post[$k] = stripslashes($_POST[$k]);

  if (empty($post["firstName"]))
    $errors["firstName"] = "**Required!";

  if (empty($post["last"]))
    $errors["lastName"] = "**Required!";

  if (empty($post["email"]))
    $errors["Email"] = "**Required!";
  else
  {
    if (!preg_match('/[^@]+@[^.]+(\.[^.]+)+/', $post["Email"]))
    {
      $errors["Email"] = "Invalid Email!";
    }
  }

  if (empty($post["phoneNumber"]))
    $errors["phoneNumber"] = "**Required!";
  else
  {
    if (!preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/', $post["phoneNumber"]))
    {
      $errors["phoneNumber"] = "**Invalid!";
    }
  }
    
  if (empty($post["comments"]))
  $errors["comments"] = "**Required!";

  $error = false;
  foreach($errors as $k => $v)
  {
    if ($errors[$k] != "")
  $error = true;
  }

  if (!$error)
  {
    $address = "[email protected]";
    $subject = "New Form Details Entered!";

    $message = "You have recieved new information via your website form. The details entered are as follows:<br><br>";
    $message .= "<b>First Name:</b> ".$post['first']."<br>";
    $message .= "<b>Last Name:</b> ".$post['last']."<br>";
    $message .= "<b>Email:</b> ".$post['email']."<br>";
    $message .= "<b>Phone Number:</b> ".$post['phone']."<br>";
    $message .= "<b>DOB:</b> ".$post['month']." ".$post['day']." ".$post['year']."<br><br>";
    
    $message .= "<b>Like Colors:</b> ".$post['love']."<br>";
    $message .= "<b>Favorite Colors:</b> ".$post['favorite']."<br><br>";
    
    $message .= "<b>Favorite Genre:</b> ".$post['genre']."<br><br>";
    
    $message .= "<b>Comments/Message:</b> ".$post['comments']."<br>";

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  $headers .= 'From: '.$post['firstName']." ".$post['lastName'].'<[email protected]>' . "\r\n";
  
    mail($address, $subject, $message, $headers);

  }
}
?>

 

This the confirmation I tried to add but it's not working and appears in the browser when the page loads.

 

<!--CONFIRMATION MESSAGE-->
<?php
$confirmation = "Thanks <span class=\"sendersName\">$firstName</span> for your message, we will contact within 24 hours.";
?>
<div class="confirmation_message">
<?php echo $confirmation; ?>
</div>
<!--END CONFIRMATION MESSAGE-->

 

 

put the confirmation message in a if block

ie

<?php
if (isset($_POST["send"]))
{
?>
<!--CONFIRMATION MESSAGE-->
<?php
$confirmation = "Thanks <span class=\"sendersName\">$firstName</span> for your message, we will contact within 24 hours.";
?>
<div class="confirmation_message">
<?php echo $confirmation; ?>
</div>
<!--END CONFIRMATION MESSAGE-->
<?php } ?>

put the confirmation message in a if block

ie

<?php
if (isset($_POST["send"]))
{
?>
<!--CONFIRMATION MESSAGE-->
<?php
$confirmation = "Thanks <span class=\"sendersName\">$firstName</span> for your message, we will contact within 24 hours.";
?>
<div class="confirmation_message">
<?php echo $confirmation; ?>
</div>
<!--END CONFIRMATION MESSAGE-->
<?php } ?>

 

Hey thanks for the help, however, the confirmation message is wrapped in a div tag and so where do I insert this?

Because since the div tags are XHTML, they show up in the form anyway.

 

Just confused.

 

It works when I have the script and the form in separate pages but then I do not know how to apply the validation with the form and the script being in separate pages.

 

IC

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.