Question

I've been working further into my contact form on a webpage and cannot get it to run correctly. (Via an include and running on apache2.2 local server).

<?php
if($_SERVER['Request_Method'] != 'POST') {
    $self = $_SERVER['PHP_SELF'];
?>
<form method="POST" action="<?php echo $self; ?>">
    <table>
        <tr>
            <td style="width:100px;">
                Name:
            </td>
            <td>
                <input style="width:150px; height:25px;" placeholder="First Name" type="text" id="custname" name="custname">
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>    
                <input style="width:150px; height:25px;" placeholder="Last Name" type="text" id="cust2name" name="cust2name">
            </td>
        </tr>
        <tr>
            <td>
                Email:
            </td>
            <td>                
                <input style="width:250px; height:25px;" placeholder="Example@domain.com" type="text" id="custemail" name="custemail">
            </td>
        </tr>
        <tr>
            <td>
                Subject:
            </td>
            <td>
                <input style="width:250px; height:25px;" placeholder="RE:Appointment & Contact" type="text" id="textsubject" name="textsubject">
            </td>
        </tr>
        <tr>
            <td>                
                Message:
            </td>
            <td>
                <textarea id="custtext" name="custtext" placeholder="Please enter your message here..." rows="6" style="resize:none; font-family:arial; width:500px; height:75px;"cols="25"></textarea>
            </td>
        </tr>
        <tr>
            <td>

            </td>
            <td>
                <input type="submit" value="Send"><input type="reset" value="Clear">
            </td>
        </tr>
    </table>
</form>
<?php
} else { 
    $name = $_POST['custname'];
    $email = $_POST['custemail'];
    $text = $_POST['custtext'];
    $subject = $_POST['textsubject'];
    $emailto = "reconnectsteam@hotmail.co.uk";

    $header = "From: $name <$email>\r\nReply-To: $email\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-type:text/html;charset=iso-8859-1\r\n";

    $message = "From: $name, Email: $email<br /><hr />$text";

    $mail($emailto, $subject, $message, $header);

    echo"Your email has been sent, it will be proccessed within 48hours.";
}

?>

Just wondering if this code will work when launched via apache 2.2 (email will not send, but should the rest of the code still work correctly and run? at the moment it seems that it is not. Are there any mistakes? (NO DATABASE included).

Thanks,

Rec

Was it helpful?

Solution

There were two things wrong with your code.

This line:

if($_SERVER['Request_Method'] != 'POST') {

Request_Method must be in uppercase REQUEST_METHOD because it is a superglobal

And the dollar $ in front of mail()

$mail($emailto, $subject, $message, $header);

which should be:

mail($emailto, $subject, $message, $header);

Reformatted and working PHP/HTML form

<?php
if($_SERVER['REQUEST_METHOD'] != 'POST') {
    $self = $_SERVER['PHP_SELF'];
?>
<form method="POST" action="<?php echo $self; ?>">
    <table>
        <tr>
            <td style="width:100px;">
                Name:
            </td>
            <td>
                <input style="width:150px; height:25px;" placeholder="First Name" type="text" id="custname" name="custname">
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>    
                <input style="width:150px; height:25px;" placeholder="Last Name" type="text" id="cust2name" name="cust2name">
            </td>
        </tr>
        <tr>
            <td>
                Email:
            </td>
            <td>                
                <input style="width:250px; height:25px;" placeholder="Example@domain.com" type="text" id="custemail" name="custemail">
            </td>
        </tr>
        <tr>
            <td>
                Subject:
            </td>
            <td>
                <input style="width:250px; height:25px;" placeholder="RE:Appointment & Contact" type="text" id="textsubject" name="textsubject">
            </td>
        </tr>
        <tr>
            <td>                
                Message:
            </td>
            <td>
                <textarea id="custtext" name="custtext" placeholder="Please enter your message here..." rows="6" style="resize:none; font-family:arial; width:500px; height:75px;"cols="25"></textarea>
            </td>
        </tr>
        <tr>
            <td>

            </td>
            <td>
                <input type="submit" value="Send"><input type="reset" value="Clear">
            </td>
        </tr>
    </table>
</form>
<?php
} else { 
    $name = $_POST['custname'];
    $email = $_POST['custemail'];
    $text = $_POST['custtext'];
    $subject = $_POST['textsubject'];
    $emailto = "email@domain.com";

    $header = "From: $name <$email>\r\nReply-To: $email\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-type:text/html;charset=iso-8859-1\r\n";

    $message = "From: $name, Email: $email<br /><hr />$text";

    mail($emailto, $subject, $message, $header);

    echo "Your email has been sent, it will be proccessed within 48hours.";
}

?>

OTHER TIPS

First try to replace

$mail($emailto,...

with

mail($emailto,...

(Remove the "$")

Not exactly answer to Your question, but some global attention / security points :).

Try to left the action empty (by default form is redirecting to the same page). Then check if every needed input was filled rather then check $_SERVER['REQUEST_METHOD'] != 'POST'. In that case You can fill in the form using the entered values.

For example, someone enters only name. Then mail is not send and values given by a user dissapear (someone must enter name once again). I would propose something like this (simple example with only one field - name)

$name = isset($_POST['custname']) ? $_POST['custname'] : '';
$name = htmlspecialchars($name); // !!!!!!!!!!!!!
if ($name != '' && your_valid_function($name)) { // && custmail test && other test
    SEND MAIL;
    header("Location: this_page"); // prevent refreshing this page and sending multiple mails
}
else {
    //Render form here, using $name with filled values, for example:
    echo "<form><input type=\"text\" id=\"custname\" name=\"custname\" value=\"$name\"></form>"; // notice $name here
}

PS. Always use at least htmlspecialchars when using POST values :). Be carefull with robots - form without captcha or some other security features can spam your mail box hard. Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top