onSubmit Events Code PROBLEM

the_fitz 08 Oct, 2010
Hi all,

The Pro noob again, is here. I have created a Chronoform that links several tables together and pulls the info correctly but I need to do some "cross table" posting. Basically, I need to take the $_POST results from my multiple querys and update one of the tables with $_POST results and create a new record in a different table, also from some $_POST results and some 'constant' data.

I am getting the $_post data into an object (I use var_dump() to confirm) and then try using the $db->insertObject and $db->updateObject functions to try to save, but it's not working. I know that I am doing something wrong or obtusely, but I cant figure it out.

Here is my onSubmit events code, after the email is sent.


<?php
///////////////////////////////////on subit code
$db = JFactory::getDBO();

$query ="SELECT COUNT(*)
    FROM ".$db->nameQuote('jml_rex_main')."
    WHERE ".$db->nameQuote('rex_card')." = ".$db->quote($_POST['rex_card'])."
    AND ".$db->nameQuote('rex_answer')." = ".$db->quote($secret_answer)."; ";
$db->setQuery($query);
$count = $db->loadResult();
if ($count === 0) {
   echo "<div align='center'><h2>Link Card to My Account</h2> The Card Number or the Secret Answer entered is incorrect, please try again. </div>";
   return;
   }
$main = (object) array(   
   'rm_id' => $_POST['rm_id'],   
   'id_link' => $_POST['id_link'],
   'status' => $_POST['status'],
   'anniv_date' => $_POST['anniv_date'],
   'exp_date' => $_POST['exp_date'],
   'rex_dlu' => $_POST['rex_dlu'],
   'rex_card' => $_POST['rex_card'],
   'rex_pid' => $_POST['rex_pid']   
 );
 var_dump($main);
  $db->updateObject('jml_rex_main', $main, 'rm_id');
 //$db->updateObject('tablename', $object_name, 'id_field');
 $tran = (object) array(   
   'tran_num' => 'NULL', 
   'tran_cardno' => $_POST['rex_card'],
   'tran_store' => 'WEB',
   'tran_oper' => 'USER',
   'tran_comment' => 'initial Card Activation'
   );
   var_dump($tran);
 $db->insertObject('my_rex_tran', $tran, 'tran_num');
//$db->insertObject('tablename', $object_name, 'id_field');
?>


Please help, with an example.

Thanks to ALL for your quick response.

The_fitz
GreyHead 08 Oct, 2010
Hi the_fitz,

I've never used either method before but looking at the docs your syntax looks OK. I'd probably turn site debug on next to see what queries are being executed.

Bob

PS There's a How-to doc on using the (similar) 'bind', 'store' & 'save' methods here.
the_fitz 08 Oct, 2010
Hi BOB,

And THANK YOU😀 that really got me going. NOW I feel like a Chronoforms Expert!

I wanted to share my onsubmit Events code (after email) for other noobs to take a lesson from.

This is for updating one table and inserting a record into a different table.

<?php
$db = JFactory::getDBO();
$my_info = $_POST['my_info'];
$query = "
	SELECT  tt1_id, tt1_field1 
		from jml_my_main 
		where my_info = $my_info ";
$db->setQuery($query);
if (is_null($posts=$db->loadRowList())) {
	$jAp->enqueueMessage(nl2br($db->getErrorMsg()),'error'); return;
}
$my_main = $db->loadAssoc();
$tt1_field1 = $my_main['tt1_field1'];
$tt1_id = $my_main['tt1_id'];
if ($tt1_field1 <> $_POST['tt2_field1']) {
   echo "<div align='center'><h2>Link info to My Account</h2> tt1_field1 does not match tt2_field1. </div>";
   return;
}

