Syntax error unexpected '<' [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I keep getting this syntax error for the line where the closing tag is and I can not for the life of me figure out what I did wrong. I thought I followed the instructions for my textbook for this assignment but I obviously didn't if I am getting an error. I must be blind because I can't spot the error. Any help would be greatly appreciated.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Contact Me</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
function validateInput($data, $fieldName) {
global $errorCount;
if (empty($data)) {
echo "\"$fieldName\" is a required field. <br />\n";
++$errorCount;
$retval = "";
} else { //Only clean up the input if it isn't // empty
$retval = trim($data);
$retval = stripslashes($retval);
}
return($retval);
}
function validateEmail ($data, $fieldName) {
global $errorCount;
if (empty($data)) {
echo "\"$fieldName\" is a required field.<br />\n";
++$errorCount;
$retval = "";
} else { // Only Clean up the input if it isn't // empty
$retval = trim($data);
$retval = stripslashes($retval);
$pattern = "/^[\w-]+(\.[\w-]+)*#" .
"[\w-]+(\.[\w-]+)*" .
"(\. [[a-z]]{2,})$/i";
if (preg_match($pattern, $retval)==0) {
echo "\"$fieldName\" is not a valid e-mail address.<br />\n";
++$errorCount;
}
}
return($retval);
}
function displayForm($Sender, $Email, $Subject, $Message) {
?>
<h2 style= "text-align:center">Contact Me</h2>
<form name="contact" action="ContactForm.php" method="post">
<p>Your Name: </p>
<p>Your E-mail:
<input type="text" name="Sender" value="<?php
echo $Sender; ?>" />
<input type="text" name="Email" value="<?php echo $Email; ?>" /></p>
<p>Subect: <input type="text" name="Subject" value="<?php echo $Subject; ?>" /></p>
<p>Message:<br />
</p>
<p><input type="reset" value="Clear Form" /> <input type="submit" name="Submit" value="Send Form" /></p>
</form>
<?php
}
$ShowForm = TRUE;
$errorCount = 0;
$Sender = "";
$Email = "";
$Subject = "";
$Message = "";
if (isset($_POST['Submit'])) {
$Sender =
validateInput($_POST['Sender'], "Your Name");
$Email =
validateEmail($_POST['Email'], "Your E-mail");
$Subject =
validateInput($_POST['Subject'], "Subject");
$Message =
validateInput($_POST['Message'],"Message");
if ($errorCount==0)
$ShowForm = FALSE;
else
$ShowForm = TRUE;
}
if ($ShowForm == TRUE) {
if ($errorCount>0) // if there were errors echo "<p>Please re-enter the form information below.</p>\n";
displayForm($Sender, $Email, $Subject, $Message);
}
else {
$SenderAddress= "$Sender <$Email>";
$Headers= "From: $SenderAddress\nCC:
$SenderAddress\n";
// Substitute your own email address for // recipient#example.com
$result = mail ("recipient#example.com",
$Subject, $Message, $Headers);
if ($result)
echo "<p>Your message has been sent. Thank you, " . $Sender . ".</p>\n";
else
echo "<p>There was an error sending your message, " .
$Sender . ".</p>\n";
</body>
</html>

You are not closing the last else block
else {
$SenderAddress= "$Sender <$Email>";
$Headers= "From: $SenderAddress\nCC:
$SenderAddress\n";
// Substitute your own email address for // recipient#example.com
$result = mail ("recipient#example.com",
$Subject, $Message, $Headers);
if ($result)
echo "<p>Your message has been sent. Thank you, " . $Sender . ".</p>\n";
else
echo "<p>There was an error sending your message, " .
$Sender . ".</p>\n";
} ?>

Related

E-Mail Validation Error in PHP All-in-One Form

I was doing an exercise from a book and copied exactly what was in there (to my knowledge) yet I still can't get it to process a valid E-Mail. I've tried different regular expressions but have had no luck. I'm pretty sure I missed something stupid (and sound pretty stupid asking for this but I am new to PHP). Can anyone take a look and see if you can see what I'm missing (or doing wrong)?
<!DOCTYPE html>
<html lang="en">
<head>
<title>Contact Me</title>
</head>
<header>
</header>
<body>
<?php
function validateInput($data, $fieldName) {
global $errorCount;
if (empty($data)) {
echo "\"$fieldName\" is a required field.<br />\n";
++$errorCount;
$retval = "";
} else {
$retval = trim($data);
$retval = stripslashes($retval);
}
return($retval);
}
function validateEmail($data, $fieldName) {
global $errorCount;
if(empty($data)) {
echo "\"$fieldName\" is a required field.<br />\n";
++$errorCount;
$retval = "";
} else {
$retval = trim($data);
$retval = stripslashes($retval);
$pattern = "/^[\w-]+(\.[\w-]+)*#" . "[\w-]+(\.[\w-]+)*" . "(\.[[a-z]]{2,})$/i";
if (preg_match($pattern, $retval)==0) {
echo "\"$fieldName\" is not a valid e-mail address.<br />\n";
++$errorCount;
}
}
return($retval);
}
function displayForm($Sender, $Email, $Subject, $Message) {
?>
<h2 style = "text-align:center">Contact Me</h2>
<form name=contact: action="ContactForm.php" method="post">
<p>Your Name: <input type="text" name="Sender" value="<?php echo $Sender; ?>" /></p>
<p>Your E-mail: <input type="text" name="Email" value="<?php echo $Email; ?>" /></p>
<p>Subject: <input type="text" name="Subject" value="<?php echo $Subject; ?>" /></p>
<p>Message: <br />
<textarea name="Message"><?php echo $Message; ?></textarea></p>
<p><input type="reset" value="Clear Form" />
<input type="submit" name="Submit" value="Send Form" /></p>
</form>
<?php
}
$showForm = TRUE;
$errorCount = 0;
$Sender = "";
$Email = "";
$Subject = "";
$Message = "";
if (isset($_POST['Submit'])) {
$Sender = validateInput($_POST['Sender'], "Your Name");
$Email = validateEmail($_POST['Email'], "Your Email");
$Subject = validateInput($_POST['Subject'], "Subject");
$Message = validateInput($_POST['Message'], "Message");
if($errorCount==0)
$showForm = FALSE;
else
$showForm = TRUE;
}
if ($showForm == TRUE) {
if ($errorCount>0)
echo "<p>Please re-enter the form information below.</p>\n";
displayForm($Sender, $Email, $Subject, $Message);
}
else {
$SenderAddress = "$Sender <$Email>";
$Headers = "From: $SenderAddress\n CC: $SenderAddress\n";
$result = mail("greg.englar#gmail.com", $Subject, $Message, $Headers);
if ($result)
echo "<p>Your message has been sent. Thank you, " . $Sender . ".</p>\n";
else
echo "<p>There was an error sending your message, " . $Sender . ".</p>\n";
}
?>
</body>
</html>
I think I have a good grasp as to what the code is doing and how it is working but I am clearly missing something. It doesn't accept any E-Mail as valid and always shows an error message. For reference the book I'm using is "PHP Programming with MySQL Second Edition" by Don Gosselin.
There's an error in the regular expression. Should be [a-z] instead of [[a-z]] at the end

header Location isn't transferring [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
I have form in my site example.com/pp.php and that formĀ“s action is pp.php because that script is not some external file, but inside of that page. The problem is I need to put header("Location: http://example.com/pp.php#contactForm"); because after pressing Send button I want to reload page on exact position which is /pp.php#contactForm. But header Location is not working.
<form action="pp.php" method="post">
<label>Name:</label>
<input type="text" name="name" value="<?php if($_POST['name']) {
echo $_POST['name']; } ?>" />
<label>Email:</label>
<input type="text" name="email" value="<?php if($_POST['email'])
{ echo $_POST['email']; } ?>" />
<label>Message:</label><br />
<textarea name="message" rows="20" cols="20"><?php
if($_POST['message']) { echo $_POST['message']; } ?></textarea>
<label><img src="captcha.php"></label>
<input type="text" name="code"> <br />
<input type="submit" class="submit" name="submit" value="Send
message" />
</form>
This is php
<?php
if (isset($_POST['submit'])) {
$error = "";
if (!empty($_POST['name'])) {
$name = $_POST['name'];
} else {
$error .= "You didn't type in your name. <br />";
}
if (!empty($_POST['email'])) {
$email = $_POST['email'];
if (!preg_match("/^[a-z0-9]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*
(\.[a-z]{2,3})$/i", $email)){
$error .= "The e-mail address you entered is not valid. <br/>";
}
} else {
$error .= "You didn't type in an e-mail address. <br />";
}
if (!empty($_POST['message'])) {
$message = $_POST['message'];
} else {
$error .= "You didn't type in a message. <br />";
}
if(($_POST['code']) == $_SESSION['code']) {
$code = $_POST['code'];
} else {
$error .= "The captcha code you entered does not match. Please try
again. <br />";
}
if (empty($error)) {
$from = 'From: ' . $name . ' <' . $email . '>';
$to = "mail#gmail.com";
$subject = "New contact form message";
$content = $name . " has sent you a message: \n" . $message;
$success = "<h3>Thank you! Your message has been sent!</h3>";
mail($to,$subject,$content,$from);
}
}
?>
<?php
if (!empty($error)) {
echo '<p class="error"><strong>Your message was NOT sent<br/> The
following error(s) returned:</strong><br/>' . $error . '</p>';
} elseif (!empty($success)) {
echo $success;
}
header("Location: http://example.com/pp.php#contactForm");
?>
You can't redirect with header() after outputting to the DOM:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
As such, you'll need to remove the echo statements in your lines:
if (!empty($error)) {
echo '<p class="error"><strong>Your message was NOT sent<br/> The
following error(s) returned:</strong><br/>' . $error . '</p>';
} elseif (!empty($success)) {
echo $success;
}
Before calling:
header("Location: http://example.com/pp.php#contactForm");
Try this:
header('Location: pp.php#contactForm');
And make sure you do not output any html tag through anyway before this line.
like the echo $success;

Form not showing?

I require some assistance that I hope that one of you can answer for me. I have an assignment for my PHP course. For this assignment we were tasked with making a form with 4 input fields (including a reset and submit button). The fields are supposed to be labeled as name, address, email, and phone number. Now when I write the code into Dreamweaver I get no syntax errors but whenever I execute the script using Wamp, the form doesn't show. Any help would be appreciated as this is something that has to be done for our midterm.
<?php
function validateInput($data, $fieldName) {
global $errorCount;
if (empty($data)) {
echo "\"$fieldName\" is a required field. <br />\n";
++$errorCount;
$retval = "";
} else { //Only clean up the input if it isn't // empty
$retval = trim($data);
$retval = stripslashes($retval);
}
return($retval);
}
function validateEmail ($data, $fieldName) {
global $errorCount;
if (empty($data)) {
echo "\"$fieldName\" is a required field.<br />\n";
++$errorCount;
$retval = "";
} else { // Only Clean up the input if it isn't // empty
$retval = trim($data);
$retval = stripslashes($retval);
$pattern = "/^[\w-]+(\.[\w-]+)*#" .
"[\w-]+(\.[\w-]+)*" .
"(\. [[a-z]]{2,})$/i";
if (preg_match($pattern, $retval)==0) {
echo "\"$fieldName\" is not a valid e-mail address.<br />\n";
++$errorCount;
}
}
return($retval);
}
function displayForm($Sender, $Email, $Address, $Phone) {
?>
<h2 style= "text-align:center">Contact Us</h2>
<form name="contact" action="contact_us.php" method="post">
<p>Your Name: <input type="text" name="Sender" value="<?php
echo $Sender; ?>" /> </p>
<p>Your E-mail: <input type="text" name="Email" value="<?php echo $Email; ?>" /></p>
<p>Address: <input type="text" name="Address" value="<?php echo $Address; ?>" /></p>
<p>Phone #: <input type="number" name="Phone" value="<?php echo $Phone; ?>"<br />
</p>
<p><input type="reset" value="Clear Form" /> <input type="submit" name="Submit" value="Send Form" /></p>
</form>
<?php
}
$ShowForm = TRUE;
$errorCount = 0;
$Sender = "";
$Email = "";
$Address = "";
$Phone = "";
if (isset($_POST['Submit'])) {
$Sender =
validateInput($_POST['Sender'], "Your Name");
$Email =
validateEmail($_POST['Email'], "Your E-mail");
$Subject =
validateInput($_POST['Address'], "Your Address");
$Message =
validateInput($_POST['Phone'],"Your number");
if ($errorCount==0)
$ShowForm = FALSE;
else
$ShowForm = TRUE;
}
if ($ShowForm == TRUE) {
if ($errorCount>0) // if there were errors echo "<p>Please re-enter the form information below.</p>\n";
displayForm($Sender, $Email, $Address, $Phone);
}
else {
$SenderAddress= "$Sender <$Email>";
$Headers= "From: $SenderAddress\nCC:
$SenderAddress\n";
// Substitute your own email address for // recipient#example.com
$result = mail ("recipient#example.com",
$Subject, $Message, $Headers);
if ($result)
echo "<p>Your message has been sent. Thank you, " . $Sender . ".</p>\n";
else
echo "<p>There was an error sending your message, " .
$Sender . ".</p>\n";
} ?>
You see nothing becouse you din`t call the function displayForm() you just create it.
so put this displayForm(); before the end of your php tag and see what will happen.
if ($ShowForm == TRUE) {
if ($errorCount>0) // if there were errors echo "<p>Please re-enter the form information below.</p>\n";
displayForm($Sender, $Email, $Address, $Phone);
}
It looks like your form is set to only show when $ShowForm == TRUE && $errorCount > 0. Since your defaults are $ShowForm = TRUE and $errorCount = 0 your form will never show.
This is why using curly braces on your ifs, while making the file larger, help greatly when troubleshooting.

Undefined variable Trying to fix my contact form

I have been trying to fix my contact form wherein the data can be sent via email. But i seem to have some errors at the start. It says in the web page "Undefined variable" yet. I'm only following a tutorial that i have been reading and i'm not yet adept in PHP. I'm using XAMPP at the moment in order to run my PHP
Here is the HTML Markup
<html>
<head>
<title>Contact Form</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1>Contact Form</h1>
<p class="error"> There are some misisng fields.</p>
<?php if($error == true) { ?>
<?php } if($sent == true) { ?>
<p class="sent">Thank you for sending your message</p><?php } ?>
<div class="contactform">
<form name="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="name">Name:</label>
<input type="text" name="name" />
<label for="email">Email:</label>
<input type="email" name="email" />
<label for="comments">Comments:</label>
<textarea name="comments"></textarea>
<input type="submit" name="submit" class="submit" value="submit" />
</form>
</div>
Here is the PHP Code
<?php if($_POST['submit']) {
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comments'])) {
$error = true;
} else {
$to = "clestcruz#gmail.com";
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$comments = trim($_POST['comments']);
$subject = "Contact Form";
$messages = "Name: $name \r\n Email: $email \r\n Comments: $comments";
$headers = "From:" . $name;
$mailsent = mail($to, $subject, $messages, $headers);
if($mailsent){
$sent= true;
}
}
}
?>
</body>
</html>
Undefine Variables
<?php if($error == true) { ?>
<?php } if($sent == true) { ?>
if($_POST['submit']) {
Try declaring the variables before you use it. PHP will give out a notice if you don't pass a value first.
<?php
$error=false;
$sent=false;
if(isset($_POST['submit'])) {
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comments'])) {
$error = true;
} else {
$to = "clestcruz#gmail.com";
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$comments = trim($_POST['comments']);
$subject = "Contact Form";
$messages = "Name: $name \r\n Email: $email \r\n Comments: $comments";
$headers = "From:" . $name;
$mailsent = mail($to, $subject, $messages, $headers);
if($mailsent){
$sent= true;
}
}
}
?>
These lines:
<?php if($error == true) { ?>
<?php } if($sent == true) { ?>
appear near the top of your HTML, but as far as I can see, there's been no PHP executed at this point, so $error and $sent won't be defined.
This line:
if($_POST['submit']) {
is testing for a value, but unless your form has been submitted, it too won't be defined. You could test this more effectively with
if (isset($_POST['submit'])) {
// do stuff
}

Form doesn't display

Can someone tell me why this is not displaying the form? If I go to the address that I have it hosted on, the page just displays "Your message has been sent. Thank you, ." It seems to just execute and display the last function.
<?php
function validateInput($data, $fieldName) {
global $errorCount;
if (empty($data)) {
echo "\"$fieldName\" is a required field.<br />\n";
++$errorCount;
$retval = "";
} else { // only clean up the input if it isn't empty
$retval = trim($data);
$retval = stripsplashes($retval);
}
return($retval);
}
function validateEmail($data, $fieldName) {
global $errorCount;
if (empty($data)) {
echo "\"$fieldName\" is a required field.<br />\n";
++$errorCount;
$retval = "";
} else { // only clean up the input if it isn't empty
$retval = trim($data);
$retval = stripsplashes($retval);
$pattern = "/^[\w-]+(\.[\w-]+)*#" . "[\w-]+(\.[\w-]+)*" . "(\.[[a-z]]{2,})$/i";
if (preg_match($pattern, $retval) == 0) {
echo "\"$fieldName\" is not a valid e-mail address.<br />\n";
++$errorCount;
}
}
return($retval);
}
function displayForm($Sender, $Email, $Subject, $Message) {
?>
<h2 style = "text-align:center">Contact Me</h2>
<form name="contact" action="ContactForm.php" method="post">
<p>Your name: <input type="text" name="Sender" value="<?php echo $Sender; ?>" /></p>
<p>Your E-mail: <input type="text" name="Email" value="<?php echo $Sender; ?>" /></p>
<p>Subject: <input type="text" name="Subject" value="<?php echo $Subject; ?>" /></p>
<p>Message:<br />
<textarea name="Message"><?php echo $Message; ?></textarea></p>
<p><input type="reset" value="Clear Form" /> <input type="submit" name="Submit" value="Send Form" /></p>
</form>
<?php
}
$ShowForm = TRUE;
$errorCount = 0;
$Sender = "";
$Email = "";
$Subject = "";
$Message = "";
if (isset($_POST['Submit'])) {
$Sender = validateInput($_POST['Sender'],"Your Name");
$Email = validateInput($_POST['Email'],"Your E-mail");
$Subject = validateInput($_POST['Subject'],"Subject");
$Message = validateInput($_POST['Message'],"Message");
if ($errorCount == 0)
$ShowForm = FALSE;
else
$ShowForm = TRUE;
}
if ($ShowForm == TRUE)
if ($errorCount>0) {// if there were errors
echo "<p>Please re-enter the form information below.</p>\n";
displayForm($Sender, $Email, $Subject, $Message);
} else {
$SenderAddress = "$Sender <$Email>";
$Headers = "From: $SenderAddress\nCC: $SenderAddress\n";
// Substitute your own e-mail address for recipient#example.com
$result = mail("recipient#example.com", $Subject, $Message, $Headers);
if ($result)
echo "<p>Your message has been sent. Thank you, " . $Sender . ".</p>\n";
else
echo "<p>There was an error sending your message, " . $Sender . ".</p>\n";
}
?>
You have:
$showForm = TRUE;
Which, when the form has not yet been submitted, will lead to the conditional statement always being true with $errorCount == 0. You don't call displayForm() in the else case of that conditional.
Hope this helps.
Ok, the error was because curly brace should've been after the if ($ShowForm == TRUE) instead of the if ($errorCount>0).

Categories