【问题标题】:Blank page on submit button提交按钮上的空白页
【发布时间】:2017-04-02 02:37:56
【问题描述】:

我的 html 页面有一些问题。我希望它在按下提交按钮后重新加载。我尝试使用 javascript 重新加载它,但它一直给我一个白页(在将我需要的数据保存在文件中之后)。

这里是html代码:

<html>
    <head>
        <title>Sample Web Form</title>
    </head>
<body>

<h1>Fill Out This Form</h1>


<form action="myprocessingscript.php" method="POST">
<p><i>Choose one or more of the available options that represent the current mental state of the System:</i></p>
  <input type="checkbox" name="mentalState[]" value="Knows user culture"> Knows User Culture<br>
  <input type="checkbox" name="mentalState[]" value="Knows concept "> Knows Concept<br>
    <input type="checkbox" name="mentalState[]" value="Knows that user knows concept"> Knows that User Knows Concept<br>


<p><i>Choose the corresponding action:</i></p>
  <input type="checkbox" name="action" value="cultureIdentif"> Culture Identification<br>
  <input type="checkbox" name="action" value="conceptIdentif"> Concept Identification<br>


<p><i>Choose one or more of the available options that represent the current mental state of the System following the action:</i></p>

  <input type="checkbox" name="mentalState2[]" value="Knows user culture"> Knows User Culture<br>
  <input type="checkbox" name="mentalState2[]" value="Knows concept"> Knows Concept<br>
    <input type="checkbox" name="mentalState2[]" value="Knows that user knows concept"> Knows that User Knows Concept<br>
  </br>

  <input type="submit" value="Save Data">

</form>
</body>
</html>

这里是php代码:

<?php


if(!empty($_POST['mentalState'])){
    // Loop to store and display values of individual checked checkbox.
    foreach($_POST['mentalState'] as $mentalState)
        ($mentalState1=$mentalState . ',' . $mentalState1);
    }

    if(!empty($_POST['mentalState2'])){
        // Loop to store and display values of individual checked checkbox.
        foreach($_POST['mentalState2'] as $mentalState)
            ($mentalState2=$mentalState . ',' . $mentalState2);
        }


        if(isset($mentalState1) && isset($_POST['action']) && isset($mentalState2)) {
            $data = $mentalState1 . '-' . $_POST['action'] . '-' . $mentalState2 . "\n";
            $ret = file_put_contents('matrix.txt', $data, FILE_APPEND | LOCK_EX);
            if($ret === false) {
                die('There was an error writing this file');
            }

    } else {
   die('no post data to process');
}

感谢您的帮助!

【问题讨论】:

  • header( "Location: $_SERVER['HTTP_REFERER']" ) ; 放在最后一个IF 语句中,当它正确完成时,它会将您重定向回您提交的页面。
  • 你能写文件吗?
  • 一些合理的代码缩进是个好主意。它可以帮助我们阅读代码,更重要的是,它将帮助您调试代码 Take a quick look at a coding standard 为您自己谋福利。
  • HTML 和 PHP 是否在 2 个单独的文件中?
  • 谢谢,我将编辑代码格式。是的。

标签: php html reload


【解决方案1】:

我认为您在这里缺少一个条件

你只有

if($ret === false) {
    die('There was an error writing this file');
}

尝试添加else看看

if($ret === false) {
    die('There was an error writing this file');
} else {
  header('Location: ' . $_SERVER['HTTP_REFERER']);
}

【讨论】:

  • @RiggsFolly 请查看我的更新,我已将您的代码添加到我的代码中
  • @KanishkaPanamaldeniya 这个语法对我有用:header('Location: ' . $_SERVER['HTTP_REFERER']) 谢谢!
  • @user2267486 不错。请将答案标记为已接受:)。我已经用你的语法更新了我的答案
  • 好的 DV 已删除。好多了,现在它回答了问题
  • @RiggsFolly,谢谢 :)
猜你喜欢
  • 1970-01-01
  • 2013-07-06
  • 1970-01-01
  • 1970-01-01
  • 2015-07-07
  • 2012-12-15
  • 2018-03-02
  • 1970-01-01
  • 2023-03-21
相关资源
最近更新 更多