dousi4257 2017-10-27 09:18
浏览 56
已采纳

用PHP无法获得表单值

I am trying to get the value of a form (text field) with _POST, and store it to a text file but it doesn't work. Here's my code:

HTML

<form>
<input type="text" name="test" id="test" value="">
<input type="button" onclick="location.href='example.com/index.php?address=true';" value="ODOSLAŤ" />
</form>

PHP

if (isset($_GET['address'])) {

    $email = $_POST['test'];

    $myfile = fopen("log.txt","a") or die("Error.");
    fwrite($myfile, "
".$email);

    // this prints nothing
    echo $email;  
  }

I can't get the value of that text field. Nor GET nor POST doesn't work for me. What am I doing wrong?

  • 写回答

3条回答 默认 最新

  • dongxinpa3101 2017-10-27 09:38
    关注

    You are missing a post method in your form.

    <form method="post" action="example.com/index.php?address=true">
         <input type="text" name="test" id="test" value="">
         <input type="submit" value="ODOSLAŤ" />
    </form>
    

    You also have to change the following line.

    if (isset($_GET['address'])) {
    

    With

    if (isset($_POST['address'])) {
    

    You want to post after triggering an action as FreedomPride mentioned

    Conclusion

    You want to declare for example a post methodif you know that you would like to post that data in the future.

    As FreedomPride also mentioned :

    You are using a GET, but if a user does not input anything your script won't work, there by it is recommended to use a POST

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题