Solved

PHP Post

Posted on 2014-03-14
12
634 Views
Last Modified: 2014-03-14
Hi Experts,

I am taking a PHP course, but cannot get the PHP post to work.

First, I have the following PHP code:
<?php
    require_once('database.php');

    // Get all categories
    $query = 'SELECT * FROM categories
              ORDER BY categoryID';
    $categories = $db->query($query);
?>
<!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">

<!-- the head section -->
<head>
    <title>My Guitar Shop</title>
    <link rel="stylesheet" type="text/css" href="main.css" />
</head>

<!-- the body section -->
<body>
    <div id="page">

    <div id="header">
        <h1>Product Manager</h1>
    </div>

    <div id="main">

    <h1>Category List</h1>
    <table>
        <tr>
            <th>Name</th>
            <th>&nbsp;</th>
        </tr>
        
    <!-- add code for the rest of the table here -->
    <?php foreach($categories as $cat) : ?>
        <tr>
            <td><?php echo $cat['categoryName']; ?></td>
                    <td>
                        <form action="delete_category.php" method="post" id="delete_cat_form">
                            <input type="hidden" name="catID" 
                                   value="<?php echo $cat['categoryID']; ?>"/>
                            <input type="submit" value="Delete "
                        </form>
                     </td>
                
        </tr>
    <?php endforeach; ?>
    </table>
    <br />

    <h2><a href="add_category_form.php">Add Category</a></h2>
    
    <!-- add code for the form here -->
    
    <br />
    <p><a href="index.php">List Products</a></p>

    </div> <!-- end main -->

    <div id="footer">
        <p>
            &copy; <?php echo date("Y"); ?> My Guitar Shop, Inc.
        </p>
    </div>

    </div><!-- end page -->
</body>
</html>

Open in new window


This produces the following html:
<!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">

<!-- the head section -->
<head>
    <title>My Guitar Shop</title>
    <link rel="stylesheet" type="text/css" href="main.css" />
</head>

<!-- the body section -->
<body>
    <div id="page">

    <div id="header">
        <h1>Product Manager</h1>
    </div>

    <div id="main">

    <h1>Category List</h1>
    <table>
        <tr>
            <th>Name</th>
            <th>&nbsp;</th>
        </tr>
        
    <!-- add code for the rest of the table here -->
            <tr>
            <td>Guitars</td>
                    <td>
                        <form action="delete_category.php" method="post" id="delete_cat_form">
                            <input type="hidden" name="catID" 
                                   value="1"/>
                            <input type="submit" value="Delete "
                        </form>
                     </td>
                
        </tr>
            <tr>
            <td>Basses</td>
                    <td>
                        <form action="delete_category.php" method="post" id="delete_cat_form">
                            <input type="hidden" name="catID" 
                                   value="2"/>
                            <input type="submit" value="Delete "
                        </form>
                     </td>
                
        </tr>
            <tr>
            <td>Drums</td>
                    <td>
                        <form action="delete_category.php" method="post" id="delete_cat_form">
                            <input type="hidden" name="catID" 
                                   value="3"/>
                            <input type="submit" value="Delete "
                        </form>
                     </td>
                
        </tr>
        </table>
    <br />

    <h2><a href="add_category_form.php">Add Category</a></h2>
    
    <!-- add code for the form here -->
    
    <br />
    <p><a href="index.php">List Products</a></p>

    </div> <!-- end main -->

    <div id="footer">
        <p>
            &copy; 2014 My Guitar Shop, Inc.
        </p>
    </div>

    </div><!-- end page -->
</body>
</html>

Open in new window


However, when I click "Delete" for any category I always get cat= 3 with the following code in my delete_category.php.
<?php

$cat = $_POST['catID'];

$query = "cat = $cat";
echo $query;
        
?>

Open in new window


What am I doing wrong?
0
Comment
Question by:APD_Toronto
  • 6
  • 6
