Hi,

So recently I downloaded a template off the internet for the site, and it has a contact form in it but no way to set it up. After contacing the developer he linked me to a site, however, its not been all that useful as I still cannot get it to work. When I put anything in and press "Send Message" it just gives me this error: "Parse error: syntax error, unexpected T_STRING in /home/bdurodjc/public_html/Home/mail.php on line 5"

I'm unsure of what this means and what I can do to fix it. Also, the "Send Message" and "Clear Form" buttons are white and not green like they should be, I'd also like them to be on the same line but I couldn't figure out a way of doing this. Apologies for asking so much, the other thing is I don't know how to make it check that the email address is valid.

The website this is on is at: http://chocants.co.uk/Home/ and the approriate code is below.

index.html (Form)

  <form action="mail.php" method="POST">
    <div class="row half">
      <div class="6u">
        <input type="text" class="text" name="name" placeholder="Name">
      </div>
      <div class="6u">
        <input type="text" class="text" name="email" placeholder="Email">
      </div>
    </div>
    <div class="row half">
      <div class="12u">
        <textarea name="message" placeholder="Message"></textarea>
      </div>
    </div>
    <div class="row">
      <div class="12u">
        <ul class="actions">
          <li><input type="submit" class="button form" value="Send Message"><input type="reset" class="button form" value="Clear Form"></li>
        </ul>
      </div>
    </div>
  </form>

mail.php

<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "jadon.mayhew@me.com<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>

Thanks for any help :)

Hi,

you cannot submit javascript as recipient, so this:

$recipient = "jadon.mayhew@me.com<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>";

must be:

$recipient = "jadon.mayhew@me.com";

check the available formats for the first argument of the mail() function:

Hi,

After doing that it gave me another error: Parse error: syntax error, unexpected T_VAR in /home/bdurodjc/public_html/Home/mail.php on line 7 - This is extremely confusing me :(

Can you show the updated code? Currently it seems that the javascript code is still in the PHP block, as example:

<?php

$recipient = "jadon.mayhew@me.com";

/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */

The var keyword in the Javascript code, will generate your error, but only because the engine is trying to interpretate the Javascript as PHP, so remove the Javascript from there, if you want to validate the email, use the filter_var() function:

if( filter_var($_POST['to'], FILTER_VALIDATE_EMAIL) !== FALSE)
{
    $email = $_POST['to'];
}

Docs:

  <form action="mail.php" method="POST">
    <div class="row half">
      <div class="6u">
        <input type="text" class="text" name="name" placeholder="Name">
      </div>
      <div class="6u">
        <input type="text" class="text" name="email" placeholder="Email">
      </div>
    </div>
    <div class="row half">
      <div class="12u">
        <textarea name="message" placeholder="Message"></textarea>
      </div>
    </div>
    <div class="row">
      <div class="12u">
        <ul class="actions">
          <li><input type="submit" class="button form" value="Send Message"><input type="reset" class="button form" value="Clear Form"></li>
        </ul>
      </div>
    </div>
  </form>

Thats the form on the index.html

Below is the mail.php

<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "jadon.mayhew@me.com";
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>

If you take a look at chocants.co.uk/Home, you'll see the buttons are on top of each other and not next to each other. Also they should be the color when you hover over them by default, but they're white for some random reason?

Hi, check my previous updated answer, i.e. remove the Javascript code from the PHP block and it should work fine.

Hi,

Sorry, I hadn't seen your code then when I had replied. I've added that in now and it appears to work, though, the code you put..

if( filter_var($_POST['to'], FILTER_VALIDATE_EMAIL) !== FALSE)
{
    $email = $_POST['to'];
}

Does not check if the email is correct, as I did it and it allowed me to send an email that is not legitamete.

This is how mail.php now looks

<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "jadon.mayhew@me.com";
if( filter_var($_POST['to'], FILTER_VALIDATE_EMAIL) !== FALSE)
{
    $email = $_POST['to'];
}
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>

Did you notice the quotation marks are double around the script type you'll need to change those to single quotes (') or place a backslash before each double quote (\")

Original

$recipient = "jadon.mayhew@me.com<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>";

So this is what it should look like:

$recipient = "jadon.mayhew@me.com<script type='text/javascript'>
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName('script');l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>";

Hi,

Thanks, that seems to have made it so that you cannot enter an incorrect email, however, it won't let me enter any email at all now?

<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "jadon.mayhew@me.com<script type='text/javascript'>
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName('script');l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("Location: emailsent.html");
?>

Josh suggestion is syntactically correct but you cannot include javascript in the recipient value, i.e. in the first argument of the mail() function.

The above should be:

<?php

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $formcontent="From: $name \n Message: $message";
    $recipient = "jadon.mayhew@me.com";
    $subject = "Contact Form";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    header("Location: emailsent.html");

?>

My previous suggestion about the filter_var() instead was an example, based on an hypotetical to input field, something like:

<input type="text" name="to" />

So you have to change it to match your form, in your case:

<?php

    $email = FALSE;

    if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) !== FALSE))
    {
        $email = $_POST['email'];
    }

    $name = $_POST['name'];
    $message = $_POST['message'];
    $formcontent="From: $name \n Message: $message";
    $recipient = "jadon.mayhew@me.com";
    $subject = "Contact Form";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    header("Location: emailsent.html");

    ?>

