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
Solved

PHP Post

Posted on 2014-03-14
12
649 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 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

840 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