Posting variables from a list to a form that will be used on form submission

chozma

Member
Hi guys,

I would much appreciate some advice on how to go about the below... :) I think I have figured out the general approach but would like some advice on whether I have that approach right and maybe some help with the syntax if I have. So....

I have a custom button (labelled 'Add to mobs') on a list called 'Animals'. I have created the button using the javascript list plugin. When some rows are selected and that button is clicked I would like it to load up a form called 'Add to mobs'. The user needs to fill some details in on the form and when the form is saved I would like to call some bespoke php that saves the form data. The key thing is I need the form to make use of the ids that were selected on the 'Animals' list when saving.

I know how to write the php to save the forms, I can't figure out how to pass the variables into the form for use.

The approach I'm using is to use ajax to pass the selected ids from the list to the form. Using the firebug console I can see that ids is the array of values I want to pass/make use of in the form. Then I'm telling the form to load. I am trying to do this with the following JS...

Code:
        var url = "/index.php/mobs/form/24";
       
        new Request({url:url,
        method: 'post',
        data: {ids:ids},
        .done(function( data ) {
          alert( "Data Loaded: " + data );
        }).send();
       
        window.open ('/index.php/mobs/form/24','_self',false);

When the form is submitted I'm using the form php plugin to do the necessary actions with the data on form submission. However, I'm struggling to retrieve the variables in the php. The code I am using is:

Code:
        $ids = $_POST['ids'];
       
        var_dump($ids);exit;

At the moment this just outputs NULL which suggests to me I'm not actually managing to pass the variables into the form properly.

I don't know if AJAX works in the way I am trying to use it.... :confused: Ie posting the variables for use when a function is called a bit later...

Hope this description makes sense... Any ideas on how to do this or if there is an alternative approach? Thanks as always... Hannah :)
 
Why do you use JS/ajax?

I would take the php list plugin and open the form with ids appended in a URL param. PHP code e.g.
Code:
$ids = $app->input->get( 'ids', array(), 'array' );
$idstring = implode(',',$ids);
 
$your_url = "j33/fabrik-user-reg/form/17/?lids=".$idstring;
$your_msg = "ids are ".$idstring;
 
$app->redirect( JRoute::_($your_url), $your_msg);
 
Great thanks Troester - I appreciate your advice a lot! :) I'll try that in the morning...I'm in New Zealand so its almost bed time here.

I'm trying with JS/AJAX as I would like to find a way to keep the data hidden.
 
I'm not really clear what you are doing with this process, it seems wrong to me, or there are missing steps....

I don't see why you are doing an ajax call to submit the form first before opening it?

I think what you need to do is just open the form with :

Code:
// Replace this with the pop up form's element name which will store the ids:
var fullElementName = 'popupform___reference_ids';

 window.open ('/index.php/mobs/form/24?' + fullElementName + '=' + ids.join(',') ,'_self',false);

Then in your form have an element whose full name for example "popupform___reference_ids" , which will be used to store the selected row ids.

Then when the pop up form is saved it automatically saves the reference ids for you
 
Hi Rob, Thanks very much for coming back to me on this one. Javascript is a new one to me and when I did some research on the internet the only ways I could find to pass variables privately were with AJAX which is what led me down that route.

Then I've been thinking about this for a few days now and wondering do I really need the variables to be private. I guess not, they are only id strings after all.

So I'll give your approach a whirl and hopefully I can get it to work! :D

Thanks, Hannah x
 
Well, anything you are getting from the page isn't "private" anyway. Those id's are in the source of the page. And whether you use AJAX or a window.open(), or a simple link to another page with a query string, you are still just scraping them from the page and sending them over the wire to the server. You can protect the "over the wire" bit by using https, but regardless, any data you get from the list/table is already "public" (subject to whatever ACL's you have on the list defining who can load it, of course).

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Staff online

Members online

Back
Top