If it does not validate then apply the trim() function to $_POST['email'] or simply test an hardcoded email:

if(filter_var('your@mail.tld', FILTER_VALIDATE_EMAIL) !== FALSE))
{
    echo 'The mail is correct';
}

This will validate:

$email = 'your@mail.tld';

This instead it will not:

$email = ' your@mail.tld';

Because of the extra space, so if you want to apply trim, at the top of the script place:

$_POST = trim($_POST);

Complete example:

<?php

    $email = FALSE;
    $_POST = trim($_POST);

    if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) !== FALSE))
    {
        $email = $_POST['email'];
    }

    $name = $_POST['name'];
    $message = $_POST['message'];
    $formcontent="From: $name \n Message: $message";
    $recipient = "jadon.mayhew@me.com";
    $subject = "Contact Form";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    header("Location: emailsent.html");

    ?>

But consider to apply the filter_var() function to filter all the input received by the form, because user input it is not trustable. For more information check the linked documentation in my previous post.

Hi,

I'm really not good with PHP which is why I posted here, but I remove the additional bracket and it caused more erros. After putting it back, this error appeared again:

"Parse error: syntax error, unexpected ')' in /home/bdurodjc/public_html/Home/mail.php on line 3"

After playing around a bit more with all of the code I'm still back to square one with this weird error. I don't think it was checking if the email was correct beforehand either, which is frustrating as people could put false emails.

Also, I have no idea why the "Send Message" thing is white and not green, like it should be. It seems to have swapped itself, when I hover over it, it goes green, however it's supposed to be the other way around. Nothing changed in the CSS either.

I have no idea why the "Send Message" thing is white and not green, like it should be. It seems to have swapped itself, when I hover over it, it goes green, however it's supposed to be the other way around. Nothing changed in the CSS either.

Probably because of this rule:

form input.button,
form input.text,
form select,
form textarea
{
    -webkit-appearance: none;
    display: block;
    border: 0;
    background: #f5f5f5;
    width: 100%;
    padding: 0.75em;
    -moz-transition: background-color 0.35s ease-in-out;
    -webkit-transition: background-color 0.35s ease-in-out;
    -o-transition: background-color 0.35s ease-in-out;
    -ms-transition: background-color 0.35s ease-in-out;
    transition: background-color 0.35s ease-in-out;
}

The form input.button, declaration overrides this one:

.button
{
    position: relative;
    display: inline-block;
    border: 0;
    background: #35b88f;
    color: #fff;
    text-shadow: 0 0 0.5px rgba(255,255,255,0.25);
    cursor: pointer;
    text-decoration: none;
    outline: 0;
    padding: 1em 3em 1em 3em;
    text-align: center;
    border-radius: 3em;
    font-weight: 400;
    -moz-transition: background-color 0.35s ease-in-out, color 0.35s ease-in-out;
    -webkit-transition: background-color 0.35s ease-in-out, color 0.35s ease-in-out;
    -o-transition: background-color 0.35s ease-in-out, color 0.35s ease-in-out;
    -ms-transition: background-color 0.35s ease-in-out, color 0.35s ease-in-out;
    transition: background-color 0.35s ease-in-out, color 0.35s ease-in-out;
}

So just remove the form input.button from that rule and it should work fine.

The CSS issue happens because of:

3) Sort rules with the same importance and origin by specificity of selector: more specific selectors will override more general ones. Pseudo-elements and pseudo-classes are counted as normal elements and classes, respectively.
4) Finally, sort by order specified: if two declarations have the same weight, origin and specificity, the latter specified wins. Declarations in imported style sheets are considered to be before any declarations in the style sheet itself.

Source: http://www.w3.org/TR/CSS2/cascade.html#cascading-order

Hi,

Thanks, so that's that fixed however that error is still occurring from earlier: "
Parse error: syntax error, unexpected ')' in /home/bdurodjc/public_html/Home/mail.php on line 3"

