Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

php issue

Options
  • 17-10-2010 10:34pm
    #1
    Registered Users Posts: 972 ✭✭✭


    hi i spent ages trying to get this to work but cant, can anyone see what i am doing wrong

    i keep getting this error on form submission
    "Warning: Cannot modify header information - headers already sent by"

    php

    <?php
    if(isset($_POST)) {
    $redirect = "My_thank_you_page.html";
    $to = "robert@robert.com";
    $subject = "Email enquiry From the website";
    $name_field = $_POST;
    $email_field = $_POST;
    $phone_field = $_POST;
    $message = $_POST;
    $body = "From: $name_field\nE-Mail: $email_field\nPhone: $phone_field\nMessage:\n $message";
    $redirect = "http://www.google.ie";
    mail($to, $subject, $body);
    header("$redirect");

    } else {

    }
    ?>


Comments

  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    yeah, you cant have any output/html prior to a header call, so i assume you have other code above that section in your php file?


  • Registered Users Posts: 8,488 ✭✭✭Goodshape


    Even a space or line break before the opening <?php could cause this error.


  • Registered Users Posts: 2,793 ✭✭✭oeb


    In addition to what the others mentioned there, there is a problem with your use of header(). The correct call should be:

    header("Location: $redirect");


  • Registered Users Posts: 972 ✭✭✭Prefect_1998


    no i am VERY new to this,

    the php is all i have.. it was working but called an echo when the submit button was called..

    i want it to redirect back to the page the info was sent on

    Thanks for help


  • Registered Users Posts: 2,793 ✭✭✭oeb


    no i am VERY new to this,

    the php is all i have.. it was working but called an echo when the submit button was called..

    i want it to redirect back to the page the info was sent on

    Thanks for help


    Ok, I'll try and explain it a little better.

    The PHP header() function sends HTTP headers to the client. Because the header has to be sent before the data, you can not output ANYTHING before you use header().

    (By output I mean either inline html, or through print, echo etc)

    The most common reason for receiving the headers already sent error message is that you either have a space or a newline before the php opening tag (<?php ). Make sure that the tag is on the very start of the very first line of your document.

    Now, what I was referring to was the syntax of your use of header(). To use header() to redirect someone to lets say, google.com, you would say
    header('Location: http://www.google.com');

    You are missing the 'Location:' bit in your code (6 lines from the bottom).


  • Advertisement
  • Registered Users Posts: 894 ✭✭✭Dale Parish


    danielle_door,
    Assuming the data being sent is from a form and you want it to redirect to the same page with the form on it then all you have to do is include the PHP code in that page.
    
    <?php
    if($_POST['submit']) {
    $to = "robert@robert.com";
    $subject = "Email enquiry From the website";
    $name_field = $_POST['name'];
    $email_field = $_POST['email'];
    $phone_field = $_POST['phone'];
    $message = $_POST['message'];
    $body = "From: $name_field\nE-Mail: $email_field\nPhone: $phone_field\nMessage:\n $message";
    mail($to, $subject, $body);
    
    } else {
    echo "Some error message, possibly relating to form validation..";
    }
    ?>
    
    <form method="post" action="sendemail.php">
    Name: <input type="text" name="name"/>
    Email: <input type="text" name="email"/>
    Phone: <input type="text" name="phone"/>
    //depending on how large you want the message to be
    Message: <input type="text" name="message"/>
    <input type="submit" name="submit"/>
    <form/>
    

    As oeb said it would need to be header('Location: somepage.html');

    Also you had $redirect = in there twice.


Advertisement