Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP $_POST接受不到数据,但$_GET可以接受数据

PHP $_POST接受不到数据,但$_GET可以接受数据

WBOY
WBOYasal
2016-06-06 20:14:131010semak imbas

新手一枚,代码如下,把post 改成 get 就没问题,但如果是post 就会提示
Notice: Undefined index: a in D:\xampp\htdocs\myphp\add.php on line 3

add.html

<code>


    <meta charset="UTF-8">
    <title>Title</title>


<form action="add.php" method="post">
     a:<input type="text" name="a"> <br>
     b:<input type="text" name="b"><br>
    <input type="submit" value="提交">

</form>


</code>

add.php

<code>   <?php if($_POST['a'] && $_POST['b']){
        echo $_POST['a'] + $_POST['b'];
    }else{
        echo ' 请输入数字';
    }
    ?></code>

回复内容:

新手一枚,代码如下,把post 改成 get 就没问题,但如果是post 就会提示
Notice: Undefined index: a in D:\xampp\htdocs\myphp\add.php on line 3

add.html

<code>


    <meta charset="UTF-8">
    <title>Title</title>


<form action="add.php" method="post">
     a:<input type="text" name="a"> <br>
     b:<input type="text" name="b"><br>
    <input type="submit" value="提交">

</form>


</code>

add.php

<code>   <?php if($_POST['a'] && $_POST['b']){
        echo $_POST['a'] + $_POST['b'];
    }else{
        echo ' 请输入数字';
    }
    ?></code>

我也是类似的问题,只有一种可能,就是你得phpstrom配置的端口与phpinfo()端口不符。先看一下phpinfo()?>的端口在哪里比如localhost,那么就配置phpstrom-》Tools-》Dployment-》Configuration-》
新建比如Name=localhost,然后设置Web sever root url为http://localhost,端口与phpinfo()端口一致即可

php.ini 中配置 enable_post_data_reading 为 On, 这样才会自动将 POST 数据填入 $_POST 数组中。

<code>; Whether PHP will read the POST data.
; This option is enabled by default.
; Most likely, you won't want to disable this option globally. It causes $_POST
; and $_FILES to always be empty; the only way you will be able to read the
; POST data will be through the php://input stream wrapper. This can be useful
; to proxy requests or to process the POST data in a memory efficient fashion.
; http://php.net/enable-post-data-reading
; enable_post_data_reading = Off</code>

判断是否输入a和b的值,应该使用:

<code>isset($_POST['a']) && isset($_POST['b']</code>

你的写法在a或b输入0值时,不会走相加分支。

打印 $_REQUEST数组,

如果查验是否有数据POST过来,如果没有确定的条件下,推荐在业务处理前先进行打印数据。
上面可以直接
var_dump($_POST);
看下是否有值。

post有好几种方式,你看你是什么方式提交的?

php配置问题吧

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn