Login Retreive lost passwordRegister
Search

Forum Index / Feedback / Processing Twice

[ This topic is solved ]

  Daniel Hollands 24 March 08 / 21:25  
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(400220);

// set-up form variables
$addEditForm false;
$deleteForm false;

$form->getJavascriptCode(false);

//make a new TemplatePower object
$tpl = new TemplatePower('./templates/adminfood.tpl');

// assign header template
$tpl->assignInclude('header''./templates/header.tpl');
// assign footer template
$tpl->assignInclude('footer''./templates/footer.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;

    }
    else 
// user id invalid
    
{
    
        
// display 'edit_error' block
        
$tpl->newBlock('edit_error');

    }

}
else if (
$_REQUEST['do'] == 'delete'// delete entry
{

    
// display 'delete' block
    
$tpl->newBlock('delete');

    
// 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;

    }
    else 
// user id invalid
    
{
    
        
// display 'delete_error' block
        
$tpl->newBlock('delete_error');

    }

}
else 
// list entries
{

    
// display 'list' block
    
$tpl->newBlock('list');

    
// query database for mailing list
    
if (mysql_num_rows($result mysql_query('SELECT * FROM mailinglist'))) // entries exist
    
{

        
// fatch results from query
        
while ($row mysql_fetch_object($result))
        {

            
// display 'list' block
            
$tpl->newBlock('list_bit');
            
//assign a values to variables
            
$tpl->assign('id',$row->id);
            
$tpl->assign('name',$row->name);
            
$tpl->assign('email',$row->email);

        }

    }
    else 
// user id invalid
    
{
    
        
// display 'edit_error' block
        
$tpl->newBlock('list_error');

    }

}

/* FORM CONTROL */
// add/edit form
if ($addEditForm)
{

    
// set-up fields
    
$form->hiddenField('id');
    
$form->textField('Name''name'FH_STRING2020);
    
$form->textArea('Address''address'FH_TEXT205);
    
$form->textField('Post Code''postcode'FH_STRING208);
    
$form->radioButton('Eat In?''eatin', array(true => 'yes',false => 'no'), nulltrue);
    
$form->radioButton('Take Out?''takeout', array(true => 'yes',false => 'no'), nulltrue);
    
$form->radioButton('Delivery?''delivery', array(true => 'yes',false => 'no'), nulltrue);
    
$form->textArea('Notes''notes'FH_TEXT205);
    
// add controlling hidden field
    
$form->hiddenField('do');
    
// submit button
    
$form->submitButton();
    
// set the 'after form submit' function
    
$form->onCorrect('processAddEdit');
    
// display the form
    
$content $form->flush(true);
    
}

// delete form
if ($deleteForm)
{

    
// set-up fields
    
$form->hiddenField('id');
    
$form->checkBox('Are you sure?''areYouSure'trueFH_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);

//print the result
$tpl -> printToScreen();

?>


Do you have any ideas?

  Daniel Hollands 27 March 08 / 15:36  
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.

  Daniel Hollands 27 March 08 / 18:33  
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?

  Daniel Hollands 27 March 08 / 18:42  
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

  Daniel Hollands 27 March 08 / 18:55  
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

  Top


powered by PHP-GLOBE   © 2004 - 2008 FormHandler. All rights reserved.   -   Open source license