SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Validating Simple PHP Contact Form causes to wipe fields when error with fields?

    I have form that once you have pressed the submit button will validate 3 * fields to check if they have been entered in correctly. If the have errors it will not process the form to email and instead display the errors and tell the user to amend the error fields before it can be processed.

    My problem is that i have tested the validation and works to a certain standard my only PROBLEM is that if there is an error in the form it wipes the whole form clear so ultimately the user will lose everything they have entered.

    I do not want this happening, much like other forms on websites i have used it still displays the form as you entered it and so you can ammend the error fields and start again making sure everything is correct and processed properly.

    Can someone check over my code to see the exact cause to why it clears and wipes the form after validating an error, thanks

    code

    PHP Code:
    <?php

    if ($_POST['submita'] == "Submit")
      {
      
    $valid_form true;
      
    if (
    $_POST['name'] == "")
            {
                echo 
    "Enter your name<br>";
                
    $valid_form false;
            }
            
    else
            {
                
    $name $_POST['name'];
            }
            
            
            
    if (
    $_POST['email'] == "")
            {
                echo 
    "Enter your email<br>";
                
    $valid_form false;
            }

    elseif (!
    strstr($_POST['email'], '@'))
            {
                
                echo 
    "Enter a valid email address<br>";
                
    $valid_form false;
            }
                    
    else
            {
                
    $name $_POST['email'];
            }

    if (
    $_POST['message'] == "")
            {
                echo 
    "Enter your Message";
                
    $valid_form false;
            }
            
    else
            {
                
    $name $_POST['message'];
            }







      

    if(
    $valid_form == true)
            {
            
        
    $msg .= "<b>Name:</b>                                $_POST[name]<br>";
        
    $msg .= "<b>Business / Organisation:</b>            $_POST[organisation]<br>";
        
    $msg .= "<b>Email Address:</b>                            $_POST[email]<br>";
        
    $msg .= "<b>Phone Number:</b>                            $_POST[phone]<br><br>";
        
    $msg .= "<b>Message:</b><br>                            $_POST[message]";
            
        
        
    $recipient "email@gmail.com";
        
    $subject "Online Contact Query";
        
    $from $_POST[email];
        
    $headers  "MIME-Version: 1.0\r\n";
        
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
        
    $headers .= "Content-Transfer-Encoding: 8bit\r\n";
        
    $headers .= "From: " $from "\r\n";
        
    $mailsent mail($recipient$subject$msg$headers);
        
        if (
    $mailsent) {
        echo 
    "<center>";
        echo 
    "<img src=\"images/logo.gif\"><br>";
        echo 
    "<h1>Congratulations Your Message Has Been Sent<br>";
        echo 
    "$msg";
        echo 
    "</center>";
        
        } else {
        
        echo 
    "There was an error.. Please go back and try again, Thanks.";
        }
        
            
            }
    }

    ?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <fieldset>

    <legend>Contact Information</legend>
      <p>

                            <label for="msg_name"><strong>*</strong> Your Name:</label>
                            <input class="fullwidth" name="name" size="40" type="text" />

                        </p>
                        <p>
                            <label for="msg_organisation">Your Business / Organisation:</label>
                            <input class="fullwidth" name="organisation" size="40" type="text" />
                        </p>
                        <p>
                            <label for="msg_email"><strong>*</strong> Email Address:</label>
                            <input class="fullwidth" name="email" size="40" type="text" />

                        </p>
                         <p>
                            <label for="msg_phone">Phone Number:</label>
                            <input class="fullwidth" name="phone" size="40" type="text" />
                        </p>
                    </fieldset>
                    <fieldset>
                        <legend>Message</legend>
                        
                        <p>
                            <label for="msg_message"><strong>*</strong> Your Query</label>
                            <textarea cols="60" name="message" type="text" rows="10"></textarea>
                                                </p>
                        
                        </fieldset>
      <input type="submit" name="submita" value="Submit"/>
                    </form>
    Last edited by joe_thomas; May 14, 2007 at 12:20.

  2. #2
    SitePoint Zealot mwasif's Avatar
    Join Date
    Apr 2007
    Location
    Pakistan
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks i have been trying out some new methods and nearly have it working

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anybody help my new problem?

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ? help anyone

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Evening Joe,

    what you need to do is to echo the POST'ed values back to the form inputs
    PHP Code:
    <input class="fullwidth" name="email" size="40" type="text" value="<?php if(isset($_POST['email'])) { echo $_POST['email']; } ?>" />
    So for all the other inputs you need to do the same.
    Hope that makes sense!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ View Post
    Evening Joe,

    what you need to do is to echo the POST'ed values back to the form inputs
    PHP Code:
    <input class="fullwidth" name="email" size="40" type="text" value="<?php if(isset($_POST['email'])) { echo $_POST['email']; } ?>" />
    So for all the other inputs you need to do the same.
    Hope that makes sense!

    You are a lifesaver it works perfectly now, many thanks sir

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    No worries, hadn't seen the thread as it had made it's way down the page or I would have answered earlier.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


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
  •