2017-06-16 26 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

相关问题