2017-07-17 60 views
1

我在表單中使用<?php echo $_SERVER['PHP_SELF']; ?>。 當我刷新頁面時提交表單,它一次又一次地提交表單。避免刷新頁面上的數據提交php ['PHP_SELF']

請幫助我避免在刷新頁面上重新提交表單。

這裏是PHP代碼:

if (isset($_POST["patientName"])){ 
    // set the PDO error mode to exception 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    $sql = "INSERT INTO `inpatient`(`patientId`, `patientName`, `guardianName`, 
    `age`, `gender`, `mobile`, `address`, `city`, `symptoms`, `reference`, `date`, `time`, `doctorName`, `email`) 
    VALUES('$patientId', '$patientName', '$guardianName', '$age', '$gender', '$mobile', '$address', '$city', '$symptoms', 
    '$reference', '$date', '$time', '$doctorName', '$email')"; 


    $conn->exec($sql); 
} 
    echo ' 
     <div style="position:absolute; bottom:0; left:0; z-index:1000; " class="alert alert-icon-left alert-success alert-dismissible fade in mb-2" role="alert"> 
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"> 
             <span aria-hidden="true">x</span> 
            </button> 
            <strong>Well done!</strong> You successfully entered the <a href="#" class="alert-link">Patient</a> data. 
           </div> 
    '; 
} 
    catch(PDOException $e) 
    { 
    echo $sql . "<br>" . $e->getMessage(); 
    } 

$conn = null; 



?> 

我不想使用頭。我在同一頁上打印成功消息

+0

向我們顯示此頁的代碼。 – Tajgeer

+2

可能的重複[最好的方式來避免由於刷新頁面提交](https://stackoverflow.com/questions/5690541/best-way-to-avoid-the-submit-due-toa-a-頁面刷新) – Ali786

+0

它是瀏覽器的預期行爲(在本例中發佈請求時重新創建了以前的請求),以便在處理髮布請求後更改您需要重定向的行爲。 – Filip

回答

0

請在提交表單後不要顯示回覆。只需將其重定向到任何其他頁面。

header('Location: /path/to/record'); 
exit; 
+0

我不想使用標題,我正在使用echo在同一頁上打印成功消息 –

+0

如果您不想使用標題,那麼您可以先處理數據後取消設置$ _POST變量。例如,未設置($ _ POST);然後在下一步中使用頭部('Location:'。$ _ SERVER ['PHP_SELF'])將頁面重定向到同一頁面中。 – Jigsdatta