QuickForms: Two forms on one page
| From: | Stelio Gouveia | Date: | Thu, 04 May 2006 11:56:45 +0000 |
| Subject: | QuickForms: Two forms on one page | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-23926@lists.php.net to get a copy of this message | ||
I currently have two forms on one page/script.
Problem is: when i submit either one of the forms, the other forms rules
will complain.
example... i change my surname in the second form bellow, then the
password form complains about
1. Current password is required -> For old password field
2 .New password is required -> For new password field
But the updates still happen...
what i want to do is have these forms do actions independant of each
other but still remain on the same page...
any suggestions...
<?php
// The user password update form
$pwd_form = new HTML_QuickForm('frmPPwdUpdate', null,
'skyrove.php?cmd=user_profile');
$pwd_form->addElement('password', 'old_pwd', 'Old Password',
array('class'=>'text'));
$pwd_form->addElement('password', 'new_pwd', 'New Password',
array('class'=>'text'));
$pwd_form->addElement('password', 'confirm_pwd', 'Confirm
Password',
array('class'=>'text'));
$pwd_form->addElement('submit', 'submit', 'update');
// Rules
$pwd_form->addRule('old_pwd', 'Current password is required',
'required', '', 'client');
$pwd_form->addRule('new_pwd', 'New password is required',
'required',
'', 'client');
$pwd_form->addRule('new_pwd','Your password must be at least six
letters.', 'minlength', 6, 'client');
$pwd_form->addRule(array('new_pwd', 'confirm_pwd'), 'Your passwords
don\'t match', 'compare');
$pwd_form->registerRule('checkoldpassword', 'callback',
'checkPwd');
$pwd_form->addRule('old_pwd', 'Invalid Password',
'checkoldpassword');
if ($pwd_form->validate())
{
$pwd_form->process('update_user_password');
$pwd_form->freeze();
echo "Password Successfully updated";
}
else
$pwd_form->display();
function checkPwd($user_entered_password)
{
// Query the database here to see if the username is free
$current_pwd_qry = "SELECT password FROM users WHERE
username='".$_SESSION['username']."';";
$result = pg_query(DB, $current_pwd_qry);
$pwd_row = pg_fetch_array($result);
if ($pwd_row['value'] == $user_entered_password)
return true;
else
return false;
}
function update_user_password()
{
$update_user_qry = "UPDATE users SET
password='".$_POST['new_pwd']."' WHERE
username='".$_SESSION['username']."';";
pg_query(DB, $update_user_qry );
}
echo "<hr>";
$form = new HTML_QuickForm('frmProfileUpdate', null,
'skyrove.php?cmd=user_profile');
$form->addElement('text', 'firstname', 'Firstname',
array('class'=>'text',
'value'=>$user_details['firstname']));
$form->addElement('text', 'surname', 'Surname',
array('class'=>'text',
'value'=>$user_details['surname']));
$form->addElement('submit', 'submit', 'update');
function update_user()
{
// Forcing billing to be per megabyte; 1 = time : 2 = megs
$_POST['billing'] = 2;
$qry= "UPDATE users SET firstname =
'".$_POST['firstname']."',
surname = '".$_POST['surname']."' WHERE
username='".$_SESSION['username']."';";
pg_query(DB, $qry);
}
if ($form->validate())
{
$form->process('update_user');
$form->freeze();
echo "User details successfully updated";
}
else
$form->display();
?>