Save and Redirect after button click

ricslabb03

Member
Hi

I asked a similar question a few days ago, but have hence got further and run into a few more obstacles that constitute a new thread.

I need to submit a form from a custom button, but instead of redirecting to the respective list I want to redirect to another list. This I know how to do using a calc field that returns a html button and then using a php form plugin onAfterProcess to redirect the form. See code below:

In the calc field:

PHP:
$qiID = '{quote_items___id}';

$session =& JFactory::getSession();
$session->set( 'qiID', $qiID );

$button = '<button class="btn btn-success button save" name="Submit" type="submit">Edit Area Items</button>';

return $button;

In the php form plugin onAfterProcess:

PHP:
$session =& JFactory::getSession();
$qiID = $session->get( 'qiID', '' );
$myUrl = JURI::root().'index.php/area-items?qiid='.$qiID.'&tmpl=component';

header('Location: ' . $myUrl, true, 302);
exit();

The only problem with that set up is that I need to create a session variable of the id of the repeated row which contains the html button I clicked ie {quote_items___id}. Using the JFactory session variables doesn't work since it returns the id of the last repeat row, obviously since I'm using a form plugin to capture the value.

I would like to know if there is a way to save and redirect the form where the URL contains a session variable of the repeated group's rowid?

Thanks
 
I'm a little confused as to where you are putting that calc submit button - in the parent form or the repeat groups? If this doesn't help, attach a screen shot to clarify.

If it's in the repeat group you should assign the button a unique name and store {quote_items___id} (from that repeat group, right?) as the value. You can then check that name and value on submit.

E.g. for the calc...
PHP:
$qiID = '{quote_items___id}';
$button = '<button class="btn btn-success button save" name="qiID_submit" type="submit" value="'.$qiID.'">Edit Area Items</button>';
return $button;
Then to check for the value on submit...
PHP:
$qiID = isset($_POST['qiID_submit']) ? $_POST['qiID_submit']  : '' ;
if( (int) $qiID > 0 )
{
   // code (redirect?) here
}
I'm not quite sure I understand why you need to store the value to the session. But have a look at this page for using Joomla user state variables (the greatest feature in Joomla since sliced bread)... How to use user state variables.

It sounds like example 2 is what you would use (It will either set or get the session variable)
E.g. is you pass the qiID value in the url request - you can use this to retrieve it (or set it to 0 if it's not passed in the URL) when the form is loaded...
PHP:
$mainframe = JFactory::getApplication();
// retrieve the value of the state variable. First see if the variable has been passed
// in the request. Otherwise retrieve the stored value. If none of these are specified,
// the specified default value will be returned
// function syntax is getUserStateFromRequest( $key, $request, $default );
$quID =  $mainframe->getUserStateFromRequest( 'quID', 'quID', 0 );

I usually use the redirect function for php redirects.
PHP:
$app = JFactory::getApplication();
$message = JText::('Your redirect message, if needed');
$app->redirect(JRoute::_('index.php/area-items?qiID='.$qiID.'&tmpl=component', false), $message, 'notice');
 
Last edited:
Thank you so much Bauer, it worked!

The calc submit button was placed in the repeat group quote_items.

For other people who have this problem, this is what I did.

The button was to be used to redirect to another list based on the id of the repeat group. ie, if if the repeated group id of the button pressed was 54, I would first save the current quote (the parent) form and redirect to the other list's URL: "index.php/area-items?qiID=54&tmpl=component."

In the repeat group, quote_items, I created a calc element called quote_items___edit_area_items. This is the element where I placed the button code. Here is the code:

PHP:
$qiID = '{quote_items___id}';
$button = '<button class="btn btn-success button save" name="qiID_submit" type="submit" value="'.$qiID.'">Edit Area Items</button>';
return $button;

Now the parent table is called quote. In quote's form, I created a php form plugin onAfterProcess and placed this code in it:

PHP:
$app =& JFactory::getApplication();
$message = JText::_('Area Items');

$qiID = isset($_POST['qiID_submit']) ? $_POST['qiID_submit']  : '' ;

if( (int) $qiID > 0 )
{
  $app->redirect(JRoute::_(JURI::root().'index.php/area-items?qiid='.$qiID.'&tmpl=component', false), $message, 'notice');
}

This will take the button's value parameter (which is the quote_items___id) and use it to redirect the page.
 
The only issue with $qiID is that if a new repeat record is added $qiID defaults to the value of the last saved $qiID ie it duplicates the calc field. Plus the button code does not submit/fire if the record isn't saved first. Is there a way to save and redirect? or, at least, force the user to save first?
 
The only issue with $qiID is that if a new repeat record is added $qiID defaults to the value of the last saved $qiID ie it duplicates the calc field. Plus the button code does not submit/fire if the record isn't saved first. Is there a way to save and redirect? or, at least, force the user to save first?
Hmm, if the code is in 'onAfterProcess', the data should already be saved. Are you sure you put the php code in the 'onAfterProcess' of the parent form (not the repeat group form)?

As for the issue with the calc - is it set to calc on load?

It seems I ran into something similar before with repeat groups. I think what I did was use jQuery to set the value (id used in the button url) after the page is loaded.

JavaScript:
Fabrik.addEvent('fabrik.list.update', function(){
    var qiID;
    jQuery("button[name=qiID_submit]").each(function() {
        /* initialize the qiID from the id in each fabrikGroup */
        qiID = jQuery(this).closest(".fabrikGroup").find("#quote_items___id").val();
        /* set the value of the qiID_submit button to the id of the group it is contained in. */
        jQuery(this).prop('value',qiID);
    });
});

I'm not sure if it was by using 'fabrik.list.update' - but I know you can 'fix' the qiID by including similar code inside whatever fabrik event gets triggered on completion of the ajax add.
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top