Jump to content
Larry Ullman's Book Forums

Zie

Members
  • Posts

    6
  • Joined

  • Last visited

  • Days Won

    1

Posts posted by Zie

  1. Thanks so much for your help! The errors seem to be coming from the form_functions.inc.php file. I have made changes on line 87, even deleting it entirely (it's actually just a comment) and I still get the same error.

     

    I also changed the add_pages file to require_once for including the form_functions.inc.php file.

     

    Here is a copy of the form_functions,inc.php file unchanged from the source file which is how I had uploaded it to the server originally:

     

    I have highlighted lines 13 and 87 in red

     

    <?php
     
    // This script defines any functions required by the various forms.
    // This script is created in Chapter 3.
     
    // This function generates a form INPUT or TEXTAREA tag.
    // It takes five arguments:
    // - The name to be given to the element.
    // - The type of element (text, password, textarea).
    // - The label for the element
    // - An array of errors.
    // - An array of additional options.
    function create_form_input($name, $type, $label = '', $errors = array(), $options = array()) {
     
    // Assume no value already exists:
    $value = false;
     
    // Check for a value in POST:
    if (isset($_POST[$name])) $value = $_POST[$name];
     
    // Strip slashes if Magic Quotes is enabled:
    if ($value && get_magic_quotes_gpc()) $value = stripslashes($value);
     
    // Start the DIV:
    echo '<div class="form-group';
     
    // Add a class if an error exists:
    if (array_key_exists($name, $errors)) echo ' has-error';
     
    // Complete the DIV:
    echo '">';
     
    // Create the LABEL, if one was provided:
    if (!empty($label)) echo '<label for="' . $name . '" class="control-label">' . $label . '</label>';
     
    // Conditional to determine what kind of element to create:
    if ( ($type === 'text') || ($type === 'password') || ($type === 'email')) {
     
    // Start creating the input:
    echo '<input type="' . $type . '" name="' . $name . '" id="' . $name . '" class="form-control"';
     
    // Add the value to the input:
    if ($value) echo ' value="' . htmlspecialchars($value) . '"';
     
    // Check for additional options:
    if (!empty($options) && is_array($options)) {
    foreach ($options as $k => $v) {
    echo " $k=\"$v\"";
    }
    }
     
    // Complete the element:
    echo '>';
     
    // Show the error message, if one exists:
    if (array_key_exists($name, $errors)) echo '<span class="help-block">' . $errors[$name] . '</span>';
     
    } elseif ($type === 'textarea') { // Create a TEXTAREA.
     
    // Show the error message above the textarea (if one exists):
    if (array_key_exists($name, $errors)) echo '<span class="help-block">' . $errors[$name] . '</span>';
     
    // Start creating the textarea:
    echo '<textarea name="' . $name . '" id="' . $name . '" class="form-control"';
     
    // Check for additional options:
    if (!empty($options) && is_array($options)) {
    foreach ($options as $k => $v) {
    echo " $k=\"$v\"";
    }
    }
     
    // Complete the opening tag:
    echo '>';
     
    // Add the value to the textarea:
    if ($value) echo $value;
     
    // Complete the textarea:
    echo '</textarea>';
     
    } // End of primary IF-ELSE.
     
    // Complete the DIV:
    echo '</div>';
     
    } // End of the create_form_input() function.
     
    // Omit the closing PHP tag to avoid 'headers already sent' errors!
  2. Hi daviddawn, 

     

    Thanks for you quick reply and advice. Here is the code for add_page.php. I was able to follow the tutorial in the book until this point. Then, when I uploaded this file, the form would show up and I cannot see what is wrong. I uploaded it as is from the author's source files:

     

    ?php
     
    // This page is used by an administrator to create a specific page of HTML content.
    // This script is created in Chapter 5.
     
    // Require the configuration before any PHP code as the configuration controls error reporting:
    require('./includes/config.inc.php');
     
    // If the user isn't logged in as an administrator, redirect them:
    redirect_invalid_user('user_admin');
     
    // Require the database connection:
    require(MYSQL);
     
    // Include the header file:
    $page_title = 'Add a Site Content Page';
    include('./includes/header.html');
     
    // For storing errors:
    $add_page_errors = array();
     
     
    // Check for a title:
    if (empty($_POST['title'])) {
    $t = escape_data(strip_tags($_POST['title']), $dbc);
    } else {
    $add_page_errors['title'] = 'Please enter the title!';
    }
     
    // Check for a category:
    if (filter_var($_POST['category'], FILTER_VALIDATE_INT, array('min_range' => 1))) {
    $cat = $_POST['category'];
    } else { // No category selected.
    $add_page_errors['category'] = 'Please select a category!';
    }
     
    // Check for a description:
    if (!empty($_POST['description'])) {
    $d = escape_data(strip_tags($_POST['description']), $dbc);
    } else {
    $add_page_errors['description'] = 'Please enter the description!';
    }
     
    // Check for the content:
    if (!empty($_POST['content'])) {
    $allowed = '<div><p><span><br><a><img><h1><h2><h3><h4><ul><ol><li><blockquote>';
    $c = escape_data(strip_tags($_POST['content'], $allowed), $dbc);
    } else {
    $add_page_errors['content'] = 'Please enter the content!';
    }
     
    if (empty($add_page_errors)) { // If everything's OK.
     
    // Add the page to the database:
     
    $q = "INSERT INTO pages (categories_id, title, description, content) VALUES ($cat, '$t', '$d', '$c')";
    $r = mysqli_query($dbc, $q);
     
    if (mysqli_affected_rows($dbc) === 1) { // If it ran OK.
     
    // Print a message:
    echo '<div class="alert alert-success"><h3>The page has been added!</h3></div>';
     
    // Clear $_POST:
    $_POST = array();
     
    // Send an email to the administrator to let them know new content was added?
     
    } else { // If it did not run OK.
    trigger_error('The page could not be added due to a system error. We apologize for any inconvenience.');
    }
     
    } // End of $add_page_errors IF.
     
    } // End of the main form submission conditional.
     
    // Need the form functions script, which defines create_form_input():
    require('includes/form_functions.inc.php');
    ?>
    <h1>Add a Site Content Page</h1>
    <form action="add_page.php" method="post" accept-charset="utf-8">
     
    <fieldset><legend>Fill out the form to add a page of content:</legend>
     
    <?php
    create_form_input('title', 'text', 'Title', $add_page_errors); 
     
    // Add the category drop down menu:
    echo '<div class="form-group';
    if (array_key_exists('category', $add_page_errors)) echo ' has-error'; 
     
    echo '"><label for="category" class="control-label">Category</label>
    <select name="category" class="form-control">
    <option>Select One</option>';
     
    // Retrieve all the categories and add to the pull-down menu:
    $q = "SELECT id, category FROM categories ORDER BY category ASC";
    $r = mysqli_query($dbc, $q);
    while ($row = mysqli_fetch_array($r, MYSQLI_NUM)) {
    echo "<option value=\"$row[0]\"";
    // Check for stickyness:
    if (isset($_POST['category']) && ($_POST['category'] == $row[0]) ) echo ' selected="selected"';
    echo ">$row[1]</option>\n";
    }
     
    echo '</select>';
    if (array_key_exists('category', $add_page_errors)) echo '<span class="help-block">' . $add_page_errors['category'] . '</span>';
    echo '</div>';
     
    create_form_input('description', 'textarea', 'Description', $add_page_errors); 
    create_form_input('content', 'textarea', 'Content', $add_page_errors); 
    ?>
     
    <input type="submit" name="submit_button" value="Add This Page" id="submit_button" class="btn btn-default" />
     
    </fieldset>
     
    </form> 
     
     
    <?php /* PAGE CONTENT ENDS HERE! */
     
    // Include the footer file to complete the template:
    include('./includes/footer.html');
    ?>
     
     
     
    Thanks in advance for your help!
×
×
  • Create New...