首页 > 解决方案 > php 无法从 html 中获取值

问题描述

我有疑问,为什么我的 html 文件无法与 php 文件连接。

但我确信在我点击提交按钮后,<input type="hidden" id="t1_response" name="t1_response" value="">可以给出值。

但是,url 不会更改为 php 形式(如 tocsv.php?t1_response=...)。

php给了我一个通知:

注意:未定义索引:第 2 行 C:\xampp\htdocs\colors\tocsv.php 中的 t1_response

我该如何解决?

<script>
...
function pressSubmit() {
    $('#startTrial').hide();

    for (var i = 1; i <=3; i++) {
        rt = rt + parseFloat(document.getElementById("t"+i+"_rt").value);
    };
    if (document.getElementById("t1_response").value === "diff") {acc=acc+1};
    if (document.getElementById("t2_response").value === "diff") {acc=acc+1};
    if (document.getElementById("t3_response").value === "same") {acc=acc+1};


    document.write("<div>The average react time: " + rt/3 +" ms</div>");
    document.write("<div>Acc Rate:" + acc/3*100+"%</div>");


}

</script>

<style>
...
</style>


<!DOCTYPE html>
<html>
<body>
<h3>Remember the colors</h3>


Press 's' if the two displays are the same. Press 'd' if a color changed (different).<br>

<form action="tocsv.php" method="get">
<input type="hidden" id="t1_response" name="t1_response" value="">
<input type="hidden" id="t1_rt" name="t1_rt" value="">

<input type="submit" id="submitButton" name="submitButton" onclick="javascript:pressSubmit()">
</form>
</body>
</html>

pressSubmit() 包含计算平均数和 document.write() 以向用户显示值。

tocsv.php

<?php 
echo $_GET["t1_response"];
?>

标签: javascriptphphtml

解决方案


我已经解决了这些问题。

谢谢大家的帮助。

在这里,在 中pressSubmit(),我写了doucument.write(...),这使得页面被覆盖。因此,结果无法获得价值,我不知道为什么会这样。

因此,我使用alert以避免覆盖页面。一切正常。

再次感谢大家的帮助!


.


推荐阅读