Sorry that's my mistake, in my first example I didn't close the IF statement:

if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) !== FALSE)

It should be:

if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) !== FALSE))

With two ending parentheses.

I've added that in and its still exactly the same error.

<?php
    $_POST = trim($_POST);
    if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) !== FALSE))
    {
        $email = $_POST['email'];
    }
    $name = $_POST['name'];
    $message = $_POST['message'];
    $formcontent="From: $name \n Message: $message";
    $recipient = "jadon.mayhew@me.com";
    $subject = "Contact Form";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    header("Location: emailsent.html");
    ?>

Ok, I'm terrible sorry, my further mistake, not two ending parentheses but one:

<?php
    $_POST = trim($_POST);
    if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) !== FALSE)
    {
        $email = $_POST['email'];
    }
    $name = $_POST['name'];
    $message = $_POST['message'];
    $formcontent="From: $name \n Message: $message";
    $recipient = "jadon.mayhew@me.com";
    $subject = "Contact Form";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    header("Location: emailsent.html");
    ?>

Testing the form and the scripts now it works fine.

This is what it happens when I have the flu and under antivirals o_o'

Urm, have I done something wrong?

Warning: trim() expects parameter 1 to be string, array given in /home/bdurodjc/public_html/Home/mail.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at /home/bdurodjc/public_html/Home/mail.php:2) in /home/bdurodjc/public_html/Home/mail.php on line 14

It's weird, I'm getting the email but its not giving me the message, who it's from or the email it was from. Its pretty redundant. However, its still displaying that error above rather than directing to the correct page.

Okay, I've got it all working but unsure on how to make it check an email addresss is valid.

<?php 
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "jadon.mayhew@me.com";
if( filter_var($_POST['to'], FILTER_VALIDATE_EMAIL) !== FALSE)
{
    $email = $_POST['to'];
}
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("Location: emailsent.html");
?>

Use this:

<?php

$_POST = array_map('trim', $_POST);

if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) !== FALSE)
{
    $email = $_POST['email'];
}

$name = $_POST['name'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "jadon.mayhew@me.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("Location: emailsent.html");
?>

It works. The filter_var function verifies if the $_POST['email'] is a valid email address. This could be false, as example abc@cba.tld but you cannot always verify if a mail is currently active or not.

Regarding this error:

Warning: trim() expects parameter 1 to be string, array given in /home/bdurodjc/public_html/Home/mail.php on line 2

This is due to another error done by me, I tested your script using a string version:

$email = trim($_POST['email']);

But when I wrote the example I translated that to the entire $_POST array, my fault, the correct version needs the implementation of array_map():

$_POST = array_map('trim', $_POST);

The array_map works essentially as a loop, like this:

foreach($_POST as $key => $value)
{
    $_POST[$key] = trim($value);
}

Docs: http://php.net/array_map

Its still sending emails when I put anything in the email address field :/

Add an else statement to stop the script basing on the result of the filter. Here you can redirect it to your previous page or to a specific error page. You can use the session to collect the errors and display them to the redirected page or, in alternative, you can log the errors quietly by using error_log().

As example with a simple redirect:

<?php

$_POST = array_map('trim', $_POST);

if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) !== FALSE)
{
    $email = $_POST['email'];
}

else
{
    header("Location: error_page.html");
}

$name = $_POST['name'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "jadon.mayhew@me.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("Location: emailsent.html");

Another example:

<?php

$email = FALSE;
$_POST = array_map('trim', $_POST);

if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) !== FALSE)
{
    $email = $_POST['email'];
}

if($email !== FALSE)
{
    $name = $_POST['name'];
    $message = $_POST['message'];
    $formcontent="From: $name \n Message: $message";
    $recipient = "jadon.mayhew@me.com";
    $subject = "Contact Form";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    header("Location: emailsent.html");
}
else
{
    header("Location: error_page.html");
}

Or simply:

<?php

$email = $_POST['email'];
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === FALSE)
{
    header("Location: error_page.html");
}

Or:

<?php

if($email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
{
    $name = $_POST['name'];
    $message = $_POST['message'];
    $formcontent="From: $name \n Message: $message";
    $recipient = "jadon.mayhew@me.com";
    $subject = "Contact Form";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    header("Location: emailsent.html");
}

else
{
    header("Location: error_page.html");
}

There are many ways to write this. If still in trouble I'll try to help further. I'm sorry for the misunderstanding :)

Hey,

No worries, you're much better at PHP than I will ever be and it is all working now too!

Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.