By pbland on
Question: I have a form with a list of products. Next to each product is a radio button that when clicked, I want to store the value $_SESSION so I can later read all the clicked items and do some processing. I haven't used AJAX before, but my research points to needing to use this. Anyone care to offer any suggestions or if there's something in Drupal that will help me accomplish this?
Thanks,
Paul
Comments
Some questions for you,
Some questions for you, because I'm not 100% sure you need AJAX:
- How is this form made? Webform module? Drupal Form API? Custom HTML?
- Javascript will never work for all users/browsers/platforms. Is that a problem? If you handle the selected items after the form submission (not immediately onClick), you don't need javascript/Ajax - is that an option for you?
If you need to do this with AJAX, you could write a simple php file which takes posted values and writes them to the $_SESSION array. On your form page you include a js file which checks if a radio button is selected and uses the jQuery.post method (http://docs.jquery.com/Ajax/jQuery.post) to send the value to your php script.
The form is made with custom
The form is made with custom HTML, in a custom module. Ideally, I would like when a radiobutton is clicked to get the value stored in $_SESSION, as opposed to having the user click the radiobuttons for the products they want and then have to click a button (i.e. submit) to get the values in $_SESSION. Based on your response, I'm not sure if my ideal solution is possible. It sounds like I need to have posted values go to a php file. Am I understanding you correctly? Thanks again.
---------------------------------------------------------------
My drupal site: Vacation-Places
Well that is certainly
Well that is certainly possible in ajax. Drupal ships with JQuery and I would recommend using it to make your ajax call, it is really easy. I am not so sure what you want to access in $_SESSION though, because putting things in there is normally not good programming unless it is things that have to do with the session such as cookie key values etc. jquery.com
My reason for wanting to
My reason for wanting to store values in $_SESSION is I have a series of product pages the user can view. On each main product page I have radiobuttons for subproducts where they can request a sample and when they are done, I have a link in the header where they can send the list of samples they've selected. A very simple shopping cart. So I thought just putting the selected items in $_SESSION would be easy enough so on the submit page, $_SESSION items can be read and put into an email. Is this not a good use of $_SESSION?
---------------------------------------------------------------
My drupal site: Vacation-Places
That's fine
yes that is another valid usage as a shopping cart type thing is something that one would associate with a visiting session. Yeah like I said Jquery is included and loaded up in drupal by default. It is very easy to achieve this with it. If you look on jquery.com they have a very good tutorial that will have you doing all of this in 5 minutes.
Thanks
Thanks. I really appreciate the suggestions and I'm looking at jquery.com right now.
---------------------------------------------------------------
My drupal site: Vacation-Places
Using the session is fine,
Using the session is fine, but AJAX will complicate things, and may not be necessary. Without AJAX, the radio selection would be saved when the user submits the form page using a submit button. With the addition of AJAX, the selection could be saved when the user clicks the radio button. Since you have multiple pages, I'm assuming you'll have a submit button between them?
A good rule of thumb for AJAX is to make things work without it first, then add AJAX features using JQuery. The goal is to make it work both ways - because Javascript isn't always reliable. Think of AJAX as "bling bling" or an add on that makes things pretty.
http://www.trailheadinteractive.com