Changed at 24 March 08 / 21:26
Hi, I've just started to use FormHandler, and think it's fantastic, just what I was looking for....
I have a problem however, for some reason, once all the validation has processed correctly, rather than using the built in database features, I've written my own function to do things like add and edit the entries in the database.
It's all working fine, except for the fact that it's doing it twice.... each entry I submit gets added to the database twice. I can't explain it.
Here is my code:
Quote
<?php
// include config
include('./config.php');
// create a new FormHandler object
$form =& new FormHandler();
// form table settings
$form->setTableSettings(400, 2, 2, 0);
// set-up form variables
$addEditForm = false;
$deleteForm = false;
$form->getJavascriptCode(false);
//make a new TemplatePower object
$tpl = new TemplatePower('./templates/adminfood.tpl');
//let TemplatePower do its thing, parsing etc.
$tpl -> prepare();
// check what the user is doing
if ($_REQUEST['do'] == 'add') // add entry
{
// display 'add' block
$tpl->newBlock('add');
// set form with required values
$form->setValue('do','add');
// display add/edit form
$addEditForm = true;
}
else if ($_REQUEST['do'] == 'edit') // edit entry
{
// display 'edit' block
$tpl->newBlock('edit');
// query database for valid entry id
if (mysql_num_rows($result = mysql_query('SELECT * FROM food_listing WHERE id = "'.$_REQUEST['id'].'" LIMIT 0,1'))) // user entry valid
{
// fatch results from query
$row = mysql_fetch_object($result);
// set values on form
$form->setValue('id', $row->id);
$form->setValue('name', $row->name);
$form->setValue('address', $row->address);
$form->setValue('postcode', $row->postcode);
$form->setValue('eatin', $row->eatin);
$form->setValue('takeout', $row->takeout);
$form->setValue('delivery', $row->delivery);
$form->setValue('notes', $row->notes);
// set form with required values
$form->setValue('do', 'edit');
// display add/edit form
$addEditForm = true;
// query database for valid user id
if (mysql_num_rows($result = mysql_query('SELECT * FROM food_listing WHERE id = "'.$_REQUEST['id'].'" LIMIT 0,1'))) // user id valid
{
// fatch results from query
$row = mysql_fetch_object($result);
// set values on form
$form->setValue('id', $row->id);
// set form with required values
$form->setValue('do', 'delete');
// display delete form
$deleteForm = true;
// set-up fields
$form->hiddenField('id');
$form->checkBox('Are you sure?', 'areYouSure', true, FH_BOOL);
// add controlling hidden field
$form->hiddenField('do');
// submit button
$form->submitButton();
// set the 'after form submit' function
$form->onCorrect('processDelete');
// display the form
$content = $form->flush(true);
}
/* FORM FUNCTIONS */
// add/edit function
function processAddEdit($data)
{
// check what the user is doing
if ($_REQUEST['do'] == 'add') // add to database
{
// insert into database
mysql_query('INSERT INTO food_listing (name, address, postcode, eatin, takeout, delivery, notes)
VALUES ("'.$data['name'].'", "'.$data['address'].'", "'.$data['postcode'].'", "'.$data['eatin'].'", "'.$data['takeout'].'", "'.$data['delivery'].'", "'.$data['notes'].'")')
or die('Could not add entry to the database! Error: '.mysql_error());
return 'You\'ve successfully added the entry into the database.';
}
else if ($_REQUEST['do'] == 'edit') // edit in database
{
// update in database
mysql_query('UPDATE food_listing
SET name = "'.$data['name'].'", address = "'.$data['address'].'", postcode = "'.$data['postcode'].'", eatin = "'.$data['eatin'].'", takeout = "'.$data['takeout'].'", delivery = "'.$data['delivery'].'", notes = "'.$data['notes'].'"
WHERE id = "'.$data['id'].'"')
or die('Could not update entry in the database! Error: '.mysql_error());
return 'You\'ve successfully updated the entry in the database.';
}
}
function processDelete($data)
{
// delete from database
mysql_query('DELETE FROM food_listing
WHERE id = "'.$data['id'].'"')
or die('Could not delete entry from the database! Error: '.mysql_error());
return 'You\'ve successfully deleted the entry from the database.';
}
//assign a values to variables
$tpl -> assign('_ROOT.content',$content);
OK, nevermind, I think I've fixed the problem... I was using another class - http://phpuserclass.com/ - and it was using the double entries in the database.. I've moved it, and it's working again fine.
Hi, me again.... the problem is back... no idea what's causing it, all I know is that when I submit the form in firefox, it adds the entry twice, but when I submit it in IE, it only adds the entry once...
How or why would there be a diffrence between firefox and ie?
Just to add more confusion.. I've just uploaded to to my production server, and here it's only submitting to the database once... so I'm willing to admit that it might have something to do with my development server (i'm using XAMPP).. but I still don't understand why I would get diffrent results from IE then from firefox..
Please look at this... I'm really keen on using FH, but it needs to be reliable
Hi, me once again.. I'm so sorry about this, but I've found out what's causing the problems..
I'm using a an addon for firefox, web developer, which has a tool allowing you to check the validity of the xhtml.. it does this by contacting the W3C validator and sending the data..
The only thing I can imagine is that because this is running in the background, the form is in fact being submitted twice, once by me, and once by the validator... causing the problem..
this would explain everything....
please ignore all my posts... or even feel free to delete them :D