简体   繁体   English

使用PHP进行Smarty表单验证

[英]Smarty form validation using PHP

I want to validate my form using PHP on Smarty template. 我想使用Smarty模板上的PHP验证表单。 I have searched in the web and here for some solutions, but nothing work in my case. 我已经在网上和这里搜索了一些解决方案,但对我而言没有任何作用。 I want to stay on the same page if some field is empty or filled with incorrect data. 如果某些字段为空或填充了错误的数据,我想保留在同一页面上。 The file "result.php" is the page where the correct submited data goes to. 文件“ result.php”是正确提交的数据所在的页面。 I have done it, using javascript , but i want to know how to do it with PHP in case of disabled javascript in the user browser and for more complex check run (white spaces, empty fields, incorrect characters...). 我已经使用javascript完成了此操作,但是我想知道如何在用户浏览器中禁用javascript以及更复杂的检查运行(空白,空白字段,错误字符...)的情况下使用PHP进行操作。 The posted PHP script does not do any validation and i get an undefined index: error. 发布的PHP脚本未进行任何验证,并且出现未定义的索引:错误。 I post a piece of my code. 我发布了一段代码。 Please, help. 请帮忙。 Thanks in advance! 提前致谢!

php code (index.php): php代码(index.php):

if (!empty($_POST)) {
if (!empty($scrubbed['name']) || !empty($scrubbed['age'])) {
    header("Location: result.php");
 }
else {
    $smarty->assign('error', 'Please fill out the form completely');
    $smarty->display('index.tpl');
 }
}

smarty code (index.tpl): 聪明的代码(index.tpl):

{$error}
<table width="600" align="center" cellpadding="5">
<form name="infoform" action="result.php" method="post">  
<tr>
    <td><div align="right">Name:</div></td>
    <td><input type="text" name="name" size="20"/></td>
</tr>
<tr>
    <td><div align="right">Age:</div></td>
    <td><input type="number" name="age" size="2" min="1" max="65"/></td>
</tr>
...  

php code (index.php): php代码(index.php):

Instead redirect just do validation here: 而是重定向,只需在此处进行验证即可:

if (!empty($scrubbed['name']) || !empty($scrubbed['age'])) {
    // do something with post data, validate it
}

smarty code (index.tpl): 聪明的代码(index.tpl):

To avoid message undefined index: error just do: 为了避免出现消息undefined index: error ,请执行以下操作:

{if isset($error)}

    {* do something here if You have error... *}

{/if}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM