PHP Newbie Parse Error Help

Hitsurume

n00b
Joined
Apr 19, 2005
Messages
11
Hi Everyone

I just picked up a PHP book and have been going through it and trying to learn. When I tried this php script that the book had with a little of my own editing I got the following parse error:

Parse error: parse error, unexpected T_STRING in /home/.instructor/test.php on line 17

Heres my code:

Code:
<?php # Script 3.12 - register.php

$page_title = 'Test!';

if (isset($_POST['submit'])) {

		if (strlen($_POST['email']) > 0) {
			$email = TRUE;
		} else {
			$email = FALSE;
			echo '<p> you forgot to enter your email! </p>';
		}


		if ($email) {

			mail ('bleh@gmail.com', 'New Email Request', 'Heres the email: ($_POST['email'])' );


			echo '<p> Thank you for registering your email Address! </p>';

					}

		else {
			echo '<p> Please go back and try again</p>'
	 		}


	}


else

	{

?>

		<form action="<?php echo $_Server['PHP_SELF']; ?>" method="post">
		<fieldset>
		<p><b>Email:</b> <input type="text" name="email" size="40" maxlength="60" /></p>
		</fieldset>
		<div align="center><input type="submit" name="submit" value="Submit Information" /></div>
		</form>

	}

Line 17 is where the: mail ( function is called

Hopefully someone can point out what i'm doing wrong

Thanks!
 
Just briefly looked at your code and the only thing that really jumped out at me was that you need to either put double quotation " around the word email like so...$_POST["email"] or leave the single quotes around that but put double quotes around all the other stuff likes so...

mail ("bleh@gmail.com", "New Email Request", "Heres the email: ($_POST['email'])" );

Try that and see how it goes.
 
Paithar said:
mail ("bleh@gmail.com", "New Email Request", "Heres the email: ($_POST['email'])" );
this is not entirely correct. the problem was with the quoting, but you must understand how PHP substitues variables within strings. you can either do
mail ("bleh@gmail.com", "New Email Request", "Heres the email: ($_POST)" );
or
mail ("bleh@gmail.com", "New Email Request", "Heres the email: ({$_POST['email']})" );

read this
[url]http://us2.php.net/manual/en/language.types.string.php[/url]
scroll down to the variable parsing section
 
Thank you guys!

I managed to atleast get it running, although I run into some weird parse problems at the end of the code during:

Code:
	else

	{
?>


		<form action="<?php echo $_Server['PHP_SELF']; ?>" method="post">
		<fieldset>
		<p><b>Email:</b> <input type="text" name="email" size="40" maxlength="60" /></p>
		</fieldset>
		<div align="center"><input type="submit" name="submit" value="Submit Information" /></div>
		</form>


	}

When I do this, the html part shows up, but above it, ELSE { is printed, and on the bottom, } is also printed, anyone know why is that?

Also, I'm not sure if my host has the php mail function running? When I tried to type in something to send to my email, I get the Thank you message, but I never get an email in my account. I'm using dreamhost so if anyone has any ideas please let me know

Thanks again!
 
Well that's because you're closing the PHP script with ?>. Consider using this instead:
Code:
	else

	{
		echo "<form action=\"<?php echo $_Server['PHP_SELF']; ?>\" method=\"post\">";
		echo "<fieldset>";
		echo "<p><b>Email:</b> <input type=\"text\" name=\"email\" size=\"40\" maxlength=\"60\" /></p>";
		echo "</fieldset>";
		echo "<div align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Submit Information\" /></div>";
		echo "</form>";
      }
?>

The echos will still spit out your HTML and the PHP script is closed properly. You can also get rid of the echos and put a . between each string to add them all up on one line but this makes it harder to read. Note, you MUST put the \ before each set of " in your HTML to make the PHP parser skip that. Otherwise, PHP will think you want to end the string so a string like:

"I'm a "string" that can be used"

turns into:

"I'm a " string " that can be used"

Blue is a valid string but the word string between the " would be look at like a command which of course would cause the script to error.

As far as the email, there really isn't anything like php mail per say. You have to configure PHP to send mail via an external app like sendmail.
 
The_Mage18 said:
Well that's because you're closing the PHP script with ?>. Consider using this instead:
Code:
	else

	{
		echo "<form action=\"<?php echo $_Server['PHP_SELF']; ?>\" method=\"post\">";
		echo "<fieldset>";
		echo "<p><b>Email:</b> <input type=\"text\" name=\"email\" size=\"40\" maxlength=\"60\" /></p>";
		echo "</fieldset>";
		echo "<div align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Submit Information\" /></div>";
		echo "</form>";
      }
?>

The echos will still spit out your HTML and the PHP script is closed properly. You can also get rid of the echos and put a . between each string to add them all up on one line but this makes it harder to read. Note, you MUST put the \ before each set of " in your HTML to make the PHP parser skip that. Otherwise, PHP will think you want to end the string so a string like:

"I'm a "string" that can be used"

turns into:

"I'm a " string " that can be used"

Blue is a valid string but the word string between the " would be look at like a command which of course would cause the script to error.

As far as the email, there really isn't anything like php mail per say. You have to configure PHP to send mail via an external app like sendmail.

Well, since there is more plain text output then script processing the 'more correct' (heh) way to do it would be to jump out of php and back in, just like he is attempting to do.

The problem with the original code (although correct in logic) is that he is not switching back to php after the HTML is sent.

OP, try this
Code:
	else

	{
?>


		<form action="<?php echo $_Server['PHP_SELF']; ?>" method="post">
		<fieldset>
		<p><b>Email:</b> <input type="text" name="email" size="40" maxlength="60" /></p>
		</fieldset>
		<div align="center"><input type="submit" name="submit" value="Submit Information" /></div>
		</form>

<?php
	}

// ... rest of php script, if any. Otherwise close php again with ?>
 
Tweakin said:
Well, since there is more plain text output then script processing the 'more correct' (heh) way to do it would be to jump out of php and back in, just like he is attempting to do.

The problem with the original code (although correct in logic) is that he is not switching back to php after the HTML is sent.

Now see this is where I would smack you. Not because that may not be the accepted format but because it makes debuging a mild pain in the ass. Then again, I'm all about making my code easy to read and understand, not obfuscated monkey turds like Windows code. :D
 
Back
Top