2017-06-16 69 views
0

我有一個向另一個頁面發送數據的表單。我想檢查數據是否存在於目錄中,然後重定向,否則顯示錯誤。
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" 
    } 

} 

?> 

,但目前它正顯示出沒有錯誤的白頁。 它不工作的原因是什麼?解決辦法是什麼?

+0

Errrr,它不會那樣工作。您不能從'action'運行PHP函數。您必須運行腳本文件(xxx.php),在POSTED數據中傳遞一個參數,該參數將由新實例化的xxx.php腳本測試,如果設置PHP代碼將調用函數 – RiggsFolly

+0

@RiggsFolly能否請您爲此提出一個答案?與你的解釋。謝謝 – passion

回答

0

試試這個:

變化:

return "my/destination/page.php" 

要:

header('Location: my/destination/page.php'); 

或:

$url = sprintf(
    "%s://%s%s", 
    isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', 
    $_SERVER['SERVER_NAME'], 
    $_SERVER['REQUEST_URI'] 
); 

header('Location: '.$url.'/my/destination/page.php'); 

對此部分:

action="<?php checkMyFile(); ?>" 

它更改到你的代碼位於

示例PHP文件:

action="checker.php" 

和內部checker.php是:

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 { 

    header('Location: my/destination/page.php'); 
    } 
+0

嗯是的,但現在真的不包括主要問題 – RiggsFolly

+0

不,它仍然不工作,我得到完全白色的頁面... – passion

+0

@RiggsFolly哦,我錯過了他的部分代碼的行動。讓我在我的回答中添加我的建議 – hungrykoala

2

好吧,讓我們假設這代碼全部在一個叫xxx.php的文件中

首先,您在動作中所能做的只是告訴瀏覽器將哪些腳本發送給表單輸入,您無法在該腳本中指定特定功能。

<?php 
// while testing, always make sure error reporting is on 
error_reporting(E_ALL); 
ini_set('display_errors', 1); 

$errormsg = ''; 

// corrected spelling error of funtion 
function checkFile(){ 

    if(!isset($_POST['mydata']) or !file_exists('../data/'.basename($_POST['mydata']).'.txt')){ 
     return '<div class="alert alert-danger"> 
         Sorry there was an error sending your message. Please try again later 
        </div>'; 

    } else { 

     header("Location: my/destination/page.php"); 
     // exit a you are now sending a new page and this one is irrelevant 
     exit; 
    } 

} 

// was the form submitted, or is this just the page being initially loaded 
// NOTE: I gave the submit button a `name` attribute of `submit` 
if (isset($_POST['submit'])) { 
    $errormsg = checkFile(); 
}  
?> 
<-- Usual doctype/html/head/body tags ommitted for brevity --> 

<?php 
    // output the error message somewhere sensible on your page 
    // which may not actually be here 
    if ($errormsg != '') { 
     echo $errormsg; 
    } 
?> 
<form class="form-inline" method="POST" action="xxx.php" enctype="multipart/form-data"> 
    <select class="form-control" style="width:auto;" name="mydata"> 
     <option value="id1">1</option> 
     <option value="id2">2</option> 
    </select> 
    <button name="submit" type="submit" class="btn btn-primary">Submit</button> 
</form> 
+0

它不工作。即使數據存在,我也會得到錯誤信息 – passion

+0

您確定需要'../ data /'中的2個點' – RiggsFolly

+0

可能這項工作'!file_exists('data /'。basename($ _ POST [' mydata'])' – RiggsFolly

相關問題