dongyin2390
2015-12-02 06:19
浏览 69
已采纳

使用XAMPP在文件中使用$ _POST时出现问题

I want to process form input into a MYSQL database but I am having trouble getting other files to recognize data in the _POST array and keep getting the typical "Undefined index" error. My form:

    <?php
    $category = '';
    $item = '';
    // Check if form is posted
    if (isset($_POST['item'])){
        // Declare POST'd values into variables
        $category = $_POST['Category'];
        $item = $_POST['item'];
    }
    ?>
    <!-- Item Input form -->
    <form id='additem' method='post' action="">
    <fieldset>
        <legend>Add Item</legend>
        <table>
        <tr>
            <td><label for='Category'>Category: </label></td>
            <td><input type='text' name='Category' list='categories' value='<?php $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 $item;?>' /></td>
        </tr><tr>
            <td></td><td><input type='Submit' value='Submit' /></td>
        </tr>
        </table>
    </fieldset>
    </form>

When I use $_POST in this file, it works perfectly fine but when I try to use it in another file, process.php:

<?php
echo $_POST['Category'];
echo '<br>';
echo $_POST['item'];
?>

it gives: Notice: Undefined index: Category in E:\Documents\XAMPP\htdocs\Website\process.php on line 3 Notice: Undefined index: item in E:\Documents\XAMPP\htdocs\Website\process.php on line 5

Now I know my _POST array is not empty, because I can access it within my original form file. If I change the form action to "process.php" it works, but also automatically takes me to that page. What I want is for process.php to send data to my database while the form returns to its own page ready for more input. I have even literally copy and pasted the code from that example into files and attempted to run those, but I get similar errors so I think it might be an issue with XAMPP/Apache.

To address this, I have also tried this but post_max_size was already set to 8M, and as per another post I saw somewhere I added 'variables_order = "EGPCS"' in the line below. Still I am getting the same undefined index.

Is there something wrong with my code? Is it XAMPP/Apache? I have tried fresh installing XAMPP, but the issue still persists.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongyi1429 2015-12-02 07:30
    已采纳

    Your $_POST array will not be accessible on all pages. As much as I have understood your problem , You are trying to access $_POST array on some other file , and this can only be used in this file or the file you post to. You need to use SESSION in order to use these values

    After POST Assign your values like this

    $_SESSION["Category"] = $_POST['Category'];
    

    Then on any other page just use session_start(); on the top of page and then print your variable like

    echo "Selected Category is " . $_SESSION["Category"] . ".<br>";
    
    已采纳该答案
    打赏 评论
  • duanqian8867 2015-12-02 07:10

    You have not specified the location or file in the action attribute of form element. The file specified in the action attribute can have the values of the form in &_POST[] global array. To access the values across multiple page you should use the cookies

    打赏 评论
  • douji1058 2015-12-02 07:54

    Put check on

    category

    as you are only checking the

    item

    in array and if category is not present in array then it will give undefined index error so try the following ways

    $category = '';
    $item = '';
     if (isset($_POST['item']) && isset($_POST['Category'])){
        // Declare POST'd values into variables
        $category = $_POST['Category'];
        $item = $_POST['item'];
    }
    

    or if you want you can check them in different conditions

    $category = '';
    $item = '';
    if (isset($_POST['item'])){
        // Declare POST'd values into variables
        $item = $_POST['item'];
    }
    
    if (isset($_POST['Category'])){
        // Declare POST'd values into variables
        $category = $_POST['Category'];
    }
    
    打赏 评论

相关推荐 更多相似问题