Jump to content
Larry Ullman's Book Forums

Recommended Posts

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.



// Script 13.7 - add_quote.php
/* This script adds a quote. */
// Define a page title and include the header:
define('TITLE', 'Add a Quote');
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>';
// Check for a form submission:
    // Handle the form.
    if (!empty($_POST['quote']) && !empty($_POST['source']))
        // Need the database connection:
        // 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;
            $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>';
            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:
        // 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>
<?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.




Link to comment
Share on other sites

You're very close. You just have to move your conditional outside of the value attribute:


<p><label>Is this a favorite? <input type="checkbox" name="favorite" value="1" <?php if(isset($_POST['favorite']) && ($_POST['favorite'] == 1)) print 'checked = "checked"'; ?>/></label></p>
  • Upvote 1
Link to comment
Share on other sites


  • Create New...