Results 1 to 2 of 2

Thread: PHP form not submitting email, but redirecting to sender.php URL

  1. #1
    Join Date
    Jun 2010
    Posts
    59
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default PHP form not submitting email, but redirecting to sender.php URL

    Hi, I have a form that I tweaked from a free online form I found (not very well versed in PHP, and definitely not in PHP5 yet!). When you click the submit button, rather than sending the email and posting the thank you message, it just takes you to the sender.php 'page' which is blank. Here is the HTML code:

    HTML Code:
    <form name="dform" action="sender.php" method="post">
            <table>
    <tr>
    <td>
    		email : <input type="text" name="email_from" style="width:215px; height:15px; overflow:hidden; background-color:#ffffff; border:solid; border-width:1px; border-color:#6986B7; font-family: Arial, Helvetica, sans-serif; color:#6986B7; font-size:14; letter-spacing:1px;" />
            
            &nbsp;
             child’s age: years <select name="age_years">
    		<option value="0" style="background-color:#ffffff;">0</option>
    		<option value="1" style="background-color:#ffffff;">1</option>
            <option value="2" style="background-color:#ffffff;">2</option>
            <option value="3" style="background-color:#ffffff;">3</option>
            <option value="4" style="background-color:#ffffff;">4</option>        
     		<option value="5" style="background-color:#ffffff;">5</option>
    		</select>
            
                 
             months <select name="age_months">
    		<option value="0" style="background-color:#ffffff;">0</option>
    		<option value="1" style="background-color:#ffffff;">1</option>
            <option value="2" style="background-color:#ffffff;">2</option>
            <option value="3" style="background-color:#ffffff;">3</option>
              <option value="4" style="background-color:#ffffff;">4</option>
              <option value="5" style="background-color:#ffffff;">5</option>
             <option value="6" style="background-color:#ffffff;">6</option>
            <option value="7" style="background-color:#ffffff;">7</option>
             <option value="8" style="background-color:#ffffff;">8</option>
             <option value="9" style="background-color:#ffffff;">9</option>
             <option value="10" style="background-color:#ffffff;">10</option>
             <option value="11" style="background-color:#ffffff;">11</option>
             <option value="12" style="background-color:#ffffff;">12</option>
    		</select>
            </td>
            <td>
    		
    <input type="image" src="images/hb-learnmorebutton.png" value="submit" name="submit" style="border:none; background:#ffffff; margin-left:10px;" />
    </td></tr></table>
    
    </form>
    And here is the PHP code from sender.php:

    PHP Code:
    <?php
    if(isset($_POST['email'])) {
         
        
    // EDIT THE 2 LINES BELOW AS REQUIRED
        
    $email_to "kate@madisonandmi.com";
        
    $email_subject "Contact Form Submission";
         
         
        function 
    died($error) {
            
    // your error code can go here
            
    echo "Oops!";
            echo 
    "These errors appear below.<br /><br />";
            echo 
    $error."<br /><br />";
            echo 
    "Please fix these errors and try again.<br /><br />";
            die();
        }
         
        
    // validation expected data exists
        
    if(!isset($_POST['email']) ||
            !isset(
    $_POST['age_years']) ||
            !isset(
    $_POST['age_months'])) {
            
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
        }
         
        
    $email_from $_POST['email']; // required
        
    $telephone $_POST['age_years']; // required
        
    $comments $_POST['age_months']; // required
         
        
    $error_message "";
        
    $email_exp '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
      if(!
    preg_match($email_exp,$email_from)) {
        
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
      }
        
    $string_exp "/^[A-Za-z .'-]+$/";

      if(!
    preg_match($string_exp,$age_years)) {
        
    $error_message .= 'Please enter your childs age in years.<br />';
      }
      if(
    strlen($age_months) < 2) {
        
    $error_message .= 'Please enter your childs age in months.<br />';
      }
      if(
    strlen($error_message) > 0) {
        
    died($error_message);
      }
        
    $email_message "Form details below.\n\n";
         
        function 
    clean_string($string) {
          
    $bad = array("content-type","bcc:","to:","cc:","href");
          return 
    str_replace($bad,"",$string);
        }
         
        
    $email_message .= "Email: ".clean_string($email_from)."\n";
        
    $email_message .= "Years: ".clean_string($age_years)."\n";
        
    $email_message .= "Months: ".clean_string($age_months)."\n";
         
         
    // create email headers
    $headers 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' phpversion();
    @
    mail($email_to$email_subject$email_message$headers);  
    ?>
    <!-- include your own success html here -->
    <link href="style.css" rel="stylesheet" type="text/css">
    <body class="center">
     
    Thank you for your interest in haepi bean! <br />We will be in touch soon!

    <?php 
    }
    ?>

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    To answer why your sender.php page is blank, it's because you have no form field named email, and so your if() condition is not met. You have age_years, age_months, submit, and email_from.

    However, I have seen this contact form example before, and I will recommend against using it. It has bad validation, bad mailing practices, bad user experience, and security vulnerabilities.

    If you're interested in "starting from scratch" -as you stated in your other thread- why not start from scratch?

    Your form is basically fine.
    HTML Code:
    <form name="dform" action="sender.php" method="post">
        <p>
            <label>email: <input type="text" name="email_from"></label>
        <p>
            <label>child’s age (years): 
                <select name="age_years">
    		<option value="0">0</option>
    		<option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>        
     		<option value="5">5</option>
                </select>
            </label>
        <p>
            <label>child's age (months): 
                <select name="age_months">
    		<option value="0">0</option>
    		<option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                    <option value="7">7</option>
                    <option value="8">8</option>
                    <option value="9">9</option>
                    <option value="10">10</option>
                    <option value="11">11</option>
                    <option value="12">12</option>
                </select>
            </label>
        <p>
            <input type="image" src="images/hb-learnmorebutton.png" value="submit" name="submit">
    </form>
    Let's think about how to process it.
    PHP Code:
    <?php
    // make sure form was submitted, including all required fiels
    if( 
        isset( 
    $_POST['submit'] ) 
        && isset( 
    $_POST['email_from'] )
        && isset( 
    $_POST['age_years'] )
        && isset( 
    $_POST['age_months'] )
    ){

        
    // validate email address (see http://php.net/filter_var):
        
    $email_from filter_var$_POST['email_from'] );

        
    // if the filter fails, $email_from will be false
        
    if( $email_from === false ){

            
    // add an error message
            
    $err "Please enter a valid email address.";
        }

        
    // validate age selections.  the regex specifies digits only.
        
    $age_years filter_var$_POST['age_years'],FILTER_VALIDATE_REGEXP,'/^[\d]+$/' );
        
    $age_months filter_var$_POST['age_months'],FILTER_VALIDATE_REGEXP,'/^[\d]+$/' );

        
    // if the filter fails, the variable(s) will be false.
        // because these are <select> fields, they should _always_ pass.
        // if they do not, it means that someone has been manipulating the form 
        //    - quite possibly indicating an attack attempt.
        // stop processing immediately.
        
    if(
            
    $age_years === false
            
    || $age_months === false
        
    ){
            die();
        }

        
    // check if there is an error.
        
    if( empty( $err ) ){

            
    // no error; proceed.
            
    $to "your@email.address";
            
    $subject "Contact Form Submission";

            
    // compose email body.
            
    $message "Email: $email_from\n"
                
    ."Years: $age_years\n"
                
    ."Months: $age_months\n";

            
    // compose email headers.
            
    $headers "From: $email_from\r\n"
                
    ."Sender: auto@your-website.com\r\n";

            
    // send
            
    if( mail$to,$subject,$message,$headers ) ){

                
    // accepted by mail server
                
    header"Location: http://your-website.com/email-success" );
                exit;
            }else{

                
    // not
                
    header"Location:  http://your-website.com/email-failed" );
                exit;
            }
        }else{

            
    // error; show error message.
            
    ?>
            <h1>Oops!</h1>
            <p><?= $err ?></p>
            <p>Please <a href="link/to/form">go back</a> and try again.</p>
            <?php
        
    }
    }

Similar Threads

  1. Confirmation Email PHP Sender
    By itskater in forum PHP
    Replies: 32
    Last Post: 09-12-2013, 09:28 PM
  2. Submitting Email form
    By suresh_gop in forum PHP
    Replies: 4
    Last Post: 08-04-2010, 02:38 PM
  3. Replies: 1
    Last Post: 04-08-2010, 07:02 PM
  4. Recall Form Values script 1 - problem after submitting form
    By FirkinB in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 05-01-2007, 08:50 PM
  5. redirecting a page after submitting a form
    By davindersangha in forum HTML
    Replies: 2
    Last Post: 10-26-2005, 07:46 AM

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
  •