Page 1 of 1

error msg on form presented in PHP book

Posted: Wed Jul 27, 2005 9:25 pm
by bruceg
I bought a book recently called 'PHP 5 Fast and Easy web development'

on of the chapters shows how to create a form to send info as email,

I am trying it out with their code and I am getting an error. It could be b/c of a typo and I was hoping someone on this list could locate it if they have a good PHP editor.

Code: Select all

<?
$form_block = "
<FORM METHOD=\"POST\" ACTION=\"$PHP_SELF\">
<p><strong>Your Name:</strong>< /br>
<INPUT type=\"text"\" NAME=\"senders_name\" SIZE=30></p>
<p><strong><p><Your E-mail Address:</strong>< /br>
<INPUT type=\"text\" NAME="senders_email\" SIZE=30></p>
<p><strong>Message:</strong>< /br>
<TEXTAREA NAME=\"message\" COLS=30 ROWS=5 WRAP=virtual></TEXTAREA></p>
<INPUT type=\"hidden\" name=\"op\" value=\"ds\">
<p><INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Send this Form\"></p>
</FORM>";

if ($_POST[op] !="ds") {
// they need to see the form
echo "$form_block";
} else if ($_POST[op] =="ds") {
//check value of $_POST[sender name]
if ($_POST[sender_name] =="") {
			$name_err = "<font color=red>Please enter your name!</font>< /br>";
			$send ="no";
}
//check value of $_POST[sender_email]
if ($POST[sender_email] =="") {
		$email_err ="<font color=red>Please enter your email address!</font>< /br>";
		$send= "no";
}
//check value of $_POST[message]
if ($POST[message] =="") {
		$message_err = "<font color=red>Please enter a message!</font>< /br>";
		$send ="no";
}
if ($send !="no") {
//it's o.k to send, so build the mail
		$msg ="E-MAIL SENT FROM WWW SITE\n";
		$msg .="Senders Name:		$POST[senders_name]\n";
		$msg .="Senders E-MAIL:		$POST[senders_email]\n";
		$msg .="Senders Name:		$POST[message]\n\n";
		$to ="webguync@gmail.com";
		$subject = "There has been a disturbance in the Force";
		$mailheaders .="Reply-To: $_POST[sender_email]\n";
//send the mail
		mail ($to, $subject, $msg, $mailheaders);
		//display confirmation to user
		echo "<p>mail has been sent!</p>";
} else if ($send =="no") {
		//print error messages
		echo "$name_err";
		echo "$email_err";
		echo "$message_err";
		echo "$form_block";
	}
}
?>
the error I get is :

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /hsphere/local/home/bruceg/inspired-evolution.com/Contact_Form_test.php on line 38

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /hsphere/local/home/bruceg/inspired-evolution.com/Contact_Form_test.php on line 40

thanks in advance for any assistance!

Posted: Wed Jul 27, 2005 9:37 pm
by stukov
I see many unescaped double-quotes (where they should be escaped). Giving you all the answers would be too easy :)

Take for example line 7 where you have NAME="senders_email". It should be NAME=\"senders_email\".

Then I saw many things like this : $POST[sender_email]. Here is the correct syntax: $_POST['sender_email'].

These may be all typo errors, just revise your code for these kind of mistakes, it should really help.

Posted: Wed Jul 27, 2005 11:03 pm
by bruceg
thanks, I fixed those and I am still getting an error, so there may be another typo or another issue.


Here is the entire PHP code (updated)

Code: Select all

<?
$form_block = "
<FORM METHOD=\"POST\" ACTION=\"$PHP_SELF\">
<p><strong>Your Name:</strong>< /br>
<INPUT type=\"text\" NAME=\"senders_name\" SIZE=30></p>
<p><strong><p><Your E-mail Address:</strong>< /br>
<INPUT type=\"text\" NAME=\"senders_email\" SIZE=30></p>
<p><strong>Message:</strong>< /br>
<TEXTAREA NAME=\"message\" COLS=30 ROWS=5 WRAP=virtual></TEXTAREA></p>
<INPUT type=\"hidden\" name=\"op\" value=\"ds\">
<p><INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Send this Form\"></p>
</FORM>";

