Jump to content

Why this code doesn't run?


Truman

Recommended Posts

Ok, I see it> $_POST['last_name];

I forgot a single quotation mark after last_name.

 

And I also spotted a mistake in line 14. Now everything is fine. I will do my best to try to figure out these beginners mistakes before I post here.

Edited by Truman
Link to comment
Share on other sites

An another problem, php code checker see no mistake:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Registration</title>
<style type="text/css" media="screen">
.error { color: red;}
</style>
</head>
<body>
<h1>Registration Results</h1>
<?php // Script 6.2 handle_reg.php
$okay = TRUE;
if (empty($_POST['email'])) {
	print '<p class="error">Please enter your email address.</p>';
	$okay = FALSE;
}

if (empty($_POST['password'])) {
	print '<p class="error">Please enter your password.</p>';
	$okay = FALSE;
} 
if (empty($_POST['confirm'])) {
	print '<p class="error">Please enter your password.</p>';
	$okay = FALSE;
}
if ($_POST['password'] != $_POST['confirm']) {
	print '<p class="error">Your confirmed password does not match the original password.</p>';
	$okay = FALSE;
}
if (is_numeric($_POST['year'])) {
	$age = 2016 - $_POST['year']; 
} else {
	print '<p>Please enter the year you were born as four digits.</p>';
	$okay = FALSE;
}
if ($_POST['year'] >= 2016) {
	print '<p class="error">Either you enter your birth year wrong or you come from the future!</p>';
	$okay = FALSE;
}

if ($okay) {
print '<p>You have been successfully registered.</p>';
print '<p>You will turn $age this year</p>';
}
?>
</body>
</html>

and I receive:

Registration Results

You have been successfully registered.

You will turn $age this year

 

I don't understand why it doesn't show $age variable, the formula should be OK.

Link to comment
Share on other sites

When using print with variable within string use double quotes, else it will print as string, else you escape string from php like

 

print '<p>You will turn '.$age.' this year</p>';
Note: second time i have done this! major problem with forum multiple 'oops error' message with 'server to busy' WHAT IS HAPPENING?
  • Like 1
Link to comment
Share on other sites

Again a thing I don't understand, I'm working on nesting conditionals:

<?php // Script 6.2 handle_reg.php
$okay = TRUE;
if (empty($_POST['email'])) {
	print '<p class="error">Please enter your email address.</p>';
	$okay = FALSE;
}

if (empty($_POST['password'])) {
	print '<p class="error">Please enter your password.</p>';
	$okay = FALSE;
} 
if (empty($_POST['confirm'])) {
	print '<p class="error">Please enter your password.</p>';
	$okay = FALSE;
}
if ($_POST['password'] != $_POST['confirm']) {
	print '<p class="error">Your confirmed password does not match the original password.</p>';
	$okay = FALSE;
}
if ( is_numeric($_POST['year']) AND (strlen($_POST['year'] == 4) ) {
	if ($_POST['year'] < 2015) {
		$age = 2015 - $_POST['year'];
	} else {
		print '<p class="error">Either you entered your birth year wrong or you come from the future!</p>';
    $okay = FALSE;		
	} 
	
 }	else {
		print '<p class="error">Please enter the year you were born as four digits.</p>';
		$okay = FALSE;
	}


if (!isset($_POST['terms']) AND ($_POST['terms'] == 'YES') ) {
	print '<p class="error">You must accept the terms.</p>';
	$okay = FALSE;
 }

if ($okay) {
print '<p>You have been successfully registered.</p>';
print "<p>You will turn $age this year</p>";
}
?>

it says:

Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\my-site\handle_reg.php on line 32

 

line 32 is: if ( is_numeric($_POST['year']) AND (strlen($_POST['year'] == 4) ) {

 

Why is opening curly brace unexpected?

Link to comment
Share on other sites

You have mismatched parentheses.

 

if ( is_numeric($_POST['year']) AND (strlen($_POST['year'] == 4) ) {

 

I don't think "AND" is a valid keyword in PHP. You should use &&.

Link to comment
Share on other sites

if ( is_numeric($_POST['year']) AND (strlen($_POST['year'] == 4) ) {

 

should be

 

if ( is_numeric($_POST['year']) && (strlen($_POST['year'] ) == 4)) {

 

You can use 'AND' but! it uses a more complicated method of parenthesis to give it higher precedence, because its precedence is lower than other operators compared to using '&&' so you have to make allowance for that.

Link to comment
Share on other sites

if ( is_numeric($_POST['year']) && (strlen($_POST['year'] ) == 4)) {
	if ($_POST['year'] < 2015) {
		$age = 2015 - $_POST['year'];
	} else {
		print '<p class="error">Either you entered your birth year wrong or you come from the future!</p>';
    $okay = FALSE;		
	} 
	
 }	else {
		print '<p class="error">Please enter the year you were born as four digits.</p>';
		$okay = FALSE;
	}

 

Link to comment
Share on other sites

compare with this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>Registration</title>
        <style type="text/css" media="screen">
            .error { color: red;}
        </style>
    </head>
    <body>
        <h1>Registration Results</h1>
        <?php
        // Script 6.2 handle_reg.php
        // XXXXXXXXXXXXXXXXXXXXXXXXXXXXX below is for testing only
        $_POST['email'] = "bull@.com";
        $_POST['password'] = "bull";
        $_POST['confirm'] = "bull";
        $_POST['year'] = 2010;
        $_POST['terms'] = 'YES';
        // XXXXXX  end testing code

        $current_year = date("Y"); //added by dsonesuk

        $okay = TRUE;

        if (empty($_POST['email'])) {
            print '<p class="error">Please enter your email address.</p>';
            $okay = FALSE;
        }

        if (empty($_POST['password'])) {
            print '<p class="error">Please enter your password.</p>';
            $okay = FALSE;
        }
        if (empty($_POST['confirm'])) {
            print '<p class="error">Please enter your password.</p>';
            $okay = FALSE;
        }
        if ($_POST['password'] != $_POST['confirm']) {
            print '<p class="error">Your confirmed password does not match the original password.</p>';
            $okay = FALSE;
        }
        if (is_numeric($_POST['year']) && (strlen($_POST['year']) == 4 )) {
            if ($_POST['year'] < $current_year) {
                $age = $current_year - $_POST['year'];
            } else {
                print '<p class="error">Either you entered your birth year wrong or you come from the future!</p>';
                $okay = FALSE;
            }
        } else {
            print '<p class="error">Please enter the year you were born as four digits.</p>';
            $okay = FALSE;
        }


        if (!isset($_POST['terms']) && ($_POST['terms'] !== 'YES')) { //should be NOT Equal
            print '<p class="error">You must accept the terms.</p>';
            $okay = FALSE;
        }

        if ($okay) {
            print '<p>You have been successfully registered.</p>';
            print "<p>You will turn $age this year</p>";
        }
        ?>
    </body>
</html>
Link to comment
Share on other sites

The file you've attached works without error when I tested it. There might be some strange server-side caching going on in your server. Try renaming the file and running it.

Link to comment
Share on other sites

I added '?wellthisis=bull' and receive the same message.

 

You have to change it every time, else it will load the older page with that querystring again!

 

?wellthisis=bull1

?wellthisis=bull2

 

or you could code querystring value to be timestamp so it will be different everytime from submitted form.

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...