www.webdeveloper.com
Results 1 to 7 of 7

Thread: Is this code correct?

  1. #1
    Join Date
    Sep 2009
    Posts
    16

    Is this code correct?

    I have a contact form I used for a long time but now need to add a honeypot.
    The form collects Name, email, phone, Street1, Street2, city, zip and comment.

    The form has field validation. This is the validation code:

    Code:
    // validation
    $validationOK=true;
    if (Trim($EmailFrom)=="") $validationOK=false;
    if (Trim($Name)=="") $validationOK=false;
    if (Trim($PostCode)=="") $validationOK=false;
    if (!$validationOK) {
    header('Location: error-message.html');
    exit();
    }
    I have added another field, Street3, hidden by css.
    I want to validate this field and,if it is filled, redirect to bademail@mydomain.com

    I set a local variable $EmailBad = "bademail@mydomain.com"
    Needless to say, I am PHP illiterate but this is what I think the code should be.

    Code:
    $validationOK=false;
    if (Trim($Street3)=="") $validationOK=true;
    if (!$validationOK) {
    $success = mail($EmailBad, $Subject, $Body, "From: <$EmailFrom>");
    else;
    
    $validationOK=true;
    if (Trim($EmailFrom)=="") $validationOK=false;
    if (Trim($Name)=="") $validationOK=false;
    if (Trim($PostCode)=="") $validationOK=false;
    if (!$validationOK) {
    header('Location: error-message.html');
    exit();
    }
    I am probably way off track so any help is appreciated.

    thanks.

    If anyone is wondering, I am setting the honeypot as a "legitimate" field and I have never had anyone fill out Street2, so it is very unlikely the will use Street3.
    I want it to go to an email address so spam programs will assume their work is done, I can monitor the success and also check if real requests slipped through (screen readers etc.)

  2. #2
    Join Date
    Sep 2009
    Posts
    16
    Perhaps, if I re-phrase this question you will understand what I am trying to do.

    I have a form where some fields are validated (name, email etc.)
    If they are empty, a warning asks to fill the field
    If all is correct, the form is sent.

    All straight forward stuff.

    I want to add a field, that needs to validate as empty for the form to be sent.
    If the field is filled then the form is sent to a different account.

    or to put it another way,
    if all fields INCLUDING field X are filled, go here
    else
    if all fields EXCLUDING field X are filled, go there.

    Help please.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,157
    PHP Code:
    if(trim($field) !== '') {
      
    // data was entered, so output an error or whatever

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Sep 2009
    Posts
    16
    Thanks NogDog,
    So, my code will be

    if(trim($Street3) !== '') {
    $success = mail($EmailBad, $Subject, $Body, "From: <$EmailFrom>");

    Now, I'm thinking this validation will need to go before my other validation so do I need to add

    else;
    { rest of validation
    exit();
    }

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,157
    It kind of comes down to personal style (or coding standards being used). Some people like to do the "go" path first, so it might be something like:
    PHP Code:
    if(trim($Street3) === '') {
       if(
    trim($EmailFrom) !== "" and trim($Name) !== "" and trim($PostCode) !== "") {
          
    // good to go, so process things normally here
       
    }
       else {
          
    // handle error condition here for missing required field(s)
       
    }
    }
    else {
       
    // handle case when street3 is entered

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Sep 2009
    Posts
    16
    I'll go with this
    I appreciate your help NogDog.

    Thank you.

  7. #7
    Join Date
    Sep 2009
    Posts
    16
    Just noticed,
    on the first part, you had three = and single quotes
    thereafter it is double = and double quotes.

    whats the difference?

    thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles