SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Aug 2008
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

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

    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.

    Code:
    <?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 = "test@test.com";
        $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'].'<test@test.com>' . "\r\n";
      
        mail($address, $subject, $message, $headers);
    
      }
    }
    ?>
    This is the confirmation I tried to add but it's not working and appears in the browser when the page loads.

    Code:
    <!--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-->

  2. #2
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Shaw AFB
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ~ Nate L ~

  3. #3
    SitePoint Addict
    Join Date
    Aug 2008
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by NateL View Post
    Looks very interesting - I'll give it a shot!

    Thanks!

    IC

  4. #4
    Non-Member adstiger's Avatar
    Join Date
    Nov 2008
    Location
    Canada
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    create a new html page for confirmation. Redirect to the confirmation page using header.

  5. #5
    SitePoint Addict
    Join Date
    Aug 2008
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by adstiger View Post
    create a new html page for confirmation. Redirect to the confirmation page using header.
    Thanks for the help.
    I did that in the past but now I would like the user or sender's first name to also appear in the form when submitted.

    IC

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Theres a section of your code that gets executed when the form is submitted, and no errors are present(hint, you "send" something). This is a good spot to put the code that sais thank you.

  7. #7
    Always learning viveknarula's Avatar
    Join Date
    Mar 2006
    Location
    INDIA
    Posts
    418
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can put the action attribute of your form to the same where both the form and validations are. However you need to put the validations on the top of the page under some condition something like
    PHP Code:
    ($_POST['submit']=="submit"
    etc, after validating the errors etc. you need to check the error array whether it is empty or not. something like
    PHP Code:
    if(sizeof($error)==0)
    {
    echo 
    "Thanks........";  
    }
     else
     {
    show error messages.   

    i hope this helps.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •