Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 43

Thread: PHP Form help!

  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    177
    Thanks
    20
    Thanked 2 Times in 1 Post

    PHP Form help!

    Hi again, I have another question. So I have this PHP code which corresponds with an html form:

    PHP Code:
    <?  
    $errors 
    ''
    $myemail $_POST['email'];//<-----Put Your email address here. 

    // Assuming the code stays the same each time ...
    if(!strtolower($_POST['verify']) == "a12098"){
    print 
    'Uh oh! You have forgotten to enter in the correct verification code! <br><br><a href="javascript:window.close();">Please click here to close this window.</a>'
    exit;
    }
     
    if(
    $myemail == ""){ 
    print 
    'Uh oh! You have forgotten to enter in an email address! <br><br><a href="javascript:window.close();">Please click here to close this window.</a>'
    }else{

    $inputs=$_POST["i"];

       
    $to $myemail;   
       
    $email_subject "Contact form submission: $name";  
       
    $email_body "Checklist:\n";   
       foreach(
    $inputs as $item){
       if(
    $item!=""){
       
    $email_body.=$item."\n";
       }
       }
         
       
    $headers "From: $myemail";   
         
       
    mail($to,$email_subject,$email_body,$headers);  
       
    //redirect to the 'thank you' page  
       
    print 'Your checklist has succesfully been submitted. The user your checklist is being sent to will receive ane mail shortly with the checklist information. Thank you.<br><br>Click here to close this window.'
    }
    ?>
    And it's all working really well. The only problem is when the user enters in an un-valid email address, it will give it the default "Internal Service Error" message. I would like the user to be presented with a custom message. Thanks in advance!

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    double quotes are causing an error ... escape them with a slash ...


    print 'Uh oh! You have forgotten to enter in an email address! <br><br><a href=\"javascript:window.close();\">Please click here to close this window.</a>';

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Posts
    177
    Thanks
    20
    Thanked 2 Times in 1 Post
    Oh, no. That's not what the error is. It's when the user enters in a non valid email I want them to see a custom error message. Here's a working copy:

    http://www.pxlcreations.com/paraguide/checklist.html

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Maybe we should just stay with the thread below ... LOL!

    http://www.webdesignforums.net/showt...t=33468&page=2

    I just figured-out it's you and I on different forums.
    Last edited by mlseim; 10-22-2009 at 08:31 PM.

  • #5
    Regular Coder
    Join Date
    Oct 2009
    Posts
    177
    Thanks
    20
    Thanked 2 Times in 1 Post
    Quote Originally Posted by mlseim View Post
    Maybe we should just stay with this thread ... LOL!

    http://www.webdesignforums.net/showt...t=33468&page=2

    I just figured-out it's you and I on different forums.
    Haha! Oh, wow, this is a small world. Lol, ok, to WDF

  • #6
    Regular Coder
    Join Date
    Oct 2009
    Posts
    177
    Thanks
    20
    Thanked 2 Times in 1 Post
    Ok, well if there is anybody out there who can help me a little bit more, that will be great (thanks mlseim). So I have these two pieces of code:

    HTML:

    Code:
    <script language="Javascript" type="text/javascript">
    var counter = 1;
    var limit = 30;
    function addInput(divName){
         if (counter == limit)  {
              alert("You have reached the limit of adding " + counter + " inputs");
         }
         else {
              var newdiv = document.createElement('div');
              newdiv.innerHTML = "<br><input type='text' name='i[]'>";
              document.getElementById(divName).appendChild(newdiv);
              counter++;
         }
    }
    </script>
    
    <script type="text/javascript">
    function createPopup(target){
    var w = window.open("", target, "width=400, height=200");
    w.focus();
    }
    </script>
    </head>
    <body>
    
    <form name="form" action="email.php" method="post" target="submit" onsubmit="createPopup(this.target)">
    
    <div id="dynamicInput">
    <br><input type="text" name="i[]">
    </div>
    
    <input type="button" value="Add another text input" onClick="addInput('dynamicInput');">
    <br>
    
    <input name="email" type="text"> <br />
    <input name="submit" type="submit">
    
    <input type="reset"/>
    </form>
    PHP:

    Code:
    <?  
    $errors = ''; 
    $myemail = $_POST['email'];//<-----Put Your email address here. 
    if($myemail == ""){ 
    print 'Uh oh! You have forgotten to enter in an email address! <br><br><a href="javascript:window.close();">Please click here to close this window.</a>'; 
    }else{
    
    $inputs=$_POST["i"];
    
       $to = $myemail;   
       $email_subject = "Contact form submission: $name";  
       $email_body = "Checklist:\n";   
       foreach($inputs as $item){
       if($item!=""){
       $email_body.=$item."\n";
       }
       }
         
       $headers = "From: $myemail";   
         
       mail($to,$email_subject,$email_body,$headers);  
       //redirect to the 'thank you' page  
       print 'Your checklist has succesfully been submitted. The user your checklist is being sent to will receive ane mail shortly with the checklist information. Thank you.<br><br>Click here to close this window.'; 
    }
    ?>
    I would like to add 2 features to these items. I would like a verification code required when the user submits the form (if it's wrong, they are asked to enter the correct code) and the form to display an error message when the email address entered is not valid (can't be sent). Thanks in advance.

  • #7
    Regular Coder
    Join Date
    Oct 2009
    Posts
    177
    Thanks
    20
    Thanked 2 Times in 1 Post
    So anyone know how to fix this?

  • #8
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Google for "php captcha". They can be a little troublesome depending upon which you pick to use. I've used this one on a couple of sites.
    Are you a Help Vampire?

  • #9
    Regular Coder
    Join Date
    Oct 2009
    Posts
    177
    Thanks
    20
    Thanked 2 Times in 1 Post
    Ok, thanks! I'll look into that, do you know how to verify the email address in my PHP too?

  • #10
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    The PHP mail() function doesn't/can't know if an email address is legitimate or if a message has been received. For that matter, it can't even tell you if a message has been sent anywhere by the server. It's only "aware" of whether a mail server has accepted a message for delivery or rejected it.

    However, PHP can check if an email address is in a correct email address format. That requires regular expressions and PHP functions like preg_match. The search term would be something like "php email address validation". Should be lots of results and usable snippets on this forum.
    Are you a Help Vampire?

  • #11
    Regular Coder
    Join Date
    Oct 2009
    Posts
    177
    Thanks
    20
    Thanked 2 Times in 1 Post
    That php captcha I can't get to work. It will still send the email even if the verification code isn't correct. I'll keep working with it and let you know what I find.

  • #12
    Regular Coder
    Join Date
    Oct 2009
    Posts
    177
    Thanks
    20
    Thanked 2 Times in 1 Post
    Ok, so I have this code:

    Code:
    <?  
    $errors = ''; 
    $myemail = $_POST['email'];//<-----Put Your email address here. 
    
    if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $myemail)) 
    {
       print 'Invalid email';
    }
    
    if($myemail == ""){ 
    print 'Uh oh! You have forgotten to enter in an email address! <br><br><a href="javascript:window.close();">Please click here to close this window.</a>'; 
    }else{
    
    $inputs=$_POST["i"];
    
       $to = $myemail;   
       $email_subject = "Contact form submission: $name";  
       $email_body = "Checklist:\n";   
       foreach($inputs as $item){
       if($item!=""){
       $email_body.=$item."\n";
       }
       }
         
       $headers = "From: $myemail";   
         
       mail($to,$email_subject,$email_body,$headers);  
       //redirect to the 'thank you' page  
       print 'Your checklist has succesfully been submitted. The user your checklist is being sent to will receive ane mail shortly with the checklist information. Thank you.<br><br><a href="javascript:window.close();">Click here to close this window.</a>'; 
       
    }
    ?>
    And it works fine, except for this part:

    Code:
    if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $myemail)) 
    {
       print 'Invalid email';
    }
    It isn't printing that message if the email is invalid. How come?

  • #13
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Unrelated, but the ereg functions are deprecated as of PHP 5.3.0. You may want to update to a preg_match version.

    The eregi function returns the length of the match or FALSE for no match. Your conditional statement, then, translates as: "If this is a valid email address, tell them it's invalid. Else, no problem." You need to invert that with a not operator: !.
    Are you a Help Vampire?

  • #14
    Regular Coder
    Join Date
    Oct 2009
    Posts
    177
    Thanks
    20
    Thanked 2 Times in 1 Post
    Ok, I'm not completely following, where does the ! go? Here's my new code:

    Code:
    if (preg_match("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $myemail)) 
    {
       print 'Invalid email';
    }

  • #15
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Your preg_match syntax is off. Check the man page for examples. The pattern should be wrapped by forward slashes, for example.

    Negate the test by placing the not in front of the function name:
    PHP Code:
    if (!preg_match($pattern$myemail)) 
    {
       print 
    'Invalid email';

    Are you a Help Vampire?


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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