Email form and redirect back to the site

5 replies
hi i'm a newbie on php and i want to create a form that will collect the name and contact number of the person and send it to my email.

can anyone help me on this?
i've tried this code but doesn't send email to my email account
<?php if(isset($_POST['submit'])) {
$to = "myemail@sample.com"; $name = $_POST['name']; $contact = $_POST['contactl']; $body = "From: $name\n E-Mail: $contact; echo "Data has been submitted to $to!"; mail($to, $body);} else { echo "error!";} ?>can you help me on this?

Thanks
#back #email #redirect #site
  • Profile picture of the author Brandon Tanner
    Not sure what you are trying to do with the $body variable, but it looks pretty funky! (at the very least, it's missing end quotes). Looks like you're missing the Headers ("from") and Subject Line as well. Also, it's a good idea to sanitize/validate the submission.

    Here's a tutorial that explains the entire process, from start to finish...

    PHP form to email explained

    BTW- If you put each statement in your code on a separate line, it will be much easier to read and work with!
    Signature

    {{ DiscussionBoard.errors[5285024].message }}
  • Profile picture of the author kokopelli
    Here's a very simple short tutorial to get to this basic form (just add a contact number field):
    Code:
    <?php
    // Contact subject
     ="";
    // Details
    ="";
    
    // Mail of sender
    ="";
    // From
    ="from:  <>";
    
    // Enter your email address
     ='someone@somewhere.com';
    
    =mail(,,,);
    
    // Check, if message sent to your email
    // display message "We've recived your information"
    if(){
    echo "We've recived your contact information";
    }
    else {
    echo "ERROR";
    }
    ?>
    Signature
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    {{ DiscussionBoard.errors[5285246].message }}
  • Profile picture of the author Ikmal Syifai
    And you can use this to redirect back to your site:
    PHP: header - Manual
    {{ DiscussionBoard.errors[5289811].message }}
    • Profile picture of the author ansokclifford
      thanks for the reply. I've got a code that worked but only for chrome not on mozilla. What should be the problem?

      Here is the code

      <form method="post" name="submit" action="PhpFile.php">
      <p>sample text</p>
      <div class="text1"><input name="name" type="text" size="22" id="name" maxlength="30" class="input" />1</div>
      <div class="text2"><input name="tel" type="text" size="22" id="tel" maxlength="30" class="input" />2</div>
      <div class="submit"><input name="send" type="image" class="submit1" value="&nbsp;" /></div>
      </form>

      and the php

      <?php
      if(isset($_POST['send'])){
      $errors = '';
      $name = $_POST['name'];
      $tel = $_POST['tel'];
      $myemail = 'mymail@gmail.com';//<-----Put Your email address here.
      $subject= 'Ball';

      if($errors=='')
      {
      $to = $myemail;
      $email_subject = "$subject";//<-----Put Your email subject here.
      $email_body = "name: $name\nContact Number: $tel";


      mail($to, $email_subject, $email_body);
      //redirect to the 'thank you' page, when message is successfully sent...
      ?>
      <script type="text/javascript">
      window.location = "Thanks.html"
      </script>


      <?php

      }
      }
      ?>
      {{ DiscussionBoard.errors[5289985].message }}
      • Profile picture of the author Jeff Chandler
        Instead of
        mail($to, $email_subject, $email_body);
        //redirect to the 'thank you' page, when message is successfully sent...
        ?>
        <script type="text/javascript">
        window.location = "Thanks.html"
        </script>

        Use:
        if ( mail($to, $email_subject, $email_body) ) {
        //redirect to the 'thank you' page, when message is successfully sent...
        header ('Location: Thanks.html');
        }
        else {
        // put what you want to do if the email fails here
        }
        ?>

        Just make sure the php code is at the top of the page before you output any html or the redirect won't work. This takes it out of the javascript code and puts it all server side.
        {{ DiscussionBoard.errors[5291382].message }}

Trending Topics