I have an html page(edituserdetails.html), where I have a form containing 3 textboxes: FirstName, LastName,Email and a submit button. On clicking submit button, I'm validating these 3 textboxes(inside a function " SaveEdit "). Below is my code:
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');
}`
Now, the problem is that the same function loads the textboxes with contents from the database(This code is also inside 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']);
Due to this, when I click 'Submit' button even with empty textboxes, page refreshes and fills textboxes from database. As a result, my validations aren't working properly. Please help. Below is my html(I have used Smarty variable $FirstName,$LastName and $Email for dislaying the respective data. $FirstNameEmpty, $LastNameEmpty etc. are the smarty variables containing error messages for validation.
<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>
If I understand correctly you have problem with fills. Try to do your select from DB only if form i success submited. By the way, do update before select.
success submited = all if
clauses are passed.
Example:
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']);
}
Check my below method. Initialize a flag with false, Inside each failed validation make it true.
Before your select query add a condition to only if there is no validation error.
<?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']);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.