Whats wrong with my php form?

Soldato
Joined
7 Jan 2007
Posts
10,607
Location
Sussex, UK
Hi, I can't work out why it wont work.. can anyone shed any light?

PHP:
<?php
$education = $_POST["education"];

if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
?>
<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
Select a Level of Education:<br />
<select name="education">
<option value="Jr.High">Jr.High</option>
<option value="HighSchool">HighSchool</option>
<option value="College">College</option></select><br />
<input type="submit" value="submit" name="submit">
</form>
<?php
elseif  ($education == "Jr.High") {
		echo "$education."! . "Elseif for Junior High has worked!""<br />";
		}
		
elseif  ($education == "HighSchool") {
		echo "$education."! . "Elseif for HighSchool has worked!""<br />";
		}

elseif  ($education == "College") {
		echo "$education."! . "Elseif for College has worked!""<br />";
		}	
		
else {
		echo "There has been an error! You suck at this!";

?>



</body>
</html>
 
What error do you get?

You will need to close the else {

echo "$education."! . "Elseif for Junior High has worked!""<br />";

would be better written as

echo $education . ' Elseif for Junior High has worked!<br />';
 
Last edited:
My code now looks like this:

PHP:
<?php
$education = $_POST["education"];

if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form

<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
Select a Level of Education:<br />
<select name="education">
<option value="Jr.High">Jr.High</option>
<option value="HighSchool">HighSchool</option>
<option value="College">College</option></select><br />
<input type="submit" value="submit" name="submit">
</form>

elseif  ($education == "Jr.High") {
		echo $education . ' Elseif for Junior High has worked!<br />';
		}
		
elseif  ($education == "HighSchool") {
		echo $education . ' Elseif for HighSchool has worked!<br />';
		}

elseif  ($education == "College") {
		echo $education . ' Elseif for College has worked!<br />';
		}	
		
else {
		echo "There has been an error! You suck at this!";
		}

?>



</body>
</html>

Unfortunately, still getting:

HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.
 
try this

PHP:
<?php 
$education = $_POST["education"]; 

if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form 
?>

<html> 
<head> 
<title>Personal INFO</title> 
</head> 
<body> 
<form method="post" action="<?php echo $PHP_SELF;?>"> 
Select a Level of Education:<br /> 
<select name="education"> 
<option value="Jr.High">Jr.High</option> 
<option value="HighSchool">HighSchool</option> 
<option value="College">College</option></select><br /> 
<input type="submit" value="submit" name="submit"> 
</form> 
<?php

elseif  ($education == "Jr.High") { 
        echo $education . ' Elseif for Junior High has worked!<br />'; 
        } 
         
elseif  ($education == "HighSchool") { 
        echo $education . ' Elseif for HighSchool has worked!<br />'; 
        } 

elseif  ($education == "College") { 
        echo $education . ' Elseif for College has worked!<br />'; 
        }     
         
else { 
        echo "There has been an error! You suck at this!"; 
        } 

?> 



</body> 
</html>

You needed to close your php tag as you are displaying html!
 
ok my mamp is playing up. But I now get this error on my webhosting:


Parse error: syntax error, unexpected $end in /home/deanbeam/public_html/dropmenu.php on line 34

PHP:
<?php
$education = $_POST["education"];

if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
?>
<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
Select a Level of Education:<br />
<select name="education">
<option value="Jr.High">Jr.High</option>
<option value="HighSchool">HighSchool</option>
<option value="College">College</option></select><br />
<input type="submit" value="submit" name="submit">
</form>
<?php
echo "Traveling to $education<br />";
switch ($education){
	case "Jr.High":
		echo "Bring an extra $500";
		break;
	case "High School":
		echo "Bring an open mind";
		break;	
	case "College":
		echo "Bring 15 bottles of SPF 50 Sunscreen";
		break;	
	
}

?>
 
It's not a server error, the script is just very broken, screwing with http headers :p

Some incorrect things:

if (!isset($_POST['submit'])) { is missing a closing } and you are not echoing out the html so this will break badly.
 
Thanks for your help guys, but getting this error with your code addicted:

Parse error: syntax error, unexpected T_ELSEIF on line 22

change the first elseif to just if.

Parse errors are just errors of the code. Something silly usually. Like calling a function without the ending ();

function parseError() { echo "Yay!!"; };

parseError;
// returns parse error

parseError();
// Returns "Yay!!"


Or something like starting an elseif statment without if ;)
 
Last edited:
ok my mamp is playing up. But I now get this error on my webhosting:


Parse error: syntax error, unexpected $end in /home/deanbeam/public_html/dropmenu.php on line 34

PHP:
<?php
$education = $_POST["education"];

if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
?>
<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
Select a Level of Education:<br />
<select name="education">
<option value="Jr.High">Jr.High</option>
<option value="HighSchool">HighSchool</option>
<option value="College">College</option></select><br />
<input type="submit" value="submit" name="submit">
</form>
<?php
echo "Traveling to $education<br />";
switch ($education){
	case "Jr.High":
		echo "Bring an extra $500";
		break;
	case "High School":
		echo "Bring an open mind";
		break;	
	case "College":
		echo "Bring 15 bottles of SPF 50 Sunscreen";
		break;	
	
}

?>

Missing a closing } at the end of the page

that's what an unexpected $blah end usually means. Not always the end of the page though, but somewhere there's a } missing.
 
Ok now not producing a parse error, just not working as intended:p

When I now click submit it now says cannot find url.... :p Why isn't going through the if and elseif statements?

PHP:
<?php 
$education = $_POST["education"]; 

if (!isset($_POST['submit'])) {
		echo <<< EOT

<html> 
<head> 
<title>Personal INFO</title> 
</head> 
<body> 
<form method="post" action="<?php echo $PHP_SELF;?>"> 
Select a Level of Education:<br /> 
<select name="education"> 
<option value="Jr.High">Jr.High</option> 
<option value="HighSchool">HighSchool</option> 
<option value="College">College</option></select><br /> 
<input type="submit" value="submit" name="submit"> 
</form>
EOT;

}

if  ($education == "Jr.High") { 
        echo $education . ' Elseif for Junior High has worked!<br />'; 
        } 
         
elseif  ($education == "HighSchool") { 
        echo $education . ' Elseif for HighSchool has worked!<br />'; 
        } 

elseif  ($education == "College") { 
        echo $education . ' Elseif for College has worked!<br />'; 
        }     
         

?> 



</body> 
</html>
 
Working!

It was the form action line that was bad before.

PHP:
<?php 
$education = $_POST["education"]; 

?>


<html> 
<head> 
<title>Personal INFO</title> 
</head> 
<body> 
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
Select a Level of Education:<br /> 
<select name="education"> 
<option value="Jr.High">Jr.High</option> 
<option value="HighSchool">HighSchool</option> 
<option value="College">College</option></select><br /> 
<input type="submit" value="submit" name="submit"> 
</form>

<?php


if  ($education == "Jr.High") { 
        echo $education . ' Elseif for Junior High has worked!<br />'; 
        } 
         
elseif  ($education == "HighSchool") { 
        echo $education . ' Elseif for HighSchool has worked!<br />'; 
        } 

elseif  ($education == "College") { 
        echo $education . ' Elseif for College has worked!<br />'; 
        }     
         

?> 



</body> 
</html>
 
Back
Top Bottom