0

Hello PHP and web enthusiasts, I am having a problem with my php code when making a simple email page. The error i am getting is syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier on line 19. Here is my code. I've been trying to fix it for days.

   <html>
    <h1> Contact Form </h1>
    <form action ="HW2-4.php?action=send"method="POST">
    <p> Name: <input type="text" name="name" /></p>
    <p>Email: <input type="text" name="email" /></p>
    <p>Subject: <input type="text" name="subject" /></p> 
    <p>Message: <textarea col="20" row="5" name="message" ></textarea></p>
    <input type="submit" value="Send Email"/> 
   <?php
if(isset($_POST['action']) and $_POST['action'] == "send"){
    $name    = $_POST["name"];
    $email   = $_POST["email"];
    $subject = $_POST["subject"];
    $message = $_POST["message"];
    $headers = $_POST["headers"];

    if (mail($email, $subject, $message, $headers) == 1) {
        echo "<h3>Hello ".$_POST['name']." An email has been sent to you at ".$_POST['email']."!</h3>";
    }
}

The page loads, but the echo statement is not showing up after email is sent and i dont think the email is sending.

zz94scifi
  • 1
  • 1

1 Answers1

4

That is because the $_POST['name'] and $_POST['email'] variables are not parsed properly. You should concatenate them instead, which is personally my way to inject variables into strings (although there are other alternatives), i.e:

echo "<h3>Hello ".$_POST['name']." An email has been sent to you at ".$_POST['email']."!</h3>";

In fact, simply drop the use of double quotes instead:

echo '<h3>Hello '.$_POST['name'].' An email has been sent to you at '.$_POST['email'].'!</h3>';

You can also check out the various ways how strings are parsed, which means these are also valid. With regards to which is better falls within the domain of micro-optimisation, as the difference between any of them can be considered insignificant compared to other operations the server is tasked with.

Method 1: use {}:

echo "<h3>Hello {$_POST['name']} An email has been sent to you at {$_POST['email']}!</h3>";

Method 2: or drop the use of ' entirely:

echo "<h3>Hello $_POST[name] An email has been sent to you at $_POST[email]!</h3>";

Method 3 (suggested by @Blowski): use sprintf():

sprintf('<h3>Hello %s. An email has been sent to you at %s!</h3>', $_POST['name'], $_POST['email']);

Method 4: Make a variable (you already have them in this case)

echo "<h3>Hello $name. An email has been sent to you at $email!</h3>"
Community
  • 1
  • 1
Terry
  • 63,248
  • 15
  • 96
  • 118
  • 1
    Or use `sprintf('

    Hello %s. An email has been sent to you at %s!

    ', $_POST['name'], $_POST['email'])`, which I personally find much more readable.
    – Dan Blows Jan 21 '15 at 15:49
  • His condition is also wrong, see Sumit's answer and consider adding that solution as well – samrap Jan 21 '15 at 15:49
  • @Blowski Oh yes, I have totally forgotten about that. Thanks for the heads up, and also included your comment in my updated answer (with credits appropriately given) – Terry Jan 21 '15 at 15:53
  • Won't method two cause a warning because it converts the undefined constant into a string? – Ruan Mendes Jan 21 '15 at 16:11