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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度