Hi,
I'm using quickform to render and validate a form on a multi-purpose page. What i have, is a page that can either add an engineer (in my example), or edit a selected engineer, or view the details of a selected engineer.
Adding an engineer works just fine, but when it comes to editing an engineer, the hidden id value i've added on the edit page, doesn't get passed with the other values (like, name, email etc).
This is the basics of my code.
$form = new HTML_QuickForm('Create', post, basename(__FILE__));
$opts = array('size' => 30, 'maxlength' => 255);
$form->addElement('text', 'name', 'Name', $name_opts);
$form->addElement('text', 'email', 'Email', $email_opts);
$form->addElement('textarea', 'description', 'Profile text', $desc_opts);
$form->addElement('submit', 'submit-addEngineer', 'Submit');
if (isset($_GET['edit']))
{
$eng_id = $_GET['edit'];
// values will pull be pulled from database
$eng_name = 'name';
$eng_description = description';
$eng_email ='email';
$form->setDefaults(array('name' => $eng_name, 'email' => $eng_email, 'description' => $eng_description));
$form->addElement('hidden','id', $eng_id);
}
// Add validation rules
$form->addRule('name', 'Please enter a name', 'required', 'null', 'server');
$form->addRule('name', 'Please enter a name using only letters and / or numbers', 'minlength', '3', 'server');
$form->addRule('email', 'Please enter a valid email address', 'email', FALSE, 'server');
if ($form->validate()):
$data = $form->exportValues();
$eng_id = $data['id'];
$name = $data['name'];
$email = $data['email'];
$description = $data['description'];
$addEng = $data['submit-addEngineer'];
$editEng = $data['submit-editEngineer'];
/*
* If an engineer is added or updated
*/
if ($_POST['submit-addEngineer'] || $_POST['submit-editEngineer'])
{
// Add an engineer
if (isset($_POST['submit-addEngineer']))
{
// If successful redirects to engineer details
$form->process(addEngineer($name, $email, $description));
}
// Edit an engineer
if (isset($_POST['submit-editEngineer']))
{
// If successful redirects to engineer details
$form->process(editEngineer($name, $email, $description, $eng_id));
}
}
endif;
// Render the form
$formsource = $form->toHtml();
I've created callback functions to process the data, which works fine for adding an engineer, but the problem is that the hidden field $eng_id doesn't seem to be passed when the edit form is submitted. Looking at the code, the hidden field is there with the correct value, but if I call print_r($data) (which is the data exported from the form using $form->exportValues()), instead of processing the data, name, email and description are there, but the hidden eng_id value, and the submit button value aren't there.
Can anyone tell me where i've gone wrong.
Thanks
Richard.