Results 1 to 2 of 2

Thread: Getting duplicate form elements after PHP mySQL query

  1. #1
    Join Date
    Jun 2008
    Posts
    16
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Getting duplicate form elements after PHP mySQL query

    Hi, I have a joke database, and when I click "add a joke" from the main page, I have a form show up where I can enter text, author, and category.

    For some reason I am getting duplicate listings for the authors and categories to choose from. Here is the code:

    CONTROLLER

    PHP Code:
    <?php
    // ===== ADD NEW JOKE =====
    include_once $_SERVER['DOCUMENT_ROOT'] . '/byo/includes/magicquotes.inc.php'

    if (isset(
    $_GET['add'])) {
        
    $pagetitle 'Add new joke';
        
    $action 'addform';
        
    $text '';
        
    $authorid '';
        
    $id '';
        
    $button 'Add joke';
        
        include 
    $_SERVER['DOCUMENT_ROOT'] . '/byo/includes/db.inc.php';
        
        
    // build the list of authors (using $authors[] array variable)
        
    $sql 'SELECT id, name FROM author';
        
    $result mysqli_query($link$sql);
        if (!
    $result) {
            
    $error 'Error fetching list of authors.';
            include 
    $_SERVER['DOCUMENT_ROOT'] . '/byo/includes/error.html.php';
            exit();
        }
        
        while (
    $row mysqli_fetch_array($result)) {
            
    $authors[] = array('id' => $row['id'], 'name' => $row['name']);
        }
        
        
    // build the list of categories (using $categories[] array variable)
        
    $sql 'SELECT id, name FROM category';
        
    $result mysqli_query($link$sql);
        if (!
    $result) {
            
    $error 'Error fetching list of categories.';
            include 
    $_SERVER['DOCUMENT_ROOT'] . '/byo/includes/error.html.php';
            exit();    
        }
        
        while (
    $row mysqli_fetch_array($result)) {
            
    $categories[] = array('id' => $row['id'], 'name' => $row['name'], 'selected' => FALSE);
        }
        
        include 
    'form.html.php';
        exit();
    }
    ?>
    FORM FOR ADDING OR EDITING JOKE(S)
    HTML Code:
    <?php include_once $_SERVER['DOCUMENT_ROOT'] . '/byo/includes/helpers.inc.php'; ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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><?php htmlout($pagetitle); ?></title>
    	
    </head>
    
    <body>
    	
    <h1><?php htmlout($pagetitle); ?></h1>
    <form action="?<?php htmlout($action); ?>" method="post">
    <div>	
    	<label for="text">Type your joke here:</label><br />
    	<textarea id="text" name="text" rows="3" cols="40">
    		<?php htmlout($text); ?>
    	</textarea>
    </div>
    <div>
    	<label for="author">Author:</label>
    	<select name="author" id="author">
    		<option value="">Select one</option>
    		<?php foreach($authors as $author): ?>
    			<option value="<?php htmlout($author['id']); ?>" <?php if($author['id'] == $authorid) echo 'selected="selected"'; ?>>
    			<?php htmlout($author['name']); ?>
    			</option>
    		<?php endforeach; ?>
    	</select>
    </div>
    <fieldset>
    	<legend>Categories:</legend>
    	<?php foreach($categories as $category): ?>
    		<input 
    		type="checkbox" 
    		name="categories[]" 
    		id="category<?php htmlout($category['id']); ?>" 
    		value="<?php htmlout($category['id']); ?>"<?php 
    		if ($category['selected']) {
    			echo ' checked="checked"';
    		}
    		?> />
    		<label for="category<?php htmlout($category['id']); ?>"><?php htmlout($category['name']); ?></label>
    	<?php endforeach; ?>
    </fieldset>
    <div>
    	<input type="hidden" name="id" value="<?php htmlout($id); ?>" />
    	<input type="submit" value="<?php htmlout($button); ?>" />
    </div>
    </form>
    
    </body>
    </html>
    I'm attaching a screenshot of the output.

    Thanks for any help...

  2. #2
    Join Date
    Jun 2008
    Posts
    16
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    Figured it out, sorta.. the problem was that my code for displaying the new joke entry form was at the bottom of my controller instead of at the top, so instead of going to a new page to enter a new joke, I was injecting the new form page at the bottom of the current page.

    I'm still not sure why that was causing my outputs and checkboxes to be duplicated though.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •