need help with email script unexpect t_string error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpnewb1
Forum Newbie
Posts: 16
Joined: Fri Dec 01, 2006 4:52 pm

need help with email script unexpect t_string error

Post by phpnewb1 »

Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


the code should be correct. When I try and view the page this script is on 1) my html that is not part of this script does not load 2) the script doesn't load and i get an error that says unexpected t_String error
it says there is a problem with this line

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

Code: Select all

<?
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>

Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

EDIT | Ok, I felt like cleaning your code (I don't why). Anyway, try this and see what happens...

Code: Select all

<?php
error_reporting(E_ALL);
$form_block= '
<form action="' . basename($_SERVER['SCRIPT_FILENAME'] . '" 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>
		<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') {
	if ( 
		empty($_POST['firstname']) || 
		empty($_POST['lastname']) || 
		empty($_POST['email']) || 
		empty($_POST['commentsorquestions'])
		) {
		// a field is not filled in
		$field_err = "<font color=red>Please fill in all required fields!</font>";
		$send = "no";
	}
} else {
	echo $form_string;
}

if ($send == 'no') {
	echo "$field_err";
	echo "$form_block";
} else {
	$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>';
}
?>
</body>
</html>
phpnewb1
Forum Newbie
Posts: 16
Joined: Fri Dec 01, 2006 4:52 pm

Post by phpnewb1 »

Thank you, I will try to properly post code from now on. When I tested this code, it returned an error on line 40 an unexpected ';.' Not sure why though. But the real problem is when the script loads it sends a blank email and none of my html before the script is loaded.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Try this one...

Code: Select all

<?php
error_reporting(E_ALL);
$form_block= '<form action="' . basename($_SERVER['SCRIPT_FILENAME'] . '" 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>
		<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') {
	if ( 
		empty($_POST['firstname']) || 
		empty($_POST['lastname']) || 
		empty($_POST['email']) || 
		empty($_POST['commentsorquestions'])
		) {
		// a field is not filled in
		$field_err = "<font color=red>Please fill in all required fields!</font>";
		$send = "no";
	}
} else {
	echo $form_string;
}

if ($send == 'no') {
	echo $field_err;
	echo $form_block;
} else {
	// you need to tell the script what the 'to' address is here...
	$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>';
}
?>
</body>
</html>
User avatar
Fractal
Forum Commoner
Posts: 54
Joined: Tue Aug 16, 2005 1:28 pm

Post by Fractal »

Everah, you forgot the ) on basename($_SERVER['SCRIPT_FILENAME']) in both.

EDIT: Which looks like the cause of the "unexpected" error
EDIT2: And as a side note, this won't cause an error unless you're trying to have Valid HTML.

In the echo string at the end of the page, you missed the / on the closing paragraph tag.
phpnewb1
Forum Newbie
Posts: 16
Joined: Fri Dec 01, 2006 4:52 pm

Post by phpnewb1 »

I am still getting the same error. About 10 people have already offered suggestions but none of them seem to work.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

10 people ...I don't won't to image what the code looks like now. Please post your current version again.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Fractal wrote:Everah, you forgot the ) on basename($_SERVER['SCRIPT_FILENAME']) in both.
I swear to God I have been doing that all day. I must have ran into that 10 times today in my own coding. Man, I feel suck today.
phpnewb1
Forum Newbie
Posts: 16
Joined: Fri Dec 01, 2006 4:52 pm

Post by phpnewb1 »

i figured out why it wasn't working. I didn't update the files in my htdocs folder so I was viewing old code.
Post Reply