Ruvvan
03-02-09, 20:15
I've been trying to piece together a site for my dad with what little PHP I remember. I'm trying to implement a form that checks itself for errors and null input entries. However, for some reason, the $_SERVER['PHP_SELF'] variable doesn't seem to like me very much, as I continue getting an error when I set the form's action to it.
I get this error:
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\houses.php on line 14
When trying to use this code:
<html><head><title>Yea</title></head>
<body>
<?php $fname = $_POST['fname'];
$lname = $_POST['lname'];
$padd = $_POST['padd'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$phone2 = $_POST['phone2'];
$phone3 = $_POST['phone3'];
$form ="<form action=\"$_SERVER['PHP_SELF']\" method=\"post\">";
$form.="<font size=\"-1\" color=\"gray\"><font color=\"red\">*</font> denotes a required field.</font>
<table border=\"0\"><tr><td colspan=\"1\">First Name<font color=\"Red\" size=\"-1\">*</font></td>
<td colspan=\"1\"><input type=\"text\" name=\"fname\" size=\"30\" value=\"$fname\"></td></tr>";
$form.="<tr><td colspan=\"1\">Last Name<font color=\"Red\" size=\"-1\">*</font></td>
<td colspan=\"1\"><input type=\"text\" name=\"lname\" size=\"30\" value=\"$lname\"></td></tr>";
$form.="<tr><td colspan=\"1\">Property Address<font color=\"Red\" size=\"-1\">*</font></td>
<td colspan=\"1\"><input type=\"text\" name=\"padd\" size=\"30\" value=\"$padd\"></td></tr>";
$form.="<tr><td colspan=\"1\">City<font color=\"Red\" size=\"-1\">*</font></td>
<td colspan=\"1\"><input type=\"text\" name=\"city\" size=\"30\" value=\"$city\"></td></tr>";
$form.="<tr><td colspan=\"1\">State<font color=\"Red\" size=\"-1\">*</font></td>
<td colspan=\"1\"><input type=\"text\" name=\"state\" size=\"30\" value=\"$state\"></td></tr>";
$form.="<tr><td colspan=\"1\">Zip Code<font color=\"Red\" size=\"-1\">*</font></td>
<td colspan=\"1\"><input type=\"text\" name=\"zip\" size=\"30\" value=\"$zip\"></td></tr>";
$form.="<tr><td colspan=\"1\">E-mail Address<font color=\"Red\" size=\"-1\">*</font></td>
<td colspan=\"1\"><input type=\"text\" name=\"email\" size=\"30\" value=\"$email\"></td></tr>";
$form.="<tr><td colspan=\"1\">Phone Number<font color=\"Red\" size=\"-1\">*</font></td>
<td colspan=\"1\"><input type=\"text\" name=\"phone\" size=\"5\" value=\"$phone\">-
<input type=\"text\" name=\"phone2\" size=\"5\" value=\"$phone2\">-
<input type=\"text\" name=\"phone3\" size=\"5\" value=\"$phone3\">
</td></tr>";
$form.="<tr><td><div align=\"center\"><input type=\"submit\" name=\"sent\" value=\"Get your free cash offer\"></div></td></tr></table></form>";
if($sent)
{ $valid=true;
if( !$fname )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter your first name.</font></td></tr>";
$valid = false;
}
########################################################################
if( !$lname )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter your last name.</font></td></tr>";
$valid = false;
}
########################################################################
if( !$padd )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter the property address.</font></td></tr>";
$valid = false;
}
########################################################################
if( !$city )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter a city name.</font></td></tr>";
$valid = false;
}
########################################################################
if( !$state )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter a state.</font></td></tr>";
$valid = false;
}
########################################################################
if( !$zip )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter a zip code.</font></td></tr>";
$valid = false;
}
else
{if( strlen( $zip ) != 5 )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter a valid zip code.</font></td></tr>";
$valid = false;
}}
########################################################################
if( !$email )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter your e-mail address.</font></td></tr>";
$valid = false;
}
else
{
$useraddr = trim( $useraddr );
$_name = "/^[-!#$%&\'*+\\./\0-9=?A-Z^_`{|}~]+";
$_host = "([-0-9A-Z]+\.)+";
$tlds = "([0-9A-Z]){2,4}$/i";
if( !pregmatch($_name."@".$_host.$_tlds,$useraddr) )
{ $errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter a valid e-mail address.</font></td></tr>";
}}
########################################################################
if( !$phone or !$phone2 or !$phone3 )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter your phone number.</font></td></tr>";
$valid = false;
}
else
{if( strlen( $phone ) != 3 or strlen( $phone2 ) != 3 or strlen( $phone3 ) != 4 )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter a valid phone number.</font></td></tr>";
$valid = false;
}}
########################################################################
}
if( $valid != true )
{
echo( "<table border=\"0\">$errmsg</table><br><br>" );
echo( $form );
}
else
{ $to = "ruvvan@gmail.com";
$re = "Property information from $fname $lname";
$msg = $fname;
$headers = "MIME-Version: 1.0\r\n";
$headers.= "Content-type: text/html;";
$headers.= " charset=iso-8859-1\r\n";
$headers.= "From: $useraddr \r\n";
if( mail( $to, $re, $msg, $headers ) )
{echo( "Thanks for your submission." ); }
}
?>
</body>
</html>
Line 14, the line it has a problem with, is
$form ="<form action=\"$_SERVER['PHP_SELF']\" method=\"post\">";
I set up Apache and PHP by myself; I'm not sure if this could be stemming from some error I made in installation.
If someone could shed some light on this, it would be greatly appreciated. :)
I get this error:
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\houses.php on line 14
When trying to use this code:
<html><head><title>Yea</title></head>
<body>
<?php $fname = $_POST['fname'];
$lname = $_POST['lname'];
$padd = $_POST['padd'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$phone2 = $_POST['phone2'];
$phone3 = $_POST['phone3'];
$form ="<form action=\"$_SERVER['PHP_SELF']\" method=\"post\">";
$form.="<font size=\"-1\" color=\"gray\"><font color=\"red\">*</font> denotes a required field.</font>
<table border=\"0\"><tr><td colspan=\"1\">First Name<font color=\"Red\" size=\"-1\">*</font></td>
<td colspan=\"1\"><input type=\"text\" name=\"fname\" size=\"30\" value=\"$fname\"></td></tr>";
$form.="<tr><td colspan=\"1\">Last Name<font color=\"Red\" size=\"-1\">*</font></td>
<td colspan=\"1\"><input type=\"text\" name=\"lname\" size=\"30\" value=\"$lname\"></td></tr>";
$form.="<tr><td colspan=\"1\">Property Address<font color=\"Red\" size=\"-1\">*</font></td>
<td colspan=\"1\"><input type=\"text\" name=\"padd\" size=\"30\" value=\"$padd\"></td></tr>";
$form.="<tr><td colspan=\"1\">City<font color=\"Red\" size=\"-1\">*</font></td>
<td colspan=\"1\"><input type=\"text\" name=\"city\" size=\"30\" value=\"$city\"></td></tr>";
$form.="<tr><td colspan=\"1\">State<font color=\"Red\" size=\"-1\">*</font></td>
<td colspan=\"1\"><input type=\"text\" name=\"state\" size=\"30\" value=\"$state\"></td></tr>";
$form.="<tr><td colspan=\"1\">Zip Code<font color=\"Red\" size=\"-1\">*</font></td>
<td colspan=\"1\"><input type=\"text\" name=\"zip\" size=\"30\" value=\"$zip\"></td></tr>";
$form.="<tr><td colspan=\"1\">E-mail Address<font color=\"Red\" size=\"-1\">*</font></td>
<td colspan=\"1\"><input type=\"text\" name=\"email\" size=\"30\" value=\"$email\"></td></tr>";
$form.="<tr><td colspan=\"1\">Phone Number<font color=\"Red\" size=\"-1\">*</font></td>
<td colspan=\"1\"><input type=\"text\" name=\"phone\" size=\"5\" value=\"$phone\">-
<input type=\"text\" name=\"phone2\" size=\"5\" value=\"$phone2\">-
<input type=\"text\" name=\"phone3\" size=\"5\" value=\"$phone3\">
</td></tr>";
$form.="<tr><td><div align=\"center\"><input type=\"submit\" name=\"sent\" value=\"Get your free cash offer\"></div></td></tr></table></form>";
if($sent)
{ $valid=true;
if( !$fname )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter your first name.</font></td></tr>";
$valid = false;
}
########################################################################
if( !$lname )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter your last name.</font></td></tr>";
$valid = false;
}
########################################################################
if( !$padd )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter the property address.</font></td></tr>";
$valid = false;
}
########################################################################
if( !$city )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter a city name.</font></td></tr>";
$valid = false;
}
########################################################################
if( !$state )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter a state.</font></td></tr>";
$valid = false;
}
########################################################################
if( !$zip )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter a zip code.</font></td></tr>";
$valid = false;
}
else
{if( strlen( $zip ) != 5 )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter a valid zip code.</font></td></tr>";
$valid = false;
}}
########################################################################
if( !$email )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter your e-mail address.</font></td></tr>";
$valid = false;
}
else
{
$useraddr = trim( $useraddr );
$_name = "/^[-!#$%&\'*+\\./\0-9=?A-Z^_`{|}~]+";
$_host = "([-0-9A-Z]+\.)+";
$tlds = "([0-9A-Z]){2,4}$/i";
if( !pregmatch($_name."@".$_host.$_tlds,$useraddr) )
{ $errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter a valid e-mail address.</font></td></tr>";
}}
########################################################################
if( !$phone or !$phone2 or !$phone3 )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter your phone number.</font></td></tr>";
$valid = false;
}
else
{if( strlen( $phone ) != 3 or strlen( $phone2 ) != 3 or strlen( $phone3 ) != 4 )
{
$errmsg .= "<tr><td colspan=\"1\"><font color=\"red\">Please enter a valid phone number.</font></td></tr>";
$valid = false;
}}
########################################################################
}
if( $valid != true )
{
echo( "<table border=\"0\">$errmsg</table><br><br>" );
echo( $form );
}
else
{ $to = "ruvvan@gmail.com";
$re = "Property information from $fname $lname";
$msg = $fname;
$headers = "MIME-Version: 1.0\r\n";
$headers.= "Content-type: text/html;";
$headers.= " charset=iso-8859-1\r\n";
$headers.= "From: $useraddr \r\n";
if( mail( $to, $re, $msg, $headers ) )
{echo( "Thanks for your submission." ); }
}
?>
</body>
</html>
Line 14, the line it has a problem with, is
$form ="<form action=\"$_SERVER['PHP_SELF']\" method=\"post\">";
I set up Apache and PHP by myself; I'm not sure if this could be stemming from some error I made in installation.
If someone could shed some light on this, it would be greatly appreciated. :)