By ncameron on
Hi there,
I was after some guidance on how to integrate JQuery once a form has been submitted. I have a form for which this is the submission function:
function nl_sub_form_submit($form_id, $form_values) {
simplenews_subscribe_user($form_values['email'], '1285', $confirm = TRUE);
drupal_goto('http://www.example.com/thanks-for-subscribing');
}
So basically it subcribes the email address to the newsletter and then re-directs to a thank you page. How can I Ajax this up a little, and re-load only the content in the block to give a thank you message?
Thanks so much for your help,
Neil
Comments
I am looking to do the same
I am looking to do the same type of thing!
Do it on the the submit of the form.
Using jquery you can intercept the form submission easily, grab the data from the form and then pass it on via ajax to handle the form submission. Here is how you would go about doing that:
Step 1. You need to create the js file that you are going to use. For now, we'll assume its in your custom build module called "mymodule/form.js".
Ok, now that our .js file is all set up, lets look at the mymodule.module file which is going to contain the form people can fill out.
And now lastly, make a theme function which will display everything you want on the page, including the form. The most important part of this is to make sure that the .js file gets included so that when you submit the form, the js will be activated.
This is the basic overview of how you would accomplish what you are looking to do. I typed this super quick so I apologize ahead of time of I tyoed any coding errors, but this should get you on the right path pretty quickly. If anyone caches errors I've made they can leave comments after me and clean it up. =)
Keep it up guys! Happy programming!
thank you
Hello,
This very simple example was very usefull to me !
I think there was this missing :
});after
return falseThanks ;)
Good catch!
Yes, there needs to be one more }); in there, glad you caught you that. =) Cheers
You must use form tokens, or
You must use form tokens, or I can POST millions of spam POSTs to your callback directly.
You're a prince and a
You're a prince and a gentleman! Thanks for taking the time to explain that, really appreciate it, I look forward to trying it on my next site.
Cheers,
Neil
--
Neil Cameron
Trying to submit a form without page refresh...
Hi there,
I am trying to understand how could i dynamicaly submit a form and take back the results without a page refresh. I created a really simple module which makes an addition of two numbers. I named the module addition and created two files.
addition.info
--------------
addition.module
------------------
This example works great but after the submition there is a page refresh and the results are displayed there. How could i do the same without the page refresh. As i have find out till now i need a .js file like the one that nrussell posted here and a call with drupal_add_js but i don't understand exactly the way... Could somebody help me with that?
Thanks in advance...
Same concept
Basically you are going to do the same thing I explained earlier in this thread. You might want to consider moving out of the $_SESSIONS super global as well and use drupals built in variabale_get and variable_set methods sense the data you are storing does not need to live for any specific session.
Ok so moving on. Lets take a look at the jquery that I posted.
The url: inside this $.ajax call is a very important item that needs to be set. It is what your module is going to depend on to know which function to call based on your menu hook system. So after looking at what you have set up, this is your menu hook function currently:
We need to add one more menu item to validate the ajax callback.
What this tells us is that the url /add-ajax is going to trigger the callback to the function add_ajax. So we need to update our jquery and make the URL point to "/add". Also, you need to update the jquery to point to your form. Sense the name of your form is add_form, the ID that drupal is going to render (unless you specify one specifically) is going to be add-form. So this would be your updated jquery:
Lets break this down. The new js file triggers an action on the submit of the form with the ID of "add-form". When that submit happens, we grab the two input field values number1 and number2 and pass them on the url /add in the $_POST super global. Now the next part. How do we handle / obtain this data? It all comes back to our menu hook when we added the new path to add-ajax. This is going to get called when the ajax sends its request and its going to call the function add_ajax. So lets add that now.
I am not going to go too much further into this situation, but what we just did was handle all the math needed for the ajax callback. The print function will be available in your ajax success: option in the (msg) variable. So, in your ajax you could check to see that msg is not equal to '' and then write the results to a div that you have available in the markup.
Hope this helps everyone. I wrote this early with no double check of my work so please forgive me if there are code typoes or errors. This is more of a reference to get you on the right path.
Laters.
Fully Functional Ajax Example - Block with ajax form submition
Hello again to everybody from beautiful Greece,
Thanks to the help of nrussell and the guidance of one of my co-workers ( manolism ) i finally found out the way to handle all the proccessing around the form submition with and without ajax. At the next lines i am going to demonstrate how can we create a simple module which creates a block with a form which makes an addition of two numbers. I am going to show you the way that this can be happened without ajax submition at the beggining and with ajax submition afterwards which means that we are going to do exactly the same but without the page refresh. So follow me and i hope that i am going to help developers who will try to do the same in the future...
First things first. We will create a module which we will call simpleadd. This module will be a really simple module which will create a block in which will happen a simple addition of two numbers. So let's make it reality:
simpleadd.info
--------------
simpleadd.module
-----------------
So our simpleadd module is ready. Just activate it and then set a region for the new block that it is creating and you will be able to make addition just right from the block. Ok ok i know that this was extremely easy for anybody to make it. But now let's take a look how can we do exactly the same thing but without a page refresh. So let's ajaxify it as nrussell showed us in his previous code parts. Let's put them all together and create our shiny new ajaxadd module that will take us one step forward...
ajaxadd.info
------------
ajaxadd.module
---------------
And finally we need one more file (form.js) which will do all the ajax job and will not allow the drupal to make a page refresh. Let's ajax:
form.js
-------
As you can see above i didn't do something more than nrussell said at his comments but i just put all the pieces together to help someone who is going to be in my position as i was a few days ago. I tried really hard to put all these things together and i think that drupal documentation need an example like that and an even more advanced if it is possible to make everybody understand how easily can be done some things.
The code that i posted here can be downloaded from my official site at petrellis and anybody who wants to see my code live working can check it out in my experimental site http://www.drupal6.gr
I finally want to thank again nrussell and manolism for their guidance over my questions!!!! Thanks guys and keep developing...!
Emmanouil Petrellis
Block with ajax form submition
It doesn't work for me. I installed ajax module and ajaxadd but still my site is refreshing the page and clearing the input values when I submit a form :(
Thanks,
Ramana