Solved

PHP Post

Posted on 2014-03-14
12
656 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 83

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 31

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 31

Expert Comment

by:Marco Gasi
ID: 39930381
Fixing that, all works like a charms: http://eetest.delphicoding.com/index.php
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 83

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 31

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 83

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
 
LVL 83

Expert Comment

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

Expert Comment

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

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 83

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 31

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 83

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

630 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