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

使用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>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类