if ($_POST['op'] !="ds") {
// they need to see the form
echo "$form_block";
} else if ($_POST['op'] =="ds") {
//check value of $_POST['sender name']
if ($_POST['sender_name'] =="") {
			$name_err = "<font color=red>Please enter your name!</font>< /br>";
			$send ="no";
}
//check value of $_POST['sender_email']
if ($POST['sender_email'] =="") {
		$email_err ="<font color=red>Please enter your email address!</font>< /br>";
		$send= "no";
}
//check value of $_POST['message']
if ($POST['message'] =="") {
		$message_err = "<font color=red>Please enter a message!</font>< /br>";
		$send ="no";
}
if ($send !="no") {
//it's o.k to send, so build the mail
		$msg ="E-MAIL SENT FROM WWW SITE\n";
		$msg .="Senders Name:		$POST['senders_name']\n";
		$msg .="Senders E-MAIL:			$POST['senders_email']\n";
		$msg .="Senders Name:		$POST['message']\n\n";
		$to ="webguync@gmail.com";
		$subject = "There has been a disturbance in the Force";
		$mailheaders .="Reply-To: $_POST['sender_email']\n";
//send the mail
		mail ($to, $subject, $msg, $mailheaders);
		//display confirmation to user
		echo "<p>mail has been sent!</p>";
} else if ($send =="no") {
		//print error messages
		echo "$name_err";
		echo "$email_err";
		echo "$message_err";
		echo "$form_block";
	}
}
?>
the error I now get is:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /hsphere/local/home/bruceg/inspired-evolution.com/Contact_Form_test.php on line 58


It looks like I am getting closer, but there still is a problem!

Posted: Thu Jul 28, 2005 1:47 am
by pilau
There is - you code doesn't reach line 58! 8O

Posted: Thu Jul 28, 2005 10:01 am
by bruceg
it does if I attach all of it...

Code: Select all

<!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" xml:lang="en-US" lang="en-US" dir="ltr">
<head>
<meta http-equiv="Content-type" content="text/html; charset=ISO-8859-1" />
<title>Form Test></title>
<?php require('includes/meta_tags.inc'); ?>
<link rel="stylesheet" href="Gilbert.css" type="text/css" media="screen, handheld" />
</head>
<body>
<!-- begin outer wrapper div --> 

<div id="wrapper_outer">
<a class="skiplink" href="#startcontent">Skip over navigation</a>
<!-- begin header div --> 
<div id="header"><?php require('images/Rotating_Banner/rotating_banner.php'); ?></div>
<!-- top nav list --> 
<div id="navcontainer">
<ul id="navlist">
<li ><a href="About_Me.php" title="you know you want to learn more about me">About Me</a></li>
<li><a href="Skillset.php" title="I've got skillz">Skill set</a></li>
<li><a href="Hireme.php" title="I can do wonders for your web presence">Hire Me</a></li>
<li><a href="Portfolio.php" title="web sites, graphics, newsletters">Portfolio</a></li>
<li><a href="Contact.php"  title="how to get in touch with me">Contact</a></li>
<li><a href="Resume.php"  title="my beautiful resume">R&eacute;sum&eacute;</a></li>
<li><a href="Blog.php"  title="My musings on everyday life" >Blog</a></li>
<li><a href="RSS.php"  title="Syndication that is really simple" >RSS</a></li>

</ul>
</div>
<!-- inner content wrapper div --> 
<div id="wrapper_inner">
<!-- breadcrumbs div --> 
<div id="breadcrumbs"><a href="index.php" title="home link" accesskey="1">Home</a>  > >  Contact</div>
<!-- text div --> 
<div id="main_content">
<a name="startcontent" id="startcontent"></a>
<h1 title="Contact">Contact</h1>
<?
$form_block = "
<FORM METHOD=\"POST\" ACTION=\"$PHP_SELF\">
<p><strong>Your Name:</strong>< /br>
<INPUT type=\"text\" NAME=\"senders_name\" SIZE=30></p>
<p><strong><p><Your E-mail Address:</strong>< /br>
<INPUT type=\"text\" NAME=\"senders_email\" SIZE=30></p>
<p><strong>Message:</strong>< /br>
<TEXTAREA NAME=\"message\" COLS=30 ROWS=5 WRAP=virtual></TEXTAREA></p>
<INPUT type=\"hidden\" name=\"op\" value=\"ds\">
<p><INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Send this Form\"></p>
</FORM>";

if ($_POST['op'] !="ds") {
// they need to see the form
echo "$form_block";
} else if ($_POST['op'] =="ds") {
//check value of $_POST['sender name']
if ($_POST['sender_name'] =="") {
			$name_err = "<font color=red>Please enter your name!</font>< /br>";
			$send ="no";
}
//check value of $_POST['sender_email']
if ($POST['sender_email'] =="") {
		$email_err ="<font color=red>Please enter your email address!</font>< /br>";
		$send= "no";
}
//check value of $_POST['message']
if ($POST['message'] =="") {
		$message_err = "<font color=red>Please enter a message!</font>< /br>";
		$send ="no";
}
if ($send !="no") {
//it's o.k to send, so build the mail
		$msg ="E-MAIL SENT FROM WWW SITE\n";
		$msg .="Senders Name:		$POST['senders_name']\n";
		$msg .="Senders E-MAIL:			$POST['senders_email']\n";
		$msg .="Senders Name:		$POST['message']\n\n";
		$to ="webguync@gmail.com";
		$subject = "There has been a disturbance in the Force";
		$mailheaders .="Reply-To: $_POST['sender_email']\n";
//send the mail
		mail ($to, $subject, $msg, $mailheaders);
		//display confirmation to user
		echo "<p>mail has been sent!</p>";
} else if ($send =="no") {
		//print error messages
		echo "$name_err";
		echo "$email_err";
		echo "$message_err";
		echo "$form_block";
	}
}
?>



	
<p><span class="red">*</span> indicates a required field.</p>
<p></p>
<p></p>
<?php require('includes/bottom_links.inc'); ?></div>

