Forums

can I UPDATE a table if Submit=Save?

samoht 13 Dec, 2008
I thought I would start a new thread to address just this one issue.

I have two forms (one to input info; one to confirm and submit) On the confirm form there is an "edit" button that sends the user back to the first form with all the data filled in (via a db query using the cf_id that I GET from the url) All good so far.

Now the user makes a change to the info and the submit button is labeled "Save". Here is where I need help.
How do I set the form to UPDATE the db table instead of INSERT-ing a new record?
Can I do this in the "On Submit" ??? If so, how?

Thanks,


Code on first form to tell if the form is being edited:
<?php $edit = true; 
$cf_id = $_GET['cf_id'];

 if($cf_id > 0){
global $mainframe;
$database =& JFactory::getDBO();
$database->setQuery( 'SELECT *  FROM jos_chronoforms_giftcard_submit WHERE cf_id = '.$cf_id.'');
$database->query();
$rows = $database->loadAssocList();
?>
<!-- form in edit mode with data filled in 
...
-->
		<div> 
			<input type="submit" name="submit" value="Save" /> 
			  
			<input type="reset" value="Reset Form" />
		</div> 
<?php }else{ ?>
<!-- blank form 
...
-->
		<div> 
			<input type="submit" name="submit" value="Process" /> 
			  
			<input type="reset" value="Reset Form" />
		</div>
<?php } ?>
GreyHead 13 Dec, 2008
Hi samoht,

I think that if cf_id is set to an existing value then ChronoForms will do a REPLACE - not certain but worth testing.

Bob

PS I still think this would be easier in a single form ;-)
samoht 13 Dec, 2008
Hi Bob,

yes, one form was easier for me.

Anyway, you are right - if cf_id is specified then ChronoForms does a replace (very nice). I still have one little problem of making sure I populate the second form with the right info. Currently I grab the last row of the table - but that could be broken if multiple users are filling out the form and one goes to "edit", when he returns to the confirm form he will get the other persons info.

So can I add the cf_id to the url in "Edit" mode?
Max_admin 14 Dec, 2008
Hi samoht,

so your problem is to get the record which has been just saved with this user ? show me your auto generated code!

Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 15 Dec, 2008
Hi samoht,

Not sure I completely understand this - but probably you should use the uid (the long random key) from the entry to get the correct record back - pass this form one form to the other in some way. Grabbing the last record is - as you - say- too open to error.

Bob
samoht 15 Dec, 2008
Hi Max,

Here is the Auto gen code for the first form:
<?php
		if($paramsvalues->dbconnection == "Yes"){
			$user = JFactory::getUser();			
			$row =& JTable::getInstance("chronoforms_giftcard_submit", "Table");
			srand((double)microtime()*10000);
			$inum	=	"I" . substr(base64_encode(md5(rand())), 0, 16);
			JRequest::setVar( "recordtime", JRequest::getVar( "recordtime", date("Y-m-d")." - ".date("H:i:s"), "post", "string", "" ));
			JRequest::setVar( "ipaddress", JRequest::getVar( "ipaddress", $_SERVER["REMOTE_ADDR"], "post", "string", "" ));
			JRequest::setVar( "uid", JRequest::getVar( "uid", $inum, "post", "string", "" ));
			JRequest::setVar( "cf_user_id", JRequest::getVar( "cf_user_id", $user->id, "post", "int", "" ));
			$post = JRequest::get( "post" , JREQUEST_ALLOWRAW );			
			if (!$row->bind( $post )) {
				JError::raiseWarning(100, $row->getError());
			}				
			if (!$row->store()) {
				JError::raiseWarning(100, $row->getError());
			}
			global $row_jos_chronoforms_giftcard_submit;
			$row_jos_chronoforms_giftcard_submit = $row;
		}
		?>
		
Max_admin 15 Dec, 2008
Hi,

just after the record is saved a new global variable is set with the record, use it to get the record id :

global $row_jos_chronoforms_giftcard_submit;
$yourid = $row_jos_chronoforms_giftcard_submit->cf_id;


Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
samoht 15 Dec, 2008
Thanks Max,

Were would I call $yourid ??
Max_admin 15 Dec, 2008
it depends on what do you need to do with the data of the last record saved...why did you needed it ?
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
samoht 16 Dec, 2008
I need it for users going back to edit their info after making it to the confirmation form. I want to be sure to get the correct info on each form so that no security problems arise.

I already have the "Confirm" form passing back the correct cf_id to the first form for editing. My problem is once the edit is done I need to save the info and go back to the "Confirm" form with the correct info (not just the latest table entry as I have now)
Max_admin 16 Dec, 2008
Hi samoht,

this issue is very dependent on your forms logic, I can't understand it all but I think you can go to your confirm form as normal after saving the data ?

Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
samoht 16 Dec, 2008
Hi Max,

at the top of the confirm HTML code I have this snippet:

<?php
// Check if the form is editable or not  
$readonly = ""; 
$edit = true; 

global $mainframe;
$database =& JFactory::getDBO();
if(!$_GET[cf_id]){
$database->setQuery( '
SELECT *
FROM jos_chronoforms_giftcard_submit
WHERE cf_id = (
SELECT max( cf_id )
FROM jos_chronoforms_giftcard_submit ) 
');
}else{
$database->setQuery( '
SELECT *
FROM jos_chronoforms_giftcard_submit
WHERE cf_id = '.$_GET[cf_id].' 
');
}
$database->query();
$rows = $database->loadAssocList();

?>


So you can see that I am using the max(cf_id) to return the row from the database. This is not the safest way because more than one person can be filling out the forms at the same time - which would cause a person going back to edit his form to come up with someone else's form to confirm. So I need a way to condition my query based on the user. The cf_id is not created until after the save of the form - so how do I access it from the "confirm" form?

You mentioned the auto generated code, but I don't understand where or how to call the new variable ?
$yourid = $row_jos_chronoforms_giftcard_submit->cf_id;


Is there some code that I should have in the On Submit of the first form??
Max_admin 17 Dec, 2008
Hi samoht,

in your main form add this in the onsubmit code:

global $row_jos_chronoforms_giftcard_submit;
$yourid = $row_jos_chronoforms_giftcard_submit->cf_id;
$session =& JFactory::getSession();
$session->set('cf_id', $yourid);



at the confirm form get the session value by :

$session =& JFactory::getSession();
$cf_id = $session->get('cf_id', '0');


let me know

regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
samoht 17 Dec, 2008
Thanks for the reply Max,

This always returns a value of 0 ?? on the confirm form
Max_admin 17 Dec, 2008
ok, lets test first that $yourid has the correct value when the first form is submitted, disable any redirects or so so you can write echo $yourid; and watch the result!

regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
samoht 17 Dec, 2008
Max!!!

You - most wonderful guy, you forced me to find my problem 😀

I think I am all set now. your session variable works as far as I can tell, I just had a misspelling that was causing problems 😶

Thanks a lot!
Max_admin 17 Dec, 2008
Goood!🙂
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.