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.
Results 1 to 4 of 4

Thread: PHP form help

  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    26
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Question PHP form help

    Hi Guys,

    I need some help to fix this form.
    No mails get sent when the form is submitted, Can someone suggest what I am doing wrong?


    Code:
    <?php
    if(isset($_POST['email'])) {
        $email_to = "xxx@xxx.com";
         
        $email_subject = "Speaker Request";
         
         
        function died($error) {
           
            echo "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo "These errors appear below.<br /><br />";
            echo $error."<br /><br />";
            echo "Please go back and fix these errors.<br /><br />";
            die();
        }
         
      
        if(!isset($_POST['title']) ||
    	!isset($_POST['name']) ||
            !isset($_POST['email']) ||
            !isset($_POST['phone']) ||
            !isset($_POST['message'])) {
            died('We are sorry, but there appears to be a problem with the form you submitted.');       
        }
        $title = $_POST['title']; 
        $nname = $_POST['name']; 
        $email_from = $_POST['email']; 
        $telephone = $_POST['phone']; 
        $comments = $_POST['message']; 
         
        $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,$name)) {
        $error_message .= 'The name you entered does not appear to be valid.<br />';
      }
      
      if(strlen($message) < 2) {
        $error_message .= 'The Comments you entered do not appear to be valid.<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 .= "Title: ".clean_string($title)."\n";
    	$email_message .= "Name: ".clean_string($name)."\n";
        $email_message .= "Email: ".clean_string($email_from)."\n";
        $email_message .= "Telephone: ".clean_string($phone)."\n";
        $email_message .= "Expertise: ".clean_string($message)."\n";
         
    
    $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);  
    ?>
     
    thanks.html
     
    <?php
    }
    die();
    ?>
    Thanks for your help!
    Last edited by codernoob; 12-31-2012 at 01:38 AM.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,310
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Whats the problem, error message, fault?

    If you tell us that, we might be able to tell you an answer
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    26
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Question

    Ah yes, the issue is that I dont get any mails sent when the form is submitted.

    This is the CSS

    Code:
    <form id="speakerform" method="post" action="html_form_send.php"> 
                    <fieldset>
                    <label><strong>Title: *</strong><Select name="title"><option value="Mr.">Mr.</option>
      <option value="Ms.">Ms.</option>
      <option value="Mrs.">Mrs.</opton>
       <option value="Dr.">Dr.</opton>
        </select></label>
                      <label><strong>Name: *</strong><input type="text" value="" name="name"></label>
                      <label><strong>Email: *</strong><input type="text" value="" name="email"></label>
                      <label><strong>Phone: *</strong><input type="text" value="" name="phone"></label>
                      <label><strong>comments: *</strong><textarea input="text" name="message"></textarea></label>
                     <div class="btns"><a class="button-2" href="#form" onClick="document.getElementById('speakerform').reset()">Clear</a><a href="" class="button-2" onClick="document.getElementById('speakerform').submit()">Send</a>
                     </div>
                    </fieldset>  
                  </form>
    Does that..err help you help me?

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Why do you have the following at the bottom, including die()?

    PHP Code:
    thanks.html
     
    <?php
    }
    die();
    ?>
    Although,, I don't know that die() would prevent the email from being sent - I doubt it.

    Use print_r() or similar just before sending the email to confirm that your variables are well-formed.
    Last edited by AndrewGSW; 12-31-2012 at 01:53 AM. Reason: PHP tags
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


  •  

    Posting Permissions

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