Problem with $_SERVER['PHP_SELF']

Discussion in 'Software' started by Ruvvan, Mar 2, 2009.

  1. Ruvvan

    Ruvvan Private E-2

    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:

    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. :)
     
  2. morfien

    morfien Private E-2

    when i use the $_SERVER['PHP_SELF'] i tend to try use the echo command in there as well.

    the reason being is that the $_SERVER['PHP_SELF'] variable actually displays that page that you are on.

    i would try something like this

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

    you could also try

    $form = ?> <form action=" <?php echo $_SERVER['PHP_SELF'] ?> " method="post"> <?php
     
  3. Ruvvan

    Ruvvan Private E-2

    Ok. I don't have time to try it out now, but I'll give it a go later. Thanks. :)
     
  4. PC-XT

    PC-XT Master Sergeant

    I usually use curly brackets when identifying arrays inline in strings, especially when the subscript of the array is a string:
    PHP:
    $form ="<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">";
     
  5. morfien

    morfien Private E-2

    indeed you can do that but i find that dreamweaver doesnt change the colour of the string inside the bracers. thus making it a bit harder to debug.

    but you are perfectly right you could do that.
     
  6. PC-XT

    PC-XT Master Sergeant

    Yeah, that is a problem. I also use
    PHP:
    $form ="<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">";
    or actually, I would prefer to use
    PHP:
    $form ='<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
    in this case to avoid the escapes.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds