Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hello All,

 

Well I'm wrapping up the last part of this book and seem to have hit a roadblock.  On the first Ch. 13 Pursue assignment that tells you to make the login form sticky, I can't seem to get it to work and I've been able to get all of the previous ones pretty quickly.  I know it'll probably be something that makes me feel silly after I figure it out, but after 2 hours of mixing and rearranging code, I keep coming back to the same thing and get somewhat similar results each time.  Aside from it not working properly, when I put my code in the value attribute, it shows up as code in the actual form the first and every time after you reload the page.  Here is my code and any help is appreciated:

 

 

<?php
// Script 13.5 - login.php
/* This page lets people log into the site. */
 
// Set two variables with default values:
$loggedin = false;
$error = false;
 
// Check if the form has been submitted:
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    // Handle the form:
    if(!empty($_POST['email']) && !empty($_POST['password']))
    {
        if ( (strtolower($_POST['email']) == 'me@example.com') && ($_POST['password'] == 'testpass') )
        {
            // Correct!
 
            // Create the Cookie
            setcookie('Samuel', 'Clemens', time()+3600);
 
            // Indicate they are logged in:
            $loggedin = true;
        }
        else
        {
            // Incorrect!
            $error = 'The submitted email address and password do not match those on file!';
        }
    }
    else
    {
        // Forgot a field.
        $error = 'Please make sure you enter both an email address and a password!';
    }
}
 
// Set the page title and include the header file:
define('TITLE', 'Login');
include('templates/header.html');
 
// Print an error if one exists:
if ($error)
{
    print '<p class="error">' . $error . '</p>';
}
 
// Indicate the user is logged in, or show the form:
if($loggedin)
{
    print '<p>You are now logged in!</p>';
}
else
{
    print '<h2>Login Form</h2>
           <form action="login.php" method="post">
           <p><label>Email Address <input type="text" name="email" value="<?php if(isset($_POST[\'email\'])){ print htmlspecialchars($_POST[\'email\']); } ?>"/></label></p>
           <p><label>Password <input type="password" name="password" /></label></p>
           <p><input type="submit" name="submit" value="Log In!" /></p>
           </form>';
}
 
include('templates/footer.html');
// Need the footer.
?>

 

I've tried messing around with the red code all sorts of ways and get different results each time.  If I don't escape the post values for email then I get a unexpected 'email'(T_STRING) error.  I have a feeling it has something to do with the quotes and for the fact that the form is still within php already but can't quite grasp it yet. Please help!

 

Scatz

Link to post
Share on other sites

Off the top of my head you have php tags within php tags in the value attribute which will just be printed instead of interpreted. Also you're using single quotes which displays everything literally.

 

There are different ways to use quotes when using php to display html.

 

Use double quotes and escape the double quotes for the attributes; or if using single quotes, end the single quotes for variables and php code and use the concatenate operator.

 

My preferred way is to come out of php altogether when displaying lots of html markup. Here's one option

if($loggedin)
{
    print '<p>You are now logged in!</p>';
}
else
{
?> // exit php
<h2>Login Form</h2>
           <form action="login.php" method="post">
           <p><label>Email Address <input type="text" name="email" value="<?php if(isset($_POST['email'])){ print htmlspecialchars($_POST['email']); }?>"/></label></p>
           <p><label>Password <input type="password" name="password" /></label></p>
           <p><input type="submit" name="submit" value="Log In!" /></p>
           </form>
<?php // start php again 
}
?>
  • Upvote 2
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...