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

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算