Tuesday, 28 August 2018

The PHP contact form will not display correctly. Last bit of code does not register as code

For whatever reason - and yes, I am new to PHP - the PHP code won't function correctly, but also, it simply won't register as code. In this screenshot you can see that it just displays a plain text for some reason:

<div id="contactform">
    <?php
$action=$_REQUEST['action'];
if ($action=="")    /* display the contact form */
    {
    ?>
    <form  action="" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="action" value="submit">
    Your name:<br>
    <input name="name" type="text" value="" size="30"/><br>
    Your email:<br>
    <input name="email" type="text" value="" size="30"/><br>
    Your message:<br>
    <textarea name="message" rows="7" cols="30"></textarea><br>
    <input type="submit" value="Send email"/>
    </form>

<?php
    }
else                /* send the submitted data */
    {
    $name=$_REQUEST['name'];
    $email=$_REQUEST['email'];
    $message=$_REQUEST['message'];
    if (($name=="")||($email=="")||($message==""))
        {
        echo "All fields are required, please fill the form again.";
        }
    else
    {
        $from="From: $name<$email>/r/n Return-path: $email";
        $subject="Message sent using your contact form";
        mail("tono.nogueras@gmail.com", $subject, $message, $from);
        echo "Email sent!";
        }
    }  ?>
</div>


Your mail headers are incorrect for one thing (see fixed code below), plus as you stated in a comment I noticed now, rename your file to .php instead of .html
HTML files can run as PHP if you tell Apache to do so, but you may as well just rename it to .php, it's a lot simpler that way.
Use \r\n and not /r/n this will cause havoc and will not render Email addresses correctly and will show up as (for example) Bob<email@example.com>/r/nRet in the fromfield.
Change:
$from="From: $name<$email>/r/n Return-path: $email";

to:
$from="From: $name<$email>\r\nReturn-path: $email\r\n";

Having the space between /r/n and Return-path did not render correctly and the server did not recognize the email as a proper Email address in having the space in there.

Edit

Try this, see if you're still getting an undefined index error:
Sidenote: Replace email@example.com with your own E-mail address.
<div id="contactform">
    <?php
if (!isset($_POST['action']))    /* display the contact form */
    {
    ?>
    <form  action="" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="action" value="submit">
    Your name:<br>
    <input name="name" type="text" value="" size="30"/><br>
    Your email:<br>
    <input name="email" type="text" value="" size="30"/><br>
    Your message:<br>
    <textarea name="message" rows="7" cols="30"></textarea><br>
    <input type="submit" value="Send email"/>
    </form>

<?php
    }
else                /* send the submitted data */
    {
    $name=$_REQUEST['name'];
    $email=$_REQUEST['email'];
    $message=$_REQUEST['message'];
    if (($name=="")||($email=="")||($message==""))
        {
        echo "All fields are required, please fill the form again.";
        }
    else
    {
        $from="From: $name<$email>\r\nReturn-path: $email\r\n";
        $subject="Message sent using your contact form";
        mail("email@example.com", $subject, $message, $from);
        echo "Email sent!";
        }
    }  ?>
</div>

0 comments:

Post a Comment