duanmu2013 2013-06-26 17:14
浏览 40
已采纳

为什么我的PHP POST请求没有通过?

I'm trying to build a system that will give a user a random question, then send the user's answer and the correct answer to the next page via POST, without ever showing the user what the correct answer is. When FileB.php loads, var_dump($_POST); reads

array(1) {
  ["response"]=>
  string(32) "Whatever the user's response was"
}

Why doesn't what I have below work? Why isn't the ans post request going through?

FileA.php

<?PHP
function post($data) // from http://stackoverflow.com/questions/5647461/how-do-i-send-a-post-request-with-php
{
    $options = array(
        'http' => array(
              'header'  => "Content-type: application/x-www-form-urlencoded
"
            , 'method'  => 'POST'
            , 'content' => http_build_query($data)
        ),
    );
    $context  = stream_context_create($options);
}

post(array("ans" => "Correct Answer"));
?>
<HTML>
<HEAD>
<TITLE>Form</TITLE>
</HEAD>
<BODY>
<FORM METHOD="post" ACTION="FileB.php">
    <LABEL>What is the correct answer? <INPUT TYPE="text" NAME="response"/></LABEL>
</FORM>

FileB.php

<HTML>
<HEAD>
<TITLE>Results</TITLE>
</HEAD>
<BODY>
<?PHP
if ($_POST["ans"] == $_POST["response"])
{
    echo "You are correct!";
}
else
{
    echo "You're wrong!";
}
?>
</BODY>
</HTML>
  • 写回答

4条回答 默认 最新

  • duanpi5733 2013-06-26 17:39
    关注

    After you got Strat's suggestion working, an improvement might be to store the correct answer in a session variable instead of revealing it in the HTML source. You don't need a hidden field then. Example:

    FileA.php

    session_start();
    $_SESSION['answer'] = "....";
    

    FileB.php

    session_start();
    if ($_POST['response'] == $_SESSION['answer'])
    {
        echo "You're right.";
    }
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题