Parent/Child Relations

TimJacobs 28 May, 2010
Ok, here is what I am wanting to do.

I have a form Like this

FirstName
LastName
Attending yes no
Guest yes No

I need to know if they select yes can I have one drop down that has

FirstName
LastName
Guest of <insert ID from database here>
GreyHead 28 May, 2010
Hi Tim,

Yes you can, the most usually way is to create the extra inputs as normal but set the containing div(s) to style='display:none;' then use JavaScript to switch to display:block when the box is checked.

Bob
TimJacobs 29 May, 2010
Thank you for that, But I guess I didn't quite explain my self good enough.

I basically want the Guest to have a 3rd Row and that would have the ID# from the First One.

So in the database you have

ID# (incremented +1)
First
last
Parent


so if someone fills out the form It will be like:

ID#:23456
First: Tim
Last: Jacobs
Parent: NULL
HaveGuest: Yes (When Checked Same Form Shows Again)
________________

ID#: 23457
First:John
Last:Doe
Parent:23456
HaveGuest: No


I hope that better explains it.
GreyHead 29 May, 2010
Hi TimJacobs,

In general you are better off assigning IDs after the form is submitted but if you want to do it in hte browser then the script to unhide the extra form will need to have an extra line to check the last id and add one to it. That's certainly possible but probably not necessary.

Bob
nml375 29 May, 2010
Hi TimJacobs,
You can get the insert_id from the JTable object created during DB storage. The object would generally be available from the "on submit - after email" code using something like this:
<?
$MyForm =& CFChronoForm::getInstance("formname");
$row = $MyForm->tablerow["jos_tablename"];
$id = $row->id
?>


You can show a new instance of the form using the showform() method of the same object:
<?
$MyForm->showForm("formname");
?>


There is still the issue of telling your form that we're re-displaying the form and that the insert_id shold be used for the parent value. I'd probably use the JRequest class with the setVar/getVar methods, or possibly the session storage:

Form HTML:
...
<input type="hidden" name="Parent" value="<? echo JRequest::getInt('Parent', 0)?>" />
...


On Submit - after email:
<?
$guest = JRequest::getString('HaveGuest', 'No')
if ($guest == 'Yes') {
  $MyForm =& CFChronoForm::getInstance('formname');
  $row = $MyForm->tablerow['jos_tablename'];
  JRequest::setVar('Parent', $row->ID);
  $MyForm->showForm('formname');
?>


You'll have to change 'formname' into the name of the actual form, and 'jos_tablename' to the name of the database table at a minimum, and probably tweak the code to your likings, but it should atleast get you started.

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