...

View Full Version : need help with email script unexpected t_string error



phpnewb
11-30-2006, 03:52 AM
okay i am trying to write an email script and i am getting a parse error on this line i believe

else if ($_POST[x] == "y") {

i believe it is correct but here is the code for the form

<?
$form_block= "
<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">

<table border=0 align=center>
<tr>
<td align=left>
<p>First Name*: <input type=\"text\" name=\"firstname\" size=30 maxlength=25></p>
</td>
<tr>
<td align=left>
<p>Last Name*: <input type=\"text\" name=\"lastname\" size=30 maxlength=25></p>
</td>
</tr>
<tr>
<td align=left>
<p>Email address*: <input type=\"text\" name=\"email\" size=27 maxlength=25></p>
</td>
</tr>
<tr>
<td align=center>
<textarea rows=15 cols=45 name=\"commentsorquestions\">
Please include any comments or questions here.*
</textarea>
</td>
</tr>
<tr>
<td align=center>
<input type=\"hidden\" name=\"x\" value=\"y\">
<p><input type=\"submit\" value=\"Send\">
<input type=\"reset\" value=\"Clear\"></p></td>
</tr>
<tr>
<td align=left>*Indicates required field
</td>
</tr>
</table>
</form>";
if ($_POST[x] != "y") {
echo "$form_block";
}
else if ($_POST[x] == "y") {
if ($_POST[firstname] == "") || ($_POST[lastname] == "") || ($_POST[email] == "") || ($_POST[commentsorquestions) == ""){
// a field is not filled in
$field_err = "<font color=red>Please fill in all required fields!</font>";
$send= "no";
}
}
if ($send != "no"){
$to = "";
$subject = "Feedback";
$msg .= "Sender's First Name: $_POST[firstname]\n";
$msg .= "Sender's Last Name: $_POST[lastname]\n";
$msg .= "Email: $_POST[email]\n";
$msg .= "Message: $_POST[commentsorquestions]\n";
mail($to, $subject, $msg)
echo "<P>Your message has been sent. Thank you for your feedback.<P>";
}
else if ($send == "no") {
echo "$field_err";
echo "$form_block";
}
}
?>
</body>
</html>

any help would be much appreciated

chump2877
11-30-2006, 05:10 AM
Please use
tags for code...try this:
[PHP]
if ($_POST[x] != "y") {
echo $form_block;
}

Fou-Lu
11-30-2006, 05:23 AM
Ok mate.
1. Your mail() function needs to have a terminating semi-colon.
2. very bottom:
else if ($send == "no") {
echo "$field_err";
echo "$form_block";
}
}
One to many closing braces. Remove it.
3. The boolean logic is flawed (set up incorrectly). Use:
(($_POST[firstname] == "") || ($_POST[lastname] == "") || ($_POST[email] == "") || ($_POST[commentsorquestions] == ""))
Added the surrounding brackets to the entirety, and changed the commentsorquestions key to be inclosed in square brackets ([]), as it was closed with a regular bracket.

Cheers!

phpnewb
11-30-2006, 01:01 PM
i changed everything but i am still getting the error on that specific line

