2016-11-18 115 views
0

我對我的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'); 
} 

感謝您的幫助!

+1

地方'標題(「位置:$ _ SERVER [」 HTTP_REFERER']「);'在你最後的'IF'語句中,當它完成而沒有錯誤時,它會將你重定向回你提交的頁面。 – Blinkydamo

+0

你能寫出這個文件嗎? –

+1

一些明智的代碼縮進將是一個好主意。它可以幫助我們閱讀代碼,更重要的是,它可以幫助您**調試您的代碼** [快速瀏覽編碼標準](http://www.php-fig.org/psr/psr-2/ )爲了您自己的利益。 – RiggsFolly

回答

0

我覺得你在這裏缺少一個條件

你只有

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

嘗試添加別的,看看

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

@RiggsFolly請看到我的更新,我加入你的代碼挖掘 –

+0

@KanishkaPanamaldeniya這語法爲我工作: header('Location:'。$ _SERVER ['HTTP_REFERER']) 謝謝! – janeloulou

+0

@ user2267486很好。請將答案標記爲已接受:)。我已經用你的語法更新了我的答案 –