Is it possible to save to two tables from one form?

samoht 20 Feb, 2009
Hey Guy's,

I am getting in a bit deeper with a profile/user account page and the client now wants a editable form that pulls from two (or more) database tables. I thought about doing a tabbed form, but the client wants just one page.

Is it possible to save to multiple tables from one form?

Thanks for the help,
GreyHead 20 Feb, 2009
Hi samoht,

Yes no problem - except that you have to hand-code one of the saves. ChronoForms built-in code will only save to one table. Just add the code to do the other one in the OnSubmit box.

Bob
samoht 20 Feb, 2009
Awesome, Thanks Bob!

I assume that I will have to use Joomla code to do this save?
Can you show me an example of calling the table and using an "UPDATE" sql ??

Thanks agian!
Max_admin 20 Feb, 2009
Hi samoht,

indeed you can save to 2 tables using the DB connection tab, you can multi select more than 1 table, but you can't use one field as a relation to another field in the other table, I guess that's why Bob has suggested doing the 2nd one with a SQL

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

To tell the truth I didn't know that you could connect to more than one table - something new I learned today.

Bob
Max_admin 20 Feb, 2009
Hi Bob,

No problems, but as I said, they will be 2 completely unrelated records which I think may not be whats needed in this case!😉

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
vsbr 20 Feb, 2009
I have the same problem (post "1 form, 2 tables").
If I use "ChronoForms Autogenerated Code" to save data in the first table, what code add in the "OnSubmit box" to save data in the other table?
Bob tell me to use a global variable called "$row_chronocontact_form_name" to read the id of the first record but I don´t know how.
Can you give me an example?
GreyHead 21 Feb, 2009
Hi samoht & vsbr,

I think it goes like this.

Connect the main table to your form using DB Connection.

Use the Form Order tab to run OnSubmit After *after* the AutoGenerated Code

In the OnSubmit After box you'll need something like this:
<?php
global $row_chronocontact_form_name;
$cf_id = $row_chronocontact_form_name->cf_id;
$db =& JFactory::getDBO();
$query = "
    UPDATE `#__some_table`
        SET `field_name` = 'value',
        . .  .
        WHERE `cf_id` = $cf_id;";
$db->setQuery($query);
$db->query();
?>
Not complete, not tested and will need debugging!

Bob
vsbr 21 Feb, 2009
Thanks! This problem is solved.

Just change:
$cf_id = $row_chronocontact_form_name->cf_id;
GreyHead 21 Feb, 2009
Hi vsbr,

Thank you, I've updated my post with your correction.

Bob
samoht 24 Feb, 2009
Thanks a lot!

I am close, but I now have a weird problem with the user email returning a bunch of javascript??

When I just use the user object and:

<?php echo $user->get('email');?>

the result is :
<script language='JavaScript' type='text/javascript'> <!-- var prefix = 'ma' + 'il' + 'to'; var path = 'hr' + 'ef' + '='; var addy65893 = 'reformedstudent' + '@'; addy65893 = addy65893 + 'gmail' + '.' + 'com'; document.write( '<a ' + path + '\'' + prefix + ':' + addy65893 + '\'>' ); document.write( addy65893 ); document.write( '<\/a>' ); //-->\n </script><script language='JavaScript' type='text/javascript'> <!-- document.write( '<span style=\'display: none;\'>' ); //--> </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it <script language='JavaScript' type='text/javascript'> <!-- document.write( '</' ); document.write( 'span>' ); //--> </script>

Any idea how to strip this so I just get the email address?

Thanks
samoht 24 Feb, 2009
do I have to strip the @ and then place it back in??
GreyHead 24 Feb, 2009
Hi samoht,

You have the Joomla Email Cloaking PlugIn enabled. Go to the PlugIn manager and swap over the order of the ChronoForms and Email Cloaking PlugIns.

Bob
samoht 24 Feb, 2009
Thanks again Bob!

Another quick question though,

you posted:

    UPDATE `#__some_table`
        SET `field_name` = 'value',
        . .  .
        WHERE `cf_id` = $cf_id;";


I should be able to use {mytextboxid} as the 'value' correct?

or am I using the $_POST array?
GreyHead 24 Feb, 2009
Hi samoht,

I think you can use {mytextboxid} OK - try it and see.

But personally I prefer to use JRequest::getInt('mytextboxid', '', 'post') so that I know exactly what data I am getting.

Bob
samoht 24 Feb, 2009
Bob,

I might have been misleading - would you use the JRequest for every value that you're updating?

    UPDATE `#__some_table`
        SET `name` = JRequest::getInt('name', '', 'post'),`title` = JRequest::getInt('title', '', 'post'),
        . .  .
        WHERE `cf_id` = $cf_id;";


?? this does not seem right?
GreyHead 24 Feb, 2009
Hi samoht,

Yes you can, that's how the old version of ChronoForms used to do it.

Bob

PS I'd probably get the whole post array and validate selected fields but basically the same.
samoht 25 Feb, 2009
Bob,

Quick question about the field names from the main table and the $_POST names:

do they need to match?
(ie. username is the field name so do I need $_POST['username'])

because currently I named my inputs things like "uname" and "pword" for username and password - but I am afraid these wont work?
samoht 25 Feb, 2009
URRR!

so what I have in the onSubmit after is:

<?php
global $row_chronocontact_myProfile;
//uncomment below to see all the reg values from the form printed to screen.
echo "cf_returning_values: ".print_r($row_chronocontact_myProfile, true)."<br /><br />";
echo '<div class="key_page">';
$smuser = $row_chronocontact_myProfile->id;
$strLogin = $row_chronocontact_myProfile->username;
$strPassword = $row_chronocontact_myProfile->password_clear; 

//key re-gen stuff

$db =& JFactory::getDBO();
$query = '
	UPDATE `#__vm_product_license`
        SET `license_start` = "'.time().'", `key_code` = JRequest::getInt("lkey", "", "post")
        WHERE `user_id` = $smuser';
$db->setQuery($query);
$db->query();

//second update
$query ='
	UPDATE `#__users`
        SET 
		`username` = JRequest::getInt("uname", "", "post"), 
		`password` = JRequest::getInt("pword", "", "post"),
		`email` = JRequest::getInt("email", "", "post")
        WHERE `id` = $smuser;';
$db->setQuery($query);
$db->query();

	
?>

this brings up an empty print_r ??
So obviously nothing is getting updated on the tables

Help
GreyHead 25 Feb, 2009
Hi samoht,

Have you used the Run Order tab to have OnSubmit After run *after* the AutoGenerated Code?

Bob
samoht 25 Feb, 2009
yes, here is what I have for the run order:

autogenerated block = 2
on submit block = 3
plugins block = 1
GreyHead 25 Feb, 2009
Hi samoht,

Please take a look in the AutoGenerated Code tab window and you should see a statement lik global $row_chronocontact_myProfile; near the end. Is this identical to the one you have used?

Bob
samoht 25 Feb, 2009
Hi Bob,

No, the were not identical. So I changed the onsubmit to match the auto gen which is
global $row_jos_vm_user_info;

then I just added a
echo "cf_info: ".print_r($row_jos_vm_user_info, true)."<br /><br />";

but nothing shows up?

Isn't this var supposed to have everything from the form?
GreyHead 25 Feb, 2009
Hi samhot,

Yes it should have everything from the form in it.

Can you PM or email me a SuperAdmin logon??

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