Geoff McLennan
Geoff McLennan

Reputation: 448

Trouble with php form handling

I am attempting to create a shared grocery list webapp as a way to learn HTML and PHP. I am attempting to follow these directions to create my item addition form.

I am trying to get my page to show the submitted data under the form as a proof of concept, but nothing will display. This is my code, saved in \XAMPP\htdocs\ :

<html>
<head>
    <title>Grocery List</title>
</head>
<body>

<?php
$category = $item = "";
?>

    <form id='additem' method='post'>
    <fieldset>
        <legend>Add Item</legend>
        <table>
        <tr>
            <td><label for='Category'>Category: </label></td>
            <td><input type='text' name='Category' list='categories' value='<?php echo $category;?>' /></td>
                <datalist id='categories'>
                    <option value='Protein'>
                    <option value='Produce'>
                    <option value='Baked Goods'>
                    <option value='Dry/Canned'>
                    <option value='Household'>
                </datalist>
        </tr><tr>
            <td><label for='item'>Name: </label></td>
            <td><input type='text' name='item' value='<?php echo $item;?>' /></td>
        </tr><tr>
            <td></td><td><input type='Submit' value='Submit' /></td>
        </tr>
        </table>
    </fieldset>
    </form>

    <?php
    echo "Your Input:";
    echo $category;
    echo $item;
    ?>

but when I open that page in my browser and input some information all I get is "Your Input:" followed by empty space where my info should be output.

Can anybody see where I am going wrong?

Upvotes: 3

Views: 49

Answers (2)

Skullclutter
Skullclutter

Reputation: 46

Your Option tags are not formatted correctly.

<option value='Protein'>Protein</option>
<option value='Produce'>Produce</option>
<option value='Baked Goods'>Baked Goods</option>
<option value='Dry/Canned'>Dry/Canned</option>
<option value='Household'>Household</option>

In this case, what's between the <option> and </option> tags will appear on screen and the value=stuff is what will be saved as the value for "Categories"

Upvotes: 0

random_user_name
random_user_name

Reputation: 26160

It's because you aren't actually processing the $_POST variable that is submitted.

Change your code as follows:

<?php
    // Load the posted values into their respective variables
    $category = $_POST['Category'];
    $item = $_POST['item'];
    echo "Your Input:";
    echo $category;
    echo $item;
?>

EDIT
Since you've updated your question, I'm updating where you should make these changes.

Now you have your variables being declared at the top of the page (which is good). But you should now get them from the post variable there as well, and include some sort of check to see if it was posted or not (to avoid warnings / notices):

At the top of your file:

<?php
    $category = '';
    $item = '';
    // Check if the form is posted...
    if (isset($_POST['item'])) {
        // Load the posted values into the variables
        $category = $_POST['Category'];
        $item = $_POST['item'];
        // .. do the same for any other form values 
    }
?>

You may find code samples or suggestions to use extract($_POST), but I strongly recommend against using extract().

Upvotes: 3

Related Questions