12 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39930370
You are not understanding how PHP views POST (and GET) variables.  And you are not distinguishing between the different <form>s so any submit button submits ALL of them.  You have id="delete_cat_form" on all three forms but 'id' is supposed to unique on a page.  You also don't have a unique 'name' attribute on the forms.

I have some sample code that I'll post in a minute.
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39930372
Your forms are incorrect: the input submit is missing closing bracket />. You must fix this ion your php script this way:

    <?php foreach($categories as $cat) : ?>
        <tr>
            <td><?php echo $cat['categoryName']; ?></td>
                    <td>
                        <form action="delete_category.php" method="post" id="delete_cat_form">
                            <input type="hidden" name="catID" 
                                   value="<?php echo $cat['categoryID']; ?>"/>
                            <input type="submit" value="Delete " />
                        </form>
                     </td>
                
        </tr>
    <?php endforeach; ?>

Open in new window

0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39930381
Fixing that, all works like a charms: http://eetest.delphicoding.com/index.php
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39930390
When you post a value with the same name but multiple values like "catID=1,2,3", PHP reads that as assigning a value to 'catID' three times which leaves you with only the last value.  And you are getting multiple values because the forms do not have different names and ids.
0
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 39930404
@DaveBaldwin: hi Dave. Please, see the link above: first Notice due to quick coding apart, the page works perfectly. After all, each form acts by itself, not depending by other forms in the page: each time you press submit button, one and only one value is submitted, so the result is the expected one.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39930413
You could simply use 'catID' to add a number to the name of  each form to distinguish it to get it to look like this:
            <tr>
            <td>Guitars</td>
                    <td>
                        <form name="form1" action="delete_category.php" method="post" id="delete_cat_form">
                            <input type="hidden" name="catID" value="1"/>
                            <input type="submit" value="Delete " />
                        </form>
                     </td>
                
        </tr>
            <tr>
            <td>Basses</td>
                    <td>
                        <form name="form2" action="delete_category.php" method="post" id="delete_cat_form">
                            <input type="hidden" name="catID" 
                                   value="2"/>
                            <input type="submit" value="Delete " />
                        </form>
                     </td>
                
        </tr>
            <tr>
            <td>Drums</td>
                    <td>
                        <form name="form3" action="delete_category.php" method="post" id="delete_cat_form">
                            <input type="hidden" name="catID" 
                                   value="3"/>
                            <input type="submit" value="Delete " />
                        </form>
                     </td>
                
        </tr>
 

Open in new window

PHP like this...
    <!-- add code for the rest of the table here -->
    <?php foreach($categories as $cat) : 
    $catID = $cat['categoryID'];?>
        <tr>
            <td><?php echo $cat['categoryName']; ?></td>
                    <td>
                        <form name="form<?php echo $catID; ?>" action="delete_category.php" method="post" id="delete_cat_form">
                            <input type="hidden" name="catID" 
                                   value="<?php echo $catID; ?>"/>
                            <input type="submit" value="Delete "
                        </form>
                     </td>
                
        </tr>
    <?php endforeach; ?>

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39930419
@Marco, your method may work but I would never do it that way.
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39930426
Why?
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39930429
Personally, I would have used only one form and several checkboxes to do it, but I don't see any problem doing it that way, but I'm not an eagle :)
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39930449
Because in all the work that I have done, forms get a unique name and id.  That is often so I can use javascript validation and direct it to the proper form.  It becomes more important when there is more than one form on a page.  It has become a standard method for me.

If you use one form with several checkboxes, the checkboxes need to have unique names or a name in an array format like 'catID[]' for PHP to handle them properly when you check more than one.
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39930471
Yes, obviously the checkboxes should have an unique name.
Please, try to understand what I mean: I agree it is alway better to have unique ids and names, and if you use javascript this is mandatory. I always mean that in this specific situation, the problem was the unclosed tags for submit inputs.

Cheers
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39930509
If that is all there is to the problem, that may be all right.  But I stand by my recommendations, that's just the way I have learned to do things.  Simple pages like in this question tend to grow into more complicated ones.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now