我有一个向另一个页面发送数据的表单。我想检查数据是否存在于目录中,然后重定向,否则显示错误。
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