我有一個向另一個頁面發送數據的表單。我想檢查數據是否存在於目錄中,然後重定向,否則顯示錯誤。
php form在發送動作之前檢查數據是否存在
<form class="form-inline" method="POST" action="<?php checkMyFile(); ?>">
<select class="form-control" style="width:auto;" name="mydata">
<option value="id1">1</option>
<option value="id2">2</option>
</select>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
,我有我的上述HTML窗體頂部以下PHP函數:
<?php
funtion checkFile(){
if(!isset($_POST['mydata']) or !file_exists('../data/'.basename($_POST['mydata']).'.txt')){
echo $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
} else {
return "my/destination/page.php"
}
}
?>
,但目前它正顯示出沒有錯誤的白頁。 它不工作的原因是什麼?解決辦法是什麼?
Errrr,它不會那樣工作。您不能從'action'運行PHP函數。您必須運行腳本文件(xxx.php),在POSTED數據中傳遞一個參數,該參數將由新實例化的xxx.php腳本測試,如果設置PHP代碼將調用函數 – RiggsFolly
@RiggsFolly能否請您爲此提出一個答案?與你的解釋。謝謝 – passion