Jump to content

Syntax error in PHP


Epicular

Recommended Posts

I am very new to PHP (and forms in general) and I am using the PHP tutorial on w3schools.com to make a simple form for the user to fill out and get checked for errors. I am adapting the code to make an account registration system but there is a syntax error somewhere in the code and I can't find it.

Here is my code:

<!DOCTYPE html><html><head><style>.error {color: #FF0000;}</style></head><body><?php// define variables and set to empty values$username = $email = $password = $password = "";$nameErr = $emailErr = $passErr = $passtErr = "";if ($_SERVER["REQUEST_METHOD"] == "POST") {	if (empty($_POST["username"])) {		$nameErr = "Error: All fields required"} else {		$username = test_input($_POST["username"]);}	if (empty($_POST["email"])) {		$emailErr = "Error: All fields required"} else {		$email = test_input($_POST["email"]);}	if (emtpy($_POST["password"])) {			$passErr = "Error: All fields required"} else {		$password = test_input($_POST["password"]);}	if (empty($_POST["passwordt"])) {		$passtErr = "Error: All fields required"} else {		$passwordt = test_input($_POST["passwordt"]);}  }function test_input($data) {  $data = trim($data);  $data = stripslashes($data);  $data = htmlspecialchars($data);  return $data;}?><h3>Account Sign-up</h3><p><span class="error">All fields required.</span></p><form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">    Username: <input type="text" name="username">  <span class="error"> <?php echo $nameErr;?></span><br>  The name you will be known by.<br><br>E-mail: <input type="text" name="email"><span class="error"> <?php echo $emailErr;?></span><br>    Used for password recovery.<br><br>    Password: <input type="password" name="password"><span class="error"> <?php echo $passErr;?></span><br>Minimum of 3 characters. Please don't make it stupid.<br><br>Confirm Password:<input type="password" name="passwordt"><span class="error"> <?php echo $passtErr;?></span><br>    Don't type in a different password.<br><br>    <input type="submit" name="submit" value="Submit"></form></body></html>

Here is the actual error received when initially running the code:

Parse error: syntax error, unexpected '}' in /home/doubl140/public_html/formtest.php on line 16

Link to comment
Share on other sites

Looks like it's in the "if ($_SERVER["REQUEST_METHOD"] == "POST") {" if.

 

Just take everything out of and check for errors and the start adding everything back incrementally until you find the mistake.

 

BEFORE THAT, I var_dump() the POST array.

Link to comment
Share on other sites

It is much easier to debug code when you use sane indenting:

$username = $email = $password = $password = "";$nameErr = $emailErr = $passErr = $passtErr = "";if ($_SERVER["REQUEST_METHOD"] == "POST") {    if (empty($_POST["username"])) {        $nameErr = "Error: All fields required"    }    else {        $username = test_input($_POST["username"]);    }    if (empty($_POST["email"])) {        $emailErr = "Error: All fields required"    }    else {        $email = test_input($_POST["email"]);    }    if (emtpy($_POST["password"])) {        $passErr = "Error: All fields required"    }    else {        $password = test_input($_POST["password"]);    }    if (empty($_POST["passwordt"])) {        $passtErr = "Error: All fields required"    }    else {        $passwordt = test_input($_POST["passwordt"]);    }}function test_input($data){    $data = trim($data);    $data = stripslashes($data);    $data = htmlspecialchars($data);    return $data;}
Notice the lack of semicolons on various lines.
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...