if (!class_exists('Tablemy_main')) {
	class Tablemy_main extends JTable {
		var $tt1_id = null;
		var $tt1_link = null;
		var $tt1_field2 = null;
		var $tt1_field3 = null;
		var $tt1_field4 = null;
		var $tt1_field5 = null;
		var $tt1_field6 = null;
		var $tt1_field7 = null;
		var $my_info = null;
		var $tt1_field8 = null;
		function __construct( &$database ) {
			parent::__construct( 'jml_my_main', 'tt1_id', $database );
		}
	}
}      
$MyForm =& CFChronoForm::getInstance("info_link");// form name   
$row =& JTable::getInstance("my_main", "Table");
JRequest::setVar( "tt1_id", $tt1_id);
JRequest::setVar( "tt1_link", JRequest::getVar( "tt1_link",  "post", "int", "" ));
JRequest::setVar( "tt1_field3", JRequest::getVar( "tt1_field3", 'A', "post", "string", "" ));
JRequest::setVar( "tt1_field5", JRequest::getVar( "tt1_field5", "post", "string", "" ));
JRequest::setVar( "tt1_field6", JRequest::getVar( "tt1_field6", "post", "string", "" ));
JRequest::setVar( "tt1_field7", JRequest::getVar( "tt1_field7", "post", "string", "" ));
JRequest::setVar( "my_info", JRequest::getVar( "my_info",  "post", "string", "" ));
JRequest::setVar( "tt1_field8", JRequest::getVar( "tt1_field8", "post", "string", "" ));         
$post = JRequest::get( "post" , JREQUEST_ALLOWRAW );   
if (!$row->bind( $post )) {
	JError::raiseWarning(100, $row->getError());
}            
if (!$row->store()) {
	JError::raiseWarning(100, $row->getError());
}
$MyForm->tablerow["jml_my_main"] = $row;

if (!class_exists('Tablehist_tran')) {
	class Tablehist_tran extends JTable {
		var $tran_id = null;
		var $tran_infolink = null;
		var $tran_field3 = null;
		var $tran_field4 = null;
		var $tran_field5 = null;
		function __construct( &$database ) {
			parent::__construct( 'jml_hist_tran', 'tran_id', $database );
		}
	}
}

$MyForm =& CFChronoForm::getInstance("info_link");// form name
$row =& JTable::getInstance("hist_tran", "Table");
JRequest::setVar( "tran_infolink", JRequest::getVar( "my_info", "post", "string", "" ));
JRequest::setVar( "tran_field3", JRequest::getVar( "tran_field3", 'placedefault', "post", "string", "" ));
JRequest::setVar( "tran_field4", JRequest::getVar( "tran_field4", 'persondefault', "post", "string", "" ));
JRequest::setVar( "tran_field5", JRequest::getVar( "tran_field5", 'commentdefault' , "post", "string", "" ));      
$post = JRequest::get( "post" , JREQUEST_ALLOWRAW );         
if (!$row->bind( $post )) {
	JError::raiseWarning(100, $row->getError());
}            
if (!$row->store()) {
	JError::raiseWarning(100, $row->getError());
}
$MyForm->tablerow["jml_hist_tran"] = $row;
?>


If you want more info, look at the Joomla http://docs.joomla.org/How_to_use_the_JTable_classdocs and http://docs.joomla.org/How_to_use_the_database_classes_in_your_script and http://docs.joomla.org/API15:JRequest/getVar. These docs and BOB's helped me a bunch!!

Thanks again!
The_fitz
GreyHead 09 Oct, 2010
Hi the_fitz,

Well done :-) Great that you got it working.

Bob

PS I claim most (but not all) of the responsibility for the "How to use database classes in your script" document. Pretty much my only contribution to the Joomla! docs and the one I use the most as I can never remember all of the methods.

PPS For other users wanting to save to more than one table. A ChronoForms DB Connection can save to multiple tables so you may not need this code.
the_fitz 10 Oct, 2010
Hi Bob,

Yes, I agree that for simple updates or inserts the Multiple table feature in ChronoForms works great, but I found when you need more granular control or advanced logic, then you have to do the coding yourself.

Great work on the Joomla docs, I now refer to them often.

I am SO GRATEFUL for all your GREAT support.😀

Thanks again.

The_fitz
This topic is locked and no more replies can be posted.