Forums

Loading data from the DB table into a Form

hopemanjohn 19 Nov, 2012
Hi,

I am slowly getting to grips with the basics of chronoforms V4 but am stumped on this aspect!!

In the FAQ’s, under “How to load one row from a DB table into a form” the tutorial is very straight forward and works well. However, it stops short. There is nothing in the tutorial or anything I can find in the forum which gives any idea as how to get the value into the Request Parameter “token”.

Example given:

Index.php?option=com_chronoforms&chronoforms=user_details&token=ff2119500f1a4ec2318c210bf681b0fe

What I and I assume many others would like to know is how to pass a value for the token using a chronoform.

If the key DB field was serial_number how could you use a chronoform to get from the user the serial_number and pass it to the URL.

For Example:

A simple form which asks the user to input:

Label (Enter Serial Number) Field Name [serial_number]
[submit]

When submit is clicked the serial_number, let’s say it was 8888855555 is passed to the URL which now would read:

Index.php?option=com_chronoforms&chronoforms=user_details&token=8888855555

and as such open up the populated form with the data from the record whose serial number was 8888855555.

I am sure this is simple to many but to me I'm flummoxed!!
GreyHead 20 Nov, 2012
Hi John,

If the first form is just used to get the user ID and doesn't need to do anything else then set the action URL of the first form to the second form. The setting is called 'Submit URL' and is on the Form General tab which you can access by Clicking the Form Name link in the Forms Manager.

You just need to add index.php?option=com_chronoforms&chronoforms=user_details in the Submit URL box and then put serial_number in the Request Parameter box of the DB Record Loader (assuming here that serial_number is the name of the input on the first form).

ChronoForms will automatically get any values from the POST array of the first form (and the GET array from the URL) and make them available to the second form.

You should probably also add a Custom Code action to the start of the second form to check that the serial_number is present and redirect the user back to the first form if it isn't.

Bob
hopemanjohn 20 Nov, 2012
Hi Bob,

It’s just so easy when you know how. Perhaps, when you have time!! you might add this element to the FAQ tutorial on “How to load one row from a DB table into your form”. It would round of that tutorial very well.

I certainly will buy you a six-pack for your help, so far. In fact I just have.

However, in your post you suggested that a Custom Code action should be added to check if the serial_number is present in the DB.

You can probably guess my next question. Can you suggest a Custom Code?

John
GreyHead 20 Nov, 2012
Hi John,

Please try this:
<?php
if ( !isset($form->data['serial_number']) || !$form->data['serial_number'] ) {
  $mainframe->redirect('index.php?option=com_chronoforms&chronoforms=enter_serial', 'Please enter your serial number', 'error');
}
?>
and change the 'index.php?option=com_chronoforms&chronoforms=enter_serial' to the URL for the first form.

NB The parameter syntax for $mainframe->redirect() is 'url' (required); 'message' (optional); and 'message type' (optional) I think, 'info' (the default), 'error', and 'notice'

Bob

Later: I copied Max's tutorial from the PDF into the FAQs and added a little note about using other data sources for the token.
hopemanjohn 20 Nov, 2012
Hi Bob,

I bet you thought that you had just got rid of me!!

Well I have altered the code and inserted it at the start of the second form, as you instructed but when I enter a wrong serial number, into the first form, it returns me not to the first form with the error message but to the second form with the wrong serial number inserted in the serial_number field and with no error message.

I have tried both the following as the redirect URL addresses but it always redirects to the second forms URL:

<?php
if ( !isset($form->data['serial_number']) || !$form->data['serial_number'] ) {
  $mainframe->redirect('index.php?option=com_chronoforms&chronoforms=view_code', 'Please enter your serial number', 'error');
}
?> 


and

<?php
if ( !isset($form->data['serial_number']) || !$form->data['serial_number'] ) {
  $mainframe->redirect('index.php?option=com_chronoforms&view=form&Itemid=58', 'Please enter your serial number', 'error');
}
?>


( note view_code is the name of the first form. enter_serial is the name of the second form.)

It appears that the 'Submit URL' set in the first form is taking preference over the Custom Code set in the second form when it comes to redirection.

I am obviously doing something silly somewhere.

John
GreyHead 21 Nov, 2012
Hi John,

The first one of these (and probably the second) should do the correct redirection. I don't see anything there that would send you to the second form.

If this test is incorrect that would explain the behaviour:
if ( !isset($form->data['serial_number']) || !$form->data['serial_number'] ) {
Try adding some debug code to see if the 'if' clause is being run or not. (Comment out the redirect and add echo 'XXX'; is the simplest version.)

Bob
hopemanjohn 21 Nov, 2012
Hi Bob,

I have run the debug and see no errors but it still does not redirect the form back when the wrong serial number is entered.

I am trying this out on a test bed site which only has a home page, JCE editor and chronoforms loaded so there would be no problem for you to enter the site and have a look at the chronoform problem.

I will e-mail you privately the site, username and password to enter as the super administrator.

John H
GreyHead 21 Nov, 2012
Hi John,

Everything looks OK. That code is only checking for empty serial numbers - not wrong ones, you can only do that after you've checked the database. (And you can only test it by turning JavaScript off in your browser to disable the client-side validation.)

Please add a Event Loop action to the On Fail and No Record found events of the DB Record Loader action to handle the 'wrong code' errors.

Bob
hopemanjohn 21 Nov, 2012
Hi Bob,

Thanks a lot for all your help on this matter.

I have learned a lot through our interaction and if I may suggest to any member reading this post, if you are looking to have Bob answer any of your problems then be responsible and buy him at least a six-pack for his time and effort.

You should ask yourself 'would I do all this for next to nothing'. Remember, he like us has to make a living.

Again many thanks from a 67 year old apprentice web designer.

John H.
GreyHead 21 Nov, 2012
Hi John,

Thank you.

Thinking about it - you probably need ReDirect User actions instead of Event Loops as you want to send them back to the first form. Or you could use Custom Code actions again with just the $mainframe->redirect()

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