I want to validate my form using PHP on Smarty template. 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. 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...). The posted PHP script does not do any validation and i get an undefined index: error. I post a piece of my code. Please, help. Thanks in advance!
php code (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):
{$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):
Instead redirect just do validation here:
if (!empty($scrubbed['name']) || !empty($scrubbed['age'])) {
// do something with post data, validate it
}
smarty code (index.tpl):
To avoid message undefined index: error
just do:
{if isset($error)}
{* do something here if You have error... *}
{/if}