<!-- begin footer --> 
<div id="footer">
<?php require('includes/footer.inc'); ?>
<span class="date"><?
$last_modified = filemtime("index.php");
print("Last Modified ");
print(date("m/j/y h:i", $last_modified));
?></span>
</div>
<p class="footertag">Inspired-Evolution - Web Site: Design, Development, Marketing for Raleigh/Durham, Chapel Hill, Cary North Carolina.</p>

</div>
</div>

</body>

</html>

Posted: Thu Jul 28, 2005 10:12 am
by theda
Oh, by the way, isn't it <br /> not < /br>? You might want to fix that...

Line 58 -> put a space between the = and the "

Posted: Thu Jul 28, 2005 2:14 pm
by bruceg
thanks about the <br /> correction.

I show line 58 as being this (below)

$send = "no";

and I added the pace, but still getting the same error. Is that the line you were referring to also?

Posted: Thu Jul 28, 2005 2:37 pm
by nielsene
On line 38 and 103 you are using a short tag to open a PHP block. When I changed them to <?php it works on my server, but I'm not sure if thats the root problem or not.

Posted: Thu Jul 28, 2005 3:00 pm
by bruceg
I have fixed the errors, but my form isn't working. You get the red errors nomatter if you enter the info or not.

check it out at:
http://www.inspired-evolution.com/Contact_Form_test.php

my code now is

Code: Select all

<?
$form_block = "
<FORM METHOD=\"POST\" ACTION=\"$PHP_SELF\">
<p><strong>Your Name:</strong></p>
<INPUT type=\"text\" NAME=\"senders_name\" SIZE=30></p>
<p><strong>Your E-mail Address:</strong><p>
<INPUT type=\"text\" NAME=\"senders_email\" SIZE=30></p>
<p><strong>Message:</strong></p>
<TEXTAREA NAME=\"message\" COLS=30 ROWS=5 WRAP=virtual></TEXTAREA></p>
<INPUT type=\"hidden\" name=\"op\" value=\"ds\">
<p><INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Send this Form\"></p>
</FORM>";

if ($_POST['op'] !="ds") {
// they need to see the form
echo "$form_block";
} else if ($_POST['op'] =="ds") {
//check value of $_POST['sender name']
if ($_POST['sender_name'] =="") {
			$name_err = "<font color=red>Please enter your name!</font><br />";
			$send = "no";
}
//check value of $_POST['sender_email']
if ($POST['sender_email'] =="") {
		$email_err ="<font color=red>Please enter your email address!</font><br />";
		$send= "no";
}
//check value of $_POST['message']
if ($POST['message'] =="") {
		$message_err = "<font color=red>Please enter a message!</font><br />";
		$send ="no";
}
if ($send !="no") {
//it's o.k to send, so build the mail
		$msg ="E-MAIL SENT FROM WWW SITE\n";
		$msg .="Senders Name:		$POST{['senders_name']}\n";
		$msg .="Senders E-MAIL:			$POST{['senders_email']}\n";
		$msg .="Senders Name:		{$POST['message']}\n\n";
		$to ="webguync@gmail.com";
		$subject = "There has been a disturbance in the Force";
		$mailheaders .="Reply-To: {$_POST['sender_email']}\n";
//send the mail
		mail ($to, $subject, $msg, $mailheaders);
		//display confirmation to user
		echo "<p>mail has been sent!</p>";
} else if ($send =="no") {
		//print error messages
		echo "$name_err";
		echo "$email_err";
		echo "$message_err";
		echo "$form_block";
	}
}
?>

Posted: Thu Jul 28, 2005 3:04 pm
by nielsene
Well I still see the short tags, but I doubt that's the problem.

Try adding a
print_r($_POST) before the if's to see what data the form is getting.

You should also raise your error reporting to E_ALL.

You should add tests like

Code: Select all

if (isset($_POST["op"]) && $_POST["op"]=="ds") {

Posted: Thu Jul 28, 2005 3:33 pm
by bruceg
eric,

would I add the if (isset($_POST["op"]) && $_POST["op"]=="ds") {

to the code I already have or replace one of the current if statments with that?

eg: replace if ($_POST['op'] !="ds") {