Contact form has error when click send

I receive the following error when l click send on my website contact form. Parse error : syntax error, unexpected ‘$txt’ (T_VARIABLE) in /home4/djboziah/public_html/form-to-email.php on line 8 here is the code is my html code `

						<input class="bo-1-rad-3 bocl13 size-a-19 f1-s-13 cl5 plh6 p-rl-18 m-b-20" type="text" name="email" placeholder="Email*">
						<button class="size-a-20 bg2 borad-3 f1-s-12 cl0 hov-btn1 trans-03 p-rl-15 m-t-20" type="submit" name="submit">
							Send
						</button>
					</form>`

PHP code.

 <?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];	
$email = $_POST['email'];
$message = $_POST['msg'];

$mailto = "support@domain.com"
$txt = "You have a message From ".$name.".\n\n".$message;
	

mail($mailto, $email, $txt, $headers);
header("Location: form-to-email.php?mailsend");	
}

?>

Try adding a trailing semi-colon to the following:

$mailto = “support@domain.com”

yes that got rid of my error,Thank you…but i know receive this warning when i click send on my contact form Warning : Cannot modify header information - headers already sent by (output started at /home4/djboziah/public_html/form-to-email.php:1) in /home4/djboziah/public_html/form-to-email.php on line 11

@djboziah it is not a good idea to leave personal information when posting code. I have therefore edited your email address.

Thank you thank you for that!

There cannot be any text displayed on the screen before calling header(…) so try adding a break before calling header and see what is being displayed:

mail($mailto, $email, $txt, $headers);

exit; // or die; both will stop the header fron being called
header("Location: form-to-email.php?mailsend");	

I am completely new in PHP, the PHP works even thought i get that warning. Can you give me example how to add a break before calling header.

Try adding exit; or die; as shown in the previous post.

What is the warning message?

Find out what is being displayed to the screen and remove to prevent the warning from appearing.

That got rid of the message.Thank you. If i wanted the subscriber to get a message that says you message has been sent what do i need to enter?

Have a read up on the echo statement.

While it has got rid of the message, it has also prevented your header redirect from working. To get that working, you need to find and cure the cause of the problem, which is that your code has sent some output to the browser before it gets to the header line. It might be just as simple as blank space in the code file before your opening PHP tag. Your code in the first post appears to have a space on the first line:

 <?php

before your < character, but that might just be on the forum. Little things like that can cause it.

i will do some research on echo attribute. Yes there was a space as you mentioned thank you.

if you have nay hints for echo let me know.

i tried the echo…not working, here is the code

<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];	
$email = $_POST['email'];
$message = $_POST['msg'];



$mailto = "support@example.com";
$headers = "From: ".$email;	
$headers .= "Cc: someone@example.com \r\n";
$txt = "You have a message From ".$name.".\n\n".$message;
mail($mailto, $email, $txt, $headers);
header("Location: form-to-email.php?mailsend");	
echo $_POST["msg"]; 

}
?>

You need to give us more information, in what way is it not working? Does it give an error message, display the wrong thing, display nothing? Or do you just not see the display because immediately before it you’ve done a header redirect? Exactly what happens?

Does your email get sent? Keep in mind that a lot of email servers will only send emails “from” domains that they are configured to send, because allowing any from-address is a security issue. You should send the message from your address, and use the “reply-to” header to govern where replies are sent.

Syntax errors here too:

name = _POST[‘name’]; 
email = _POST[‘email’];
message = _POST[‘msg’];
...
mail($mailto, $email, $txt, headers);

unless they’ve just gone wrong when you posted in the forum. In any case, please mark code sections by using three backtick characters on a separate line before and after your code, makes it easier to read.

1 Like

I am sorry for not giving details, form works, the email gets sent and received just fine. What is not working is the message i want the sender/subcriber to receive like you message has been sent.

Does your page redirect now work? If it does, your echo to show the message is after the redirect, so you won’t see it. The message needs to be on the page you redirect to.

You should look at using PHPMailer instead of the built-in PHP mail() function as it’s more reliable, once you have this working.

1 Like

Yes my page redirect now works.ill research on page redirect to.

It works Now. Thank you

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.