1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Retain email form (with errors) after submit

Discussion in 'PHP' started by ian_ok, Feb 10, 2005.

  1. #1
    I've got a contact us form on a generated php page, so if the user completes and does mistakes it will advise on the next page...(this works fine)

    BUT, when I ask them to click back the data is gone. (I understand this)

    So how do I get it so when the next page is shown, it says "Form submitted, thank you" OR it shows the form again with the errors to correct and of course stays on this page until the serrors are corrected.

    Thanks,

    Ian
     
    ian_ok, Feb 10, 2005 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    Set up your form to set the value tag of your fields to be the variable you are passing them as. That way if they aren't set, they will be blank, if they are set then it will display it as the default.
     
    digitalpoint, Feb 10, 2005 IP
  3. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    OK, I'm cooking with gas (It's working - but only 90%)!!

    My validation isn't working 100%, it only check 1 and not both....I think I've messed up my IF's

    Anyone help?

    Ian

    <?php 
    
    ini_set ('sendmail_from', "myemail@domain.com");
    $email = 'myemail@domain.com';
    $subject = $HTTP_POST_VARS['subject'];
    $message = $HTTP_POST_VARS['message'];
    $from = $HTTP_POST_VARS['from']; //persons email address
    $name = $HTTP_POST_VARS['name'];
    $from_check = $HTTP_POST_VARS['from_check'];
    
      $message = "Additional Requests:   ".$message;
      $message .= "\nName:   ".$name;
      $message .= "\nEmail address 2 (could be different):   ".$from_check;
    
    if  (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $from))
    {
    echo "Your email address is not valid";
    
    if ($from <> $from_check)
    echo "different emails…try again";
    
    ?>
    <form name="form" method="post" action="http://www.domain.com/mail/1.php">
    MY FORM IS HERE ALL WORKING FINE
    </form>
    <?php
    }
    elseif
     (mail($email, $subject, $from, $message)){
      echo $name;
      echo "Thank you";
      
         }
    
    else {
      echo "Sorry we have a problem, try again or send an email to: email@domain.com";
      echo "Thank you";
    
    }
    
    ?>
    
     
    Code (markup):
     
    ian_ok, Feb 11, 2005 IP
  4. neterslandreau

    neterslandreau Peon

    Messages:
    279
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I think the problem is the bare html in the middle of the if-else statement.

    I rewrote it a little.. See if you can use this:

    
    <?php 
    
    ini_set ('sendmail_from', "myemail@domain.com");
    $form = '<form name="form" method="post" action="http://www.domain.com/mail/1.php">MY FORM IS HERE ALL WORKING FINE</form>';
    $email = 'myemail@domain.com';
    $subject = $HTTP_POST_VARS['subject'];
    $message = $HTTP_POST_VARS['message'];
    $from = $HTTP_POST_VARS['from']; //persons email address
    $name = $HTTP_POST_VARS['name'];
    $from_check = $HTTP_POST_VARS['from_check'];
    
    $message = "Additional Requests:   ".$message;
    $message .= "\nName:   ".$name;
    $message .= "\nEmail address 2 (could be different):   ".$from_check;
    
    if  (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $from)) {
      echo "Your email address is not valid";
    }
    
    if ($from <> $from_check) {
        echo "different emails! Try again";
    }
    elseif {
      (mail($email, $subject, $from, $message));
      echo $name;
      echo "Thank you";
      
    }
    else {
      echo "Sorry we have a problem, try again or send an email to: email@domain.com";
      echo "Thank you";
    
    }
    
    ?>
    
    Code (markup):
    (You can echo $form where you want your form to appear.)
     
    neterslandreau, Feb 11, 2005 IP
  5. rvarcher

    rvarcher Peon

    Messages:
    69
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Here's the logic I use with forms and validations.

    Validation and forms are in the same file (or included).

    Validation failed errors are assigned to an $errors array.

    if ( user submitted form )

    validate input

    if ( !valid email1) $errors[] = invalid email1
    if ( !valid email2) $errors[] = invalid email2
    if ( email1 != email2 ) $errors[] = emails do not match.

    repeat for password, address, zip, etc.

    all input you accept from a user should be validated by you.

    if ( no errors ) do whatever you want. email, insert database.

    end if

    if ( errors exist or user has not submitted form )

    if ( errors exist ) display $errors array. e.g. echo "there were errors:<br> " . join("<br /> - ",$errors);

    display form.

    and in your <input> tags set the value to $_REQUEST[email1], $_REQUEST[city], etc. like Shawn suggested.

    end if
     
    rvarcher, Feb 12, 2005 IP
  6. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks everyone......

    ...I've managed to get the form working.

    Ian
     
    ian_ok, Feb 12, 2005 IP