简体   繁体   中英

ajax feedback submit without page reload

I am new to ajax and i am trying to submit a form and the page should not reload. I am doing it in django.

html

#...
<form id="feedback_submit_form" action="" method="post">
{%csrf_token%}
<textarea id="feedback_text" name="feedback_text"></textarea>
<input type="submit" id="feedback_submit" value="Send feedback"/>
</form>
#...

javascript

$('#feedback_submit_form').submit(function(e){
$.post('**WHAT URL SHOULD BE HERE**', $(this).serialize(), function(data){
 ***WHAT SHOULD I DO HERE***
   });
e.preventDefault();
});

views.py

def ajax_view(request):
    if request.method=='POST':
        feedback=request.POST.get('feedback_text')
        #save feedback to database
        **DO I NEED A RETURN HERE**

I want to do that the form for the feedback is submitted and the current page should not refresh and the data in the feedback form should be saved in to the database.

How can i do it. Hope u understood it.

Thanks in advance...

The URL can be whatever you want: one solution is to use the form's action attribute with $(this).action .

You don't really specify what you want to happen, but one good pattern is for your view to render a partial template that formats the result, and the Ajax to simply insert that into the page with $(surrounding_div).html(result) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM