Jump to content
Larry Ullman's Book Forums

Scatz29

Members
  • Posts

    7
  • Joined

  • Last visited

Everything posted by Scatz29

  1. Thanks Larry! That worked like a charm and I was surprised to see how close I actually was to getting it. Scatz
  2. Ok, so posted on Ch. 13 Pursue earlier about making the login form sticky which I had great help getting resolved. Then when I get down the part where I need to make add_quote.php sticky, I have made the text area and source sticky but am having a really hard time trying to figure out how to get the checkbox to be sticky as well. I'm not even 100% sure if that's something that I'm even supposed to try to do for that part but it's an intriguing problem that I've run into and I can definitely see where knowing how to make a checkbox be sticky can be helpful in the future. I've searched all over the place for the past few days and tried countless times to get it to work. Most of the threads I've come across never give a straight answer on how to do this or it's like the original question never gets answered and the thread just kind of dies. Anyone out there know of a way to do this? Here is the code that I have and most of the variations I've tried always posts the favorite, but it just won't remain sticky. The red section and value for the checkbox at the bottom is the latest that I've come across that still doesn't seem to work. <?php // Script 13.7 - add_quote.php /* This script adds a quote. */ // Define a page title and include the header: define('TITLE', 'Add a Quote'); include('templates/header.html'); print '<h2>Add a Quotation</h2>'; // Restrict Access to administrators only: if (!is_administrator()) { print '<h2>Access Denied!</h2><p class="error">You do not have permission to access this page.</p>'; include('templates/footer.html'); exit(); } // Check for a form submission: if($_SERVER['REQUEST_METHOD'] =='POST') { // Handle the form. if (!empty($_POST['quote']) && !empty($_POST['source'])) { // Need the database connection: include('includes/mysql_connect.php'); // Prepare the values for storing: $quote = mysql_real_escape_string(trim(strip_tags($_POST['quote'])), $dbc); $source = mysql_real_escape_string(trim(strip_tags($_POST['source'])), $dbc); // Create the "favorite" value: if (isset($_POST['favorite'])) { $favorite = 1; } else { $favorite = 0; } $query = "INSERT INTO quotes (quote, source, favorite) VALUES ('$quote', '$source', '$favorite')"; $r = mysql_query($query, $dbc); if (mysql_affected_rows($dbc) == 1) { // Print a message: print '<p>Your quotation has been stored.</p>'; } else { print '<p class = "error">Could not store the quote because: <br />' . mysql_error($dbc) . '.</p><p>The query being run was: ' .$query . '</p>'; } // Close the connection: mysql_close($dbc); } else { // Failed to enter a quotation. print '<p class = "error">Please enter a quotation and a source!</p>'; } } // End of submitted IF. // Leave PHP and display the form: ?> <form action="add_quote.php" method="post"> <p><label>Quote <textarea name="quote" rows="5" cols="30" ><?php if(isset($_POST['quote'])) {print htmlspecialchars($_POST['quote']); } ?></textarea></label></p> <p><label>Source <input type="text" name="source" value="<?php if(isset($_POST['source'])) {print htmlspecialchars($_POST['source']); } ?>"/></label></p> <p><label>Is this a favorite? <input type="checkbox" name="favorite" value="<?php if(isset($_POST['favorite']) && ($_POST['favorite'] == 1)) print 'checked = "checked"'; ?>" /></label></p> <p><input type="submit" name="submit" value="Add This Quote!" /></p> </form> <?php include('templates/footer.html'); ?> At this point, I wouldn't be surprised if what I have is way off but I'm still trying and have hit a wall for sure. Thanks in advance for any help or suggestions. Scatz
  3. Thanks Margaux! That makes perfect sense and is some really good advice that I'll definitely use in the future when I encounter these situations. Thanks a lot for your help and time! Scatz
  4. 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
  5. It worked!!! Thanks Larry! Man I kind of figured that that's where my issue was and for some reason, file paths trip me up a little bit but I'll for sure figure them out and that was a great bit of advice you gave me that I'll definitely hold on to! On another note, I'm so glad you wrote this book and I knew it was a winner as soon as I started! I literally have 5 more (PHP and MySQL 4th Ed, PHP Advanced 3rd Ed, Modern JavaScript, Effortless E-Commerce, and The Yii Book for when I get to frameworks) that are on my shelf that I plan to tackle as soon as I finish this one and this summer while off from work as a teacher! I know it will take me some time but I'm extremely confident that these series of books that you've written will put me on the path to being a full on web developer in no time! You flat out rock and I just want to let you know that it is appreciated, take care! Scatz
  6. Margaux - I looked it up and am not really sure that is the issue or I'm not really understanding exactly what the php chmod() function was telling me to do as it relates to the actual issue that I'm having. Thanks for the suggestion though! I'm still working on it. Larry - My register.php file is in a folder that I've created for the book and additionally created a folder for each chapter of the book within it and they are all inside of my webroot (htdocs) folder. The actual location is C:\xampp\htdocs\php_for_the_web\ch11_files_and_directories\register.php and the location for the users text file is C:\xampp\users\users.txt ...I'm thinking that it's something to do with my file paths but I'm not sure. I'll definitely need to spend a little more time with them as they are not my strong point, even though they should be pretty simple, but I will say they are making more sense and I'm getting better! Thanks for the help in advance! Scatz
  7. I'm having a problem with the register.php file in the "Creating Directories" section to the point where I can't continue on with the chapter. I'm using xampp on windows and have PHP 5.4.7 running and I'm pretty sure that my scripts are identical to the ones in the book and after testing out the scripts, I get the exact error that is displayed on pg. 325 example A for the result if the users.txt file is not writable. It gives me the system error response every time and I'm wondering if I don't have the correct directory mapped or something, but I don't see anything after this in the book that specifically addresses what to do if you actually run into the problem that I'm having, which is the user.txt file not being writable. Any suggestions? <!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>Register</title> <style type="text/css" media="screen"> .error { color: red; } </style> </head> <body> <h1>Register</h1> <?php // Script 11.6 - register.php /* This script registers a user by storing their information in a text file and creating a directory for them. */ // Error Handling ini_set('display_errors', 1); error_reporting(E_ALL | E_STRICT); // Identify the directory and file to use: $dir = '../users/'; $file = $dir . 'users.txt'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form. $problem = FALSE; // No problems so far. // Check for each value... if (empty($_POST['username'])) { $problem = TRUE; print '<p class="error">Please enter a username!</p>'; } if (empty($_POST['password1'])) { $problem = TRUE; print '<p class="error">Please enter a password!</p>'; } if ($_POST['password1'] != $_POST['password2']) { $problem = TRUE; print '<p class="error">Your password did not match your confirmed password!</p>'; } if (!$problem) // If there weren't any problems... { if (is_writable($file)) { // Open the file. // Create the data to be written: $subdir = time() . rand(0, 4596); $data = $_POST['username'] . "\t" . md5(trim($_POST['password1'])) . "\t" . $subdir . PHP_EOL; // Write the data: file_put_contents($file, $data, FILE_APPEND | LOCK_EX); // Create the directory: mkdir($dir . $subdir); // Print a message: print '<p>You are now registered!</p>'; } else { // Couldn't write to the file print '<p class="error">You could not be registered due to a system error.</p>'; } } else { // Forgot a field. print '<p class="error">Please go back and try again!</p>'; } } else { // Display the form. // Leave PHP and display the form. ?> <form action="register.php" method="post"> <p>Username: <input type="text" name="username" size="20" /></p> <p>Password: <input type="password" name="password1" size="20" /></p> <p>Confirm Password: <input type="password" name="password2" size="20" /></p> <input type="submit" name="submit" value="Register" /> </form> <?php } // End of submission IF. ?> </body> </html>
×
×
  • Create New...