SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    May 2013
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    "Contact Us" Form

    I have created a simple contact form on my site <snip/>

    The form code in the web page is:

    <form action="includes/mail.php" method="POST">
    <p style="color: rgb(27,161,226); font-weight: bold; font-family: calibri;">Name</p>
    <input name="name" type="text">
    <p style="color: rgb(27,161,226); font-weight: bold; font-family: calibri;">Email</p>
    <input name="email" type="text">
    <p style="color: rgb(27,161,226); font-weight: bold; font-family: calibri;">Message</p>
    <textarea name="message" rows="8" cols="40"></textarea><br>
    <input value="Send" type="submit"><input value="Clear" type="reset">
    </form>

    The content of mail.php is:

    <!--?php $name = $_POST['name'];
    $email = $_POST['email'];$message = $_POST['message'];
    $formcontent="From: $name \n Message: $message";$recipient = "enquiries@la-matha.com";
    $subject = "Contact Form";$mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");echo "Thank You!" . " -" . "<a href='../contact2.php' style='text-decoration:none;color:rgb(27,161,226);'-->
    <html>
    <head>
    <meta content="text/html; charset=windows-1252" http-equiv="content-type">
    </head>
    <body>Return to La Matha site";
    ?&gt;
    </body>
    </html>

    I would like to miss the echo page out, and have the user go straight to contact2.php when pressing "send",

    Forgive what is probably a basic and simple request, but I am a novice with no background in programming etc.

    Denis
    Last edited by Mittineague; Jul 19, 2013 at 16:59. Reason: removing unnecessary link

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,433
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Hi Denis,

    You can redirect the user directly to contact2.php using the header function, like this:
    PHP Code:
    header('Location: http://www.la-matha.com/contact2.php'); 
    It's important to note that you must call the function before outputting anything to the screen, otherwise you'll get an error.

    Here's the full example of your mail.php page with the page redirection code added. I've also added some basic filtering of your form data, which is always a good idea when you're dealing with input from users. Although I've not shown it here, you might also want to check that the email address supplied is valid, and $message isn't empty before you send the email.
    PHP Code:
    <?php
    $name 
    filter_input(INPUT_POST'name'FILTER_SANITIZE_SPECIAL_CHARS);
    $email filter_input(INPUT_POST'email'FILTER_SANITIZE_EMAIL);
    $message filter_input(INPUT_POST'message'FILTER_SANITIZE_SPECIAL_CHARS);

    $formcontent "From: $name \n Message: $message";
    $recipient "enquiries@la-matha.com";
    $subject "Contact Form";
    $mailheader "From: $email \r\n";

    if ( 
    mail($recipient$subject$formcontent$mailheader) ){
        
    header('Location: http://www.la-matha.com/contact2.php');
        exit;
    } else {
        
    // Would be better to present some kind of friendly error for your users!
        
    die("Error!");
    }

  3. #3
    SitePoint Member
    Join Date
    May 2013
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help - it works exactly as I want it to. I'm not going to bother with validation - to be honest, I don't expect much traffic, but I wanted something that looked professional.

    Help from sitepoint is always appreciated!

    Denis

  4. #4
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can redirect to the user page using header function but it behave differently on each server and its not worked in some server so another way is,

    Code PHP:
    $targetPage ="http://www.la-matha.com/contact2.php";
    Code JavaScript:
    <script language="javascript">
                window.location.href="<?php echo $targetPage;?>";
            </script>

  5. #5
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,433
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Quote Originally Posted by msankar View Post
    you can redirect to the user page using header function but it behave differently on each server and its not worked in some server
    Can you explain what you mean? I've never had a problem using the header function to do a redirect.. the location header is part of the HTTP spec and should work across all browsers.

    I'd avoid using JS to do a redirection if possible because obviously it's going to break if the user has JS disabled.

  6. #6
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    Can you explain what you mean? I've never had a problem using the header function to do a redirect.. the location header is part of the HTTP spec and should work across all browsers.

    I'd avoid using JS to do a redirection if possible because obviously it's going to break if the user has JS disabled.
    I am not sure about that, while i used header function it shows error "Cannot modify header information - headers already sent by (output started at.. )" . What is reason its shows this error?

  7. #7
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,433
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Quote Originally Posted by msankar View Post
    I am not sure about that, while i used header function it shows error "Cannot modify header information - headers already sent by (output started at.. )" .
    The header function has to be called before any output is sent to the browser, otherwise you get the error message you describe.
    PHP Code:
    // This will trigger a "headers already sent" error
    echo "<p>Thank you for your email</p>";
    header('Location: http://www.example.com'); 
    PHP Code:
    // So will this, because of the space before the opening PHP tag
     <?php
    header
    ('Content-type: application/json');

  8. #8
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh k, Thanks lot


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
  •