user1597122
user1597122

Reputation: 327

submit a form with jQuery function

i have a html page, which contains a form and i want when the form is successfully submited, show the below div:

<div class="response" style="display: none;">
  <p>you can download it<a href="{{ link }}">here</a></p>
</div>

i also have a jquery function:

    <script type="text/javascript">
        $(function() {
            $('#sendButton').click(function(e) {            
                e.preventDefault();
                var temp = $("#backupSubmit").serialize();
                validateForm();
                $.ajax({
                    type: "POST",
                    data: temp,
                    url: 'backup/',
                    success: function(data) {
                        $(".response").show();
                    }
                });
            });
        });

</script>

and in my views.py (code behind) i create a link and pass it to html page. i have:

def backup(request):
    if request.is_ajax():
        if request.method=='POST':
            //create a link that user can download a file from it. (link)
            variables = RequestContext(request,{'link':link})
            return render_to_response('backup.html',variables)
        else:
            return render_to_response('backup.html')
    else:
        return render_to_response("show.html", {
            'str': "bad Request! :(",
            }, context_instance=RequestContext(request))
backup = login_required(backup)

my problem: it seems that my view doesn't execute. it doesn't show me the link that i send to this page. it seems that only jQuery function is executed. i'm confused. how can i make both of them to execute(i mean jQuery function and then the url i set in this function which make my view to be executed.)

i don't know how to use serialize function. whenever i searched, they wrote that:

The .serialize() method creates a text string in standard URL-encoded notation and produces query string like "a=1&b=2&c=3&d=4&e=5.

i don't know when i have to use it, while i can access to my form field in request.Post["field name"]. and i don't know what should be the data which is in success: function(data) in my situation.

thank very much for your help.

Upvotes: 0

Views: 261

Answers (1)

Littm
Littm

Reputation: 4947

You have to get and display the data from your ajax post function, where data is the response you render through your DJango server, for example:

t = Template("{{ link }}")
c = Context({"link": link})
t.render(c):

Your JS / jQuery should become something like this:

<script type="text/javascript">
    $(function() {
        $('#sendButton').click(function(e) {            
            e.preventDefault();
            var temp = $("#backupSubmit").serialize();
            validateForm();
            $.ajax({
                type: "POST",
                data: temp,
                url: 'backup/',

                success: function(data) {
                    // 'data' is the response from your server
                    // (=the link you want to generate from the server)

                    // Append the resulting link 'data' to your DIV '.response'
                    $(".response").html('<p>you can download it<a href="'+data+'">here</a></p>');

                    $(".response").show();
                }
            });
        });
    });
</script>

Hope this helps.

Upvotes: 1

Related Questions