Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jordanste

macrumors member
Original poster
Feb 25, 2006
83
0
hey folks,

im trying to write a mailing list singup form with two required fields "email" and "zipcode". however, to make sure that they are not entered in as blank i figured i would write an if statement to determine whether or not there is a value for each field, this is what ive got:

PHP:
//...

	if ($register=='yes')
		{

		if (!isset($_POST['email']) || empty($_POST['email']))
			{
			echo "<table id="mailing_list_form" width=250><tr><td><fieldset><legend><b>Mailing List!</b></legend>"; //line 16
			echo "<form action='http://www.#.net/mailinglist/verify.php?register=yes' method=POST>";
			echo "<table><tr><td><label>e-mail:</td><td><input type=text name='email' size=20 maxlength=40 value="$email"></label><span class=error>**email is a required field</span></td></tr>";
			echo "<tr><td><label>zipcode:</td><td><input type=text name='zipcode' size=5 maxlength=5 value="$zipcode"></label></td></tr>";
			echo "<tr><td><input type="submit" value="register"></td></tr></table>";
			echo "</form></fieldset></td></tr></table>";
			}

		if (!isset($_POST['zipcode']) || empty($_POST['zipcode']))
			{
			echo "<table id="mailing_list_form" width=250><tr><td><fieldset><legend><b>Mailing List!</b></legend>";
			echo "<form action='http://www.#.net/mailinglist/verify.php?register=yes' method=POST>";
			echo "<table><tr><td><label>e-mail:</td><td><input type=text name='email' size=20 maxlength=40 value="$email"></label></td></tr>";
			echo "<tr><td><label>zipcode:</td><td><input type=text name='zipcode' size=5 maxlength=5 value="$zipcode"></label><span class=error>**zipcode is a required field</span></td></tr>";
			echo "<tr><td><input type="submit" value="register"></td></tr></table>";
			echo "</form></fieldset></td></tr></table>";
			}

		$query="INSERT INTO mailing_list (id,email,zipcode,regdate) VALUES (NULL , '$email', '$zipcode', '$date')";
		mysql_query($query) or die('<p>Error, insert query failed</p>');
	
		mysql_close($con);

		echo "<p>Thank you for registering!</p>";


the problem is that im getting a parse error "syntax error, unexpected T_STRING, expecting ',' or ';' ... on line 16." im not sure where the problem is, but i imagine it has to do with all of those echos
is there a better way to redisplay the form if a field is empty? i.e. instead of using all of those echo echo echo echo...?

thanks.
 
i believe the problem lies here:

echo "<table id="mailing_list_form"

you need to use single quotes, when in double quotes, here and countinued in your echos the double quotes break the string, causing a parse error. I dont know if im explaining it correctly but replace the double quotes inside the echo string with single quotes should fix your problem.
 
Replacing the double quotes with single quotes is one solution like web_god61 said.

The double quotes interrupt the literal string you are trying to print out. To actually print out double quotes, you must add slashes to the quotes. They're called escape characters if you want to google more about them.

You'll have to add slashes to the rest of your script, but it's done as follows:

PHP:
echo "<table id=\"mailing_list_form\" width=250><tr><td><fieldset><legend><b>Mailing List!</b></legend>";
 
Replacing the double quotes with single quotes is one solution like web_god61 said.

The double quotes interrupt the literal string you are trying to print out. To actually print out double quotes, you must add slashes to the quotes. They're called escape characters if you want to google more about them.

You'll have to add slashes to the rest of your script, but it's done as follows:

PHP:
echo "<table id=\"mailing_list_form\" width=250><tr><td><fieldset><legend><b>Mailing List!</b></legend>";

dont forget if its got a $something to use it like this

example
PHP:
echo "<td>".$error."</td>";
 
dont forget if its got a $something to use it like this

example
PHP:
echo "<td>".$error."</td>";

Actually, in PHP, you can use the variables inside double quotes, though I'm never completely comfortable doing it so generally do it your suggested way.
 
Here's an alternative, faster and easier method to output all that html:

PHP:
<?php

        if (!isset($_POST['email']) || empty($_POST['email']))
            {
?>
<table id="mailing_list_form width=250><tr><td><fieldset><legend><b>Mailing List!</b></legend> 
<form action='http://www.jordanstephens.net/mailinglist/verify.php?register=yes' method=POST>
<table><tr><td><label>e-mail:</td><td><input type=text name='email' size=20 maxlength=40 value="<?=$email ?>"></label><span class=error>**email is a required field</span></td></tr>
<tr><td><label>zipcode:</td><td><input type=text name='zipcode' size=5 maxlength=5 value="<?=$zipcode ?>"></label></td></tr>
<tr><td><input type="submit" value="register"></td></tr></table>
</form></fieldset></td></tr></table>

<?php
            }

        if (!isset($_POST['zipcode']) || empty($_POST['zipcode']))
            {
?>
<table id="mailing_list_form" width=250><tr><td><fieldset><legend><b>Mailing List!</b></legend>
<form action='http://www.jordanstephens.net/mailinglist/verify.php?register=yes' method=POST>
<table><tr><td><label>e-mail:</td><td><input type=text name='email' size=20 maxlength=40 value="<?=$email ?>"></label></td></tr>
<tr><td><label>zipcode:</td><td><input type=text name='zipcode' size=5 maxlength=5 value="<?=$zipcode ?>"></label><span class=error>**zipcode is a required field</span></td></tr>
<tr><td><input type="submit" value="register"></td></tr></table>
</form></fieldset></td></tr></table>

<?php
            }

When outputting big chunks of html, it's easier+faster to just write it as html and not have php echo out so much of it. It also helps you because you don't have to deal with escaping single/double quotes. Any variable you wanna insert into the html chunks, you can use the quickhand echo method which is basically
PHP:
<?=$variable ?>

No need to use a semi-colon. Look in the value="" section of the html above to see how to apply the quickhand method. Hope this tip helps in any way.
 
awesome, hey thanks everybody for the help. i got it working the way i want it to and i learned alot about php in the process. im still really new to this so thanks again.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.