chump2877
11-30-2006, 01:07 PM
elseif ($_POST[x] == "y") {

maybe?

phpnewb
11-30-2006, 01:13 PM
still getting the error and i have no clue why

phpnewb
12-01-2006, 01:13 AM
any help would be much appreciated

CFMaBiSmAd
12-01-2006, 01:16 AM
Since there were a number of changes, I recommend reposting the current code.

phpnewb
12-01-2006, 01:52 AM
<?
$form_block= "
<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">
<table border=0 align=center>
<tr>
<td align=left>
<p>First Name*: <input type=\"text\" name=\"firstname\" size=30 maxlength=25></p>
</td>
<tr>
<td align=left>
<p>Last Name*: <input type=\"text\" name=\"lastname\" size=30 maxlength=25></p>
</td>
</tr>
<tr>
<td align=left>
<p>Email address*: <input type=\"text\" name=\"email\" size=27 maxlength=25></p>
</td>
</tr>
<tr>
<td align=center>
<textarea rows=15 cols=45 name=\"commentsorquestions\">
Please include any comments or questions here.*
</textarea>
</td>
</tr>
<tr>
<td align=center>
<input type=\"hidden\" name=\"x\" value=\"y\">
<p><input type=\"submit\" value=\"Send\">
<input type=\"reset\" value=\"Clear\"></p></td>
</tr>
<tr>
<td align=left>*Indicates required field
</td>
</tr>
</table>
</form>";
if ($_POST[x] != "y") {
echo "$form_block";
}
else if ($_POST[x] == "y") {
if (($_POST[firstname] == "") || ($_POST[lastname] == "") || ($_POST[email] == "") || ($_POST[commentsorquestions] == "")){
// a field is not filled in
$field_err = "<font color=red>Please fill in all required fields!</font>";
$send= "no";
}
}
if ($send != "no"){
$to = "";
$subject = "Feedback";
$msg .= "Sender's First Name: $_POST[firstname]\n";
$msg .= "Sender's Last Name: $_POST[lastname]\n";
$msg .= "Email: $_POST[email]\n";
$msg .= "Message: $_POST[commentsorquestions]\n";
mail($to, $subject, $msg);
echo "<P>Your message has been sent. Thank you for your feedback.<P>";
}
else if ($send == "no") {
echo "$field_err";
echo "$form_block";
}

?>
</body>
</html>

Fou-Lu
12-01-2006, 03:06 AM
I see no syntax errors in the code you have currently posted.
Although I don't have the SMTP set up on my pc right now, it appears to be working correctly otherwise. Please post the error message that you are receiving.

phpnewb
12-01-2006, 03:15 AM
Parse error: parse error, unexpected T_STRING in "filename" on line 81
i think the error must be caused by my html coding before this script because i just tested the script and it works so i don't have a clue what could cause this but if anyone has any ideas i would much appreciate them

mz_
12-01-2006, 05:46 PM
I've tested the script. On it's own, just copy and paste the above it works ok besides saying "Your message has been sent. Thank you for your feedback." and sending a blank email on first load.

However when I add it to my html page with the rest of my content, I get
"Parse error: syntax error, unexpected T_STRING in <filename> on line 111"
which is the following line;

<form id="contact" action=\"$_SERVER[PHP_SELF]\" method=\"post\">

I've been looking for a good contact script, but everyone I have tried either don't validate, sends blank emails (on 1st load), or has errors. :(

No luck I guess.

edit:

If someone wants to see my entire contact.php page, and can fix it up for me, I'd be happy.
My server uses Apache 1.3.37 and PHP 4.4.4, Thanks.

CFMaBiSmAd
12-01-2006, 06:25 PM
Start by fixing the index errors in each occurrence of the following -

$_SERVER[PHP_SELF] -----------> $_SERVER['PHP_SELF']
$_POST[x] --------------------> $_POST['x']
$_POST[firstname] ------------> $_POST['firstname']
$_POST[lastname] -------------> $_POST['lasname']
$_POST[email] ----------------> $_POST['email']
$_POST[commentsorquestions] --> $_POST['commentsorquestions']The first one is probably what is causing the fatal error. Also, add the following after your opening php tag and see what you get -

error_reporting(E_ALL);The reason why it sends a blank message on the first pass through the code is that the following logic is flawed -

if ($send != "no"){$send is not set to any value in the code except within a conditional statement that is not set. Therefore, on the first pass through the code $send is a null and null is not equal to "no", so it executes the mail(....) function.

phpnewb
12-01-2006, 10:12 PM
Even after those changes i am still getting the same error. If this error does not occur within the script could something else be causing it. How do I keep it from sending an email on the first load.

<?
error_reporting(E_ALL);
$form_block= "
<form action=\"$_SERVER['PHP_SELF']\" method=\"post\">
<table border=0 align=center>
<tr>
<td align=left>
<p>First Name*: <input type=\"text\" name=\"firstname\" size=30 maxlength=25></p>
</td>
<tr>
<td align=left>
<p>Last Name*: <input type=\"text\" name=\"lastname\" size=30 maxlength=25></p>
</td>
</tr>
<tr>
<td align=left>
<p>Email address*: <input type=\"text\" name=\"email\" size=27 maxlength=25></p>
</td>
</tr>
<tr>
<td align=center>
<textarea rows=15 cols=45 name=\"commentsorquestions\">
Please include any comments or questions here.*
</textarea>
</td>
</tr>
<tr>
<td align=center>
<input type=\"hidden\" name=\"x\" value=\"y\">
<p><input type=\"submit\" value=\"Send\">
<input type=\"reset\" value=\"Clear\"></p></td>
</tr>
<tr>
<td align=left>*Indicates required field
</td>
</tr>
</table>
</form>";
if ($_POST['x'] != "y") {
echo "$form_block";
}
else if ($_POST['x'] == "y") {
if (($_POST['firstname'] == "") || ($_POST['lastname'] == "") || ($_POST['email'] == "") || ($_POST['commentsorquestions'] == "")){
// a field is not filled in
$field_err = "<font color=red>Please fill in all required fields!</font>";
$send = "no";
}
}
if ($send != "no"){
$to = "";
$subject = "Feedback";
$mailheaders = "From: My Web site <> \n";
$msg .= "Sender's First Name: $_POST[firstname]\n";
$msg .= "Sender's Last Name: $_POST[lastname]\n";
$msg .= "Email: $_POST[email]\n";
$msg .= "Message: $_POST[commentsorquestions]\n";
mail($to, $subject, $msg);
echo "<P>Your message has been sent. Thank you for your feedback.<P>";
}
else if ($send == "no") {
echo "$field_err";
echo "$form_block";
}

?>
</body>
</html>

this is the code after the previous changes

mz_
12-01-2006, 11:48 PM
I got it all but working except the mail send on first load. What can we do to fix that?

On first load, it shows the form, with "Your message has been sent. Thank you for your feedback."

Suggestions on what to add/change?

I use a style sheet on the form, but you'll get the basic idea. (just piece them together)



<?php
$form_block= "
<form id=\"contact\" action=\"$_SERVER[PHP_SELF]\" method=\"post\">
<div class=\"row1\">
<span class=\"formlabel\">Your name *</span>
<span class=\"forminput\"><input type=\"text\" name=\"firstname\" /></span>
</div>
<div class=\"row1\">
<span class=\"formlabel\">Your email address *</span>
<span class=\"forminput\"><input type=\"text\" name=\"email\" /></span>
</div>
<div class=\"row1\">
<span class=\"formlabel\">Your message *</span>
<span class=\"forminput\"><textarea cols=\"34\" rows=\"5\" name=\"commentsorquestions\" class=\"textarea\"></textarea></span>
</div>
<div class=\"spacer\">&nbsp;</div>
<div class=\"row1\">
<span class=\"formlabel\"></span>
<span class=\"forminput\"><input type=\"hidden\" name=\"x\" value=\"y\">
<p><input type=\"submit\" value=\"send\" class=\"submit\" /></p></span>
</div>
</form>";




if ($_POST['x'] != "y") {
echo $form_block;
}
else if ($_POST['x'] == "y") {
if (($_POST['firstname'] == "") || ($_POST['email'] == "") || ($_POST['commentsorquestions'] == "")){
// a field is not filled in
$field_err = "<p><font color=red>Please fill in all required fields!</p></font>";
$send = "no";
}
}
if ($send != "no") {
$to = "";
$subject = "Feedback";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$msg .= "Sender's First Name: $_POST[firstname]\n";
$msg .= "Email: $_POST[email]\n";
$msg .= "Message: $_POST[commentsorquestions]\n";
mail($to, $subject, $msg, $headers);
echo "<p>Your message has been sent. Thank you for your feedback.</p>";
}
else if ($send == "no") {
echo "$field_err";
echo "$form_block";
}
?>

phpnewb
12-02-2006, 12:18 AM
Thank you for the help but i am not sure if you understand the problem. My problem is that my script works fairly well. It does not show up with the unexpected t_string error by itself. The problem is that it does not work when I put into my document which contains html before it, mostly links and a banner. My html does not show up nor does my script and I recieve the unexpected t_string error.

mz_
12-02-2006, 12:24 AM
Well, if it's not within the script you keep posting, I would assume you need to post what the line is you keep getting the error on. Or post the HTML file itself, I could be wrong though.. but without seeing where your getting the error besides a 'line #' it's hard to help.

phpnewb
12-02-2006, 01:03 AM
ok here is the whole document.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<table border="1" width="90%" align="center">
<tr>
<td width="100%" colspan="3" align="center"><img src="banner.gif">

<table border="0" align="center">
<tr>
<td><a href="alternateindex.html"><img src="home.gif" alt="Home"></a></td>
<td><a href="forums.html"><img src="forums.gif" alt="Forums"></a></td>
<td><a href="news.html"><img src="news.gif" alt="News"></a></td>
<td><a href="search.html"><img src="search.gif" alt="Search"></a></td>
<td><a href="profile.html"><img src="profile.gif" alt="Profile"></a></td>
<td><a href="memberlist.html"><img src="memberlist.gif" alt="Memberlist"></a></td>
<td><a href="usergroups.html"><img src="usergroups.gif" alt="Usergroups"></a></td>
<td><a href="faq.html"><img src="faq.gif" alt="Faq"></a></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td><a href="staff.html"><img src="staff.gif" alt="Staff"></a></td>
<td><a href="contactus.html"><img src="contactus.gif" alt="Contact Us"></a></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</td>
</tr>
</table>
<center><h2>If you have questions or comments please tell us.</h2></center>
// email script
<?
error_reporting(E_ALL);
$form_block= "
<form id=\"contact\" action=\"$_SERVER['PHP_SELF']\" method=\"post\">
<table border=0 align=center>
<tr>
<td align=left>
<p>First Name*: <input type=\"text\" name=\"firstname\" size=30 maxlength=25></p>
</td>
<tr>
<td align=left>
<p>Last Name*: <input type=\"text\" name=\"lastname\" size=30 maxlength=25></p>
</td>
</tr>
<tr>
<td align=left>
<p>Email address*: <input type=\"text\" name=\"email\" size=27 maxlength=25></p>
</td>
</tr>
<tr>
<td align=center>
<textarea rows=15 cols=45 name=\"commentsorquestions\">
Please include any comments or questions here.*
</textarea>
</td>
</tr>
<tr>
<td align=center>
<input type=\"hidden\" name=\"x\" value=\"y\">
<p><input type=\"submit\" value=\"Send\">
<input type=\"reset\" value=\"Clear\"></p></td>
</tr>
<tr>
<td align=left>*Indicates required field
</td>
</tr>
</table>
</form>";
if ($_POST['x'] != "y") {
echo "$form_block";
}
else if ($_POST['x'] == "y") {
if (($_POST['firstname'] == "") || ($_POST['lastname'] == "") || ($_POST['email'] == "") || ($_POST['commentsorquestions'] == "")){
// a field is not filled in
$field_err = "<font color=red>Please fill in all required fields!</font>";
$send = "no";
}
}
if ($send != "no"){
$to = "";
$subject = "Feedback";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "r\n\" .
"X-Mailer: PHP/' . phpversion();
$msg .= "Sender's First Name: $_POST[firstname]\n";
$msg .= "Sender's Last Name: $_POST[lastname]\n";
$msg .= "Email: $_POST[email]\n";
$msg .= "Message: $_POST[commentsorquestions]\n";
mail($to, $subject, $msg, $headers);
echo "<P>Your message has been sent. Thank you for your feedback.<P>";
}
else if ($send == "no") {
echo "$field_err";
echo "$form_block";
}

?>
</body>
</html>

phpnewb
12-02-2006, 02:46 AM
that is most of my code all that is missing is some stuff in between <head> </head> and the meta data

Fou-Lu
12-02-2006, 04:15 AM
Ok, lets fix this up:

Remove the // Mail script. Thats in the html and will show.
Although non-indexed keys are not wise to leave, they do not create errors, only warnings. Change this:


<form id=\"contact\" action=\"$_SERVER['PHP_SELF']\" method=\"post\">

to


<form id=\"contact\" action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"post\">

Change your mail headers:


$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "r\n\" .
"X-Mailer: PHP/' . phpversion();

to


$headers = "From: " . $email .
"\r\nReply-To: " . $email .
"\r\nX-Mailer: PHP/" . phpversion();


Actually... lets just redo this a bit:
Didn't change the format but try this out:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<table border="1" width="90%" align="center">
<tr>
<td width="100%" colspan="3" align="center"><img src="banner.gif">

<table border="0" align="center">
<tr>
<td><a href="alternateindex.html"><img src="home.gif" alt="Home"></a></td>
<td><a href="forums.html"><img src="forums.gif" alt="Forums"></a></td>
<td><a href="news.html"><img src="news.gif" alt="News"></a></td>
<td><a href="search.html"><img src="search.gif" alt="Search"></a></td>
<td><a href="profile.html"><img src="profile.gif" alt="Profile"></a></td>
<td><a href="memberlist.html"><img src="memberlist.gif" alt="Memberlist"></a></td>
<td><a href="usergroups.html"><img src="usergroups.gif" alt="Usergroups"></a></td>
<td><a href="faq.html"><img src="faq.gif" alt="Faq"></a></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td><a href="staff.html"><img src="staff.gif" alt="Staff"></a></td>
<td><a href="contactus.html"><img src="contactus.gif" alt="Contact Us"></a></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</td>
</tr>
</table>
<center><h2>If you have questions or comments please tell us.</h2></center>
<?php
error_reporting(E_ALL);
$sent = 0;

if (isset($_POST['x']) && $_POST['x'] == 1)
{
if (empty($_POST['firstname']) || empty($_POST['lastname']) || empty($_POST['email']) || empty($_POST['commentsorquestions']))
{
// a field is not filled in
echo "<font color=red>Please fill in all required fields!</font>";
}
else
{
// Send the mail
$to = "";
$subject = "Feedback";
$headers = "From: " . $_POST['email'] .
"\r\nReply-To: " . $_POST['email'] .
"\r\nX-Mailer: PHP/" . phpversion();
$msg = "Sender's First Name: " . $_POST['firstname'] . "\n";
$msg .= "Sender's Last Name: " . $_POST['lastname'] . "\n";
$msg .= "Email: " . $_POST['email'] . "\n";
$msg .= "Message: " . $_POST['commentsorquestions'] . "\n";
mail($to, $subject, $msg, $headers);
echo "<P>Your message has been sent. Thank you for your feedback.<P>";
$sent = 1;
}
}

if ($sent == 0)
{
?>
<form id="contact" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<table border="0" align="center">
<tr>
<td align="left">
<p>First Name*: <input type="text" name="firstname" size="30" maxlength="25" /></p>
</td>
<tr>
<td align="left">
<p>Last Name*: <input type="text" name="lastname" size="30" maxlength="25" /></p>
</td>
</tr>
<tr>
<td align="left">
<p>Email address*: <input type="text" name="email" size="27" maxlength="25" /></p>
</td>
</tr>
<tr>
<td align=center>
<textarea rows="15" cols="45" name="commentsorquestions">
Please include any comments or questions here.*
</textarea>
</td>
</tr>
<tr>
<td align=center>
<input type="hidden" name="x" value="1" />
<p><input type="submit" value="Send" />
<input type="reset" value="Clear" /></p></td>
</tr>
<tr>
<td align=left>*Indicates required field
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
}
?>

Just a couple of fixes and changed the layout to make it a little bit smaller. Also, you should look at validating your entry a bit as well.

phpnewb
12-02-2006, 02:47 PM
Thx a whole lot for all of the help. It actually works now.

mz_
12-02-2006, 04:29 PM
Yes, Thanks. Works great, don't send blank emails now. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum