我有一个html页面(edituserdetails.html),其中我有一个包含3个文本框的表单:FirstName,LastName,Email和一个提交按钮。点击提交按钮,我正在验证这3个文本框(在函数“ SaveEdit ”中)。 以下是我的代码:
if ($_POST['SaveUserDetails']) {
$Id = $args[2];
if(empty($_POST['FirstName']))
{
$this->smarty->assign('FirstNameEmpty',"Please enter your firstname!");
controller::display($this, 'tpl.edituserdetails.html');
}
if(empty($_POST['LastName']))
{
$this->smarty->assign('LastNameEmpty',"Please enter your lastname!");
controller::display($this, 'tpl.edituserdetails.html');
}
if(empty($_POST['Email']))
{
$this->smarty->assign('EmailEmpty',"Email field should not be empty!");
controller::display($this, 'tpl.edituserdetails.html');
}`
现在,问题是同一个函数在文本框中加载了数据库中的内容(此代码也在 SaveEdit 中):
$x = $args[2];
$x = mysql_real_escape_string($x);
$result = db::sql("Select contact_fname,contact_lname,contact_email from billing_buyer where id = '$x'");
$row = mysql_fetch_assoc($result);
$this->smarty->assign('FirstName', $row['contact_fname']);
$this->smarty->assign('LastName', $row['contact_lname']);
$this->smarty->assign('Email', $row['contact_email']);
因此,当我单击“提交”按钮时,即使使用空文本框,页面也会刷新并填充数据库中的文本框。因此,我的验证无法正常运行。请帮忙。 下面是我的html(我使用了Smarty变量$ FirstName,$ LastName和$ Email来消除相应的数据。$ FirstNameEmpty,$ LastNameEmpty等是包含用于验证的错误消息的智能变量。
<html>
<div>
<form action="" method="post" id="subscriptions" onsubmit="">
<input type="hidden" value="form" name="action">
<div class="form">
<h2><span>Edit User Details</span></h2>
<div>
<table id="im_list" class="primary_table" border="0" cellpadding="10px" cellspacing=0>
<tr>
<td>First Name</td>
<td class="bar_style"> <input type="text" name="FirstName" value="{$FirstName}"></td>
<td><font color="red">{$FirstNameEmpty}</font></td>
</tr>
<tr>
<td>Last Name</td>
<td class="bar_style"> <input type="text" name="LastName" value="{$LastName}"></td>
<td><font color="red">{$LastNameEmpty}</font></td>
</tr>
<tr>
<td>Contact email</td>
<td class="bar_style"><input type="text" name="Email" value="{$Email}"></td>
<td><font color="red">{$EmailEmpty}</font></td>
<td><font color="red">{$WrongEmailFormat}</font></td>
</tr>
<tr>
<td>
<input id="SaveUserDetails" name="SaveUserDetails" type="submit" value="Save Preferences">
</td>
</tr>
</table>
</div>
</div>
</form>
</div>
答案 0 :(得分:2)
如果我理解正确你有填充问题。如果表单成功提交,请尝试从DB中进行选择。 顺便说一句,在选择之前做更新。
成功提交=所有if
条款都已通过。
示例:
if(!empty($_POST['FirstName']) && !empty($_POST['LastName']) && !empty($_POST['Email']) ){
// update
// And select
$x = $args[2];
$x = mysql_real_escape_string($x);
$result = db::sql("Select contact_fname,contact_lname,contact_email from billing_buyer where id = '$x'");
$row = mysql_fetch_assoc($result);
$this->smarty->assign('FirstName', $row['contact_fname']);
$this->smarty->assign('LastName', $row['contact_lname']);
$this->smarty->assign('Email', $row['contact_email']);
}
答案 1 :(得分:1)
检查我的以下方法。用false初始化一个标志,在每次失败的验证中都将其设为true。
在您选择查询之前,仅在没有验证错误的情况下添加条件。
<?php
function yourFunctionName(){
if ($_POST['SaveUserDetails']) {
$Id = $args[2];
$error = false;
if(empty($_POST['FirstName']))
{
$this->smarty->assign('FirstNameEmpty',"Please enter your firstname!");
controller::display($this, 'tpl.edituserdetails.html');
$error = true;
}
if(empty($_POST['LastName']))
{
$this->smarty->assign('LastNameEmpty',"Please enter your lastname!");
controller::display($this, 'tpl.edituserdetails.html');
$error = true;
}
if(empty($_POST['Email']))
{
$this->smarty->assign('EmailEmpty',"Email field should not be empty!");
controller::display($this, 'tpl.edituserdetails.html');
$error = true;
}
if(!$error){
$x = $args[2];
$x = mysql_real_escape_string($x);
$result = db::sql("Select contact_fname,contact_lname,contact_email from billing_buyer where id = '$x'");
$row = mysql_fetch_assoc($result);
$this->smarty->assign('FirstName', $row['contact_fname']);
$this->smarty->assign('LastName', $row['contact_lname']);
$this->smarty->assign('Email', $row['contact_email']);
}
}
}