PHP contact form

Discussion in 'PHP' started by seosheela, May 6, 2011.

  1. #1
    I have a PHP contact form on my website and the all fields are emailed to me blank.
    It was working. all fields empty but submit send a mail , Any ideas? for erroe message empty filed please help me

    <script language="JavaScript" type="text/javascript">
    		
    function goTo() {
    	document.location.href="contacts.html";
    }
    
    
    window.onload=function(){
    	setTimeout(function(){ goTo(); },1000);
    }	
    </script>
    </head>
    
    <body  onload="goTo();" >
    
     
    <?php
     
    // Name of the website.
     
    $szFrom = "Enquiry for Great India Group";
     
    // Your email, (the recipient email address).
     
    $szRecipient = "foresightseo@gmail.com";
     
    // Website email address ( Sender's email address ).
     
    $szFromEmail = "Enquiry";
     
    /* Title comes from a hidden input in the form. This gives
     
    the possibility to set up more than one form and use this same script.*/
     
    $szTitle = $_POST['title'];
     
    // This sets up the email subject.
     
    $szSubject = $szFrom.": ".$szTitle;
     
    /* All input fields coming from the form/s should go here.
     
    The first value is the name attribute used and the second
     
    value on the right the name wanted to display
     
    on the final email.*/
     
    $aPosted = array(
     
    "visitor" => "Name",
     
    "Phone" => "phone Number",
     
    "visitormail" => "Email Address",
     
    "notes" => "Message",
     
    // add here your fields
     
    );
     
    # ------------------------------------------------------------
     
    // Email Content
     
    # ------------------------------------------------------------
     
    $szEmailContent.= $szSubject."\n";
     
    $szEmailContent.= "------------------------------------------------------------\n\n";
     
    $szLast = "";
     
    foreach($aPosted as $value => $szReal)
     
    {
    if(empty($mail)) {
    	?> 
    <script type="text/javascript">
        alert("Your Message Send Successfully");
        history.back();
     </script> 
      <?php
    }
     
    if(isset($_POST[$value]))
     
    {
     
    if($szLast != $szReal)
     
    {
     
    $szEmailContent.= $szReal.":";
     
    $szLast = $szReal;
     
    };
     
    $szEmailContent.= " ".$_POST[$value]."\n\n";
     
    };
    
    };
     
    # ------------------------------------------------------------
     
    // Declare Email headers
     
    # ------------------------------------------------------------
     
    $szHeaders = 'From: ' . $szFromEmail . "\r\n" . 'Reply-To: ' . $szFromEmail . "\r\n" . 'X-Mailer: PHP/' . phpversion();
     
    # ------------------------------------------------------------
     
    // Output
     
    # ------------------------------------------------------------
     
    if( mail( $szRecipient, $szSubject, $szEmailContent, $szHeaders, '-f' . $szFromEmail ) )
     
    // go to previous page ($lasturl) with message=1 (should be a succes message).
     
    header( 'Location: http://www.poonik.org' . $_SERVER['HTTP_HOST']  . html_entity_decode( $_POST['lasturl'] ) . '?message=1' );
     
    else
     
    // go to previous page ($lasturl) with message=2 (should be a failure message).
     
    header( 'Location: http://www.poonik.org' . $_SERVER['HTTP_HOST']  . html_entity_decode( $_POST['lasturl'] ) . '?message=2' );
     
    ?>
    
    PHP:
     
    seosheela, May 6, 2011 IP
  2. textsurp

    textsurp Peon

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Why use an array? The code seems sloppy.
     
    textsurp, May 6, 2011 IP
  3. nani

    nani Peon

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    PHP Simple Contact Form

    The simplest way to send an email with PHP is to send a text email.

    In the example below we first declare the variables ($to, $subject, $message, $from, $headers), then we use the variables in the mail() function to send an e-mail:
    <?php
    $to = "someone@example.com";
    $subject = "Test mail";
    $message = "Hello! This is a simple email message.";
    $from = "someonelse@example.com";
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);
    echo "Mail Sent.";
    ?>
    Code (markup):
    PHP Mail Form

    With PHP, you can create a feedback-form on your website. The example below sends a text message to a specified e-mail address:
    <html>
    <body>
    
    <?php
    if (isset($_REQUEST['email']))
    //if "email" is filled out, send email
      {
      //send email
      $email = $_REQUEST['email'] ;
      $subject = $_REQUEST['subject'] ;
      $message = $_REQUEST['message'] ;
      mail("someone@example.com", "$subject",
      $message, "From:" . $email);
      echo "Thank you for using our mail form";
      }
    else
    //if "email" is not filled out, display the form
      {
      echo "<form method='post' action='mailform.php'>
      Email: <input name='email' type='text' /><br />
      Subject: <input name='subject' type='text' /><br />
      Message:<br />
      <textarea name='message' rows='15' cols='40'>
      </textarea><br />
      <input type='submit' />
      </form>";
      }
    ?>
    
    </body>
    </html>
    Code (markup):
    This is how the example above works:

    * First, check if the email input field is filled out
    * If it is not set (like when the page is first visited); output the HTML form
    * If it is set (after the form is filled out); send the email from the form
    * When submit is pressed after the form is filled out, the page reloads, sees that the email input is set, and sends the email

    Note: This is the simplest way to send e-mail, but it is not secure. In the next chapter of this tutorial you can read more about vulnerabilities in e-mail scripts, and how to validate user input to make it more secure.
     
    nani, May 6, 2011 IP