conditional submit depending on ajax call

121 views
Skip to first unread message

mapapage

unread,
Aug 22, 2012, 5:56:30 AM8/22/12
to django...@googlegroups.com
Hi!I'm trying to perform an ajax call on the submit of a form. The form will be submitted depending on the call's result. 
I'm doing sth like that:

$("#myform").submit(function() {
    var rdatefrom=$('#id_rdatefrom').val();
    var arr_sdateText = rdatefrom.split("/");
    startday = arr_sdateText[0];
    startmonth = arr_sdateText[1]; 
    startyear = arr_sdateText[2];
    
      $.get(""+startday+"/"+startmonth+"/"+startyear+"/")function(data) {
        msg=data.msg;
        if(msg!=='None'){
         alert(msg);
         return false;}
        return true;
     }); 
 }); 
but the form is being submitted regardless the call's result while I get the monstrous:

Traceback (most recent call last):
  File "/usr/lib/python2.6/wsgiref/handlers.py", line 94, in run
    self.finish_response()
  File "/usr/lib/python2.6/wsgiref/handlers.py", line 135, in finish_response
    self.write(data)
  File "/usr/lib/python2.6/wsgiref/handlers.py", line 218, in write
    self.send_headers()
  File "/usr/lib/python2.6/wsgiref/handlers.py", line 274, in send_headers
    self.send_preamble()
  File "/usr/lib/python2.6/wsgiref/handlers.py", line 200, in send_preamble
    'Date: %s\r\n' % format_date_time(time.time())
  File "/usr/lib/python2.6/socket.py", line 300, in write
    self.flush()
  File "/usr/lib/python2.6/socket.py", line 286, in flush
    self._sock.sendall(buffer)
error: [Errno 32] Broken pipe

What's going on?


Tom Evans

unread,
Aug 22, 2012, 7:46:01 AM8/22/12
to django...@googlegroups.com
Your JS looks decidedly incorrect. You say the aim is to prevent the
form being submitted in certain scenarios, yet your form submit
handler neither returns true nor false. You return truthiness from
your anonymous AJAX handler, but that is not the same.

Secondly, your check to see whether to submit the form uses AJAX. The
first A is important there, the communication happens asynchronously.
This sequence of events occurs:

Form submit handler called
Form submit handler creates an AJAX request
AJAX request is submitted to the server
Form submit handler completes with no return value
Browser prepares to submit the form
Browser closes any open network connections the page has
Server attempts to write to closed socket, generating quoted exception
Browser submits form to server

You need to make your jquery AJAX call synchronous, and return
true/false from the appropriate location. "jquery synchronous ajax"
should give you all the information you need.

Cheers

Tom
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/7G1wG2673jQJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

mapapage

unread,
Aug 22, 2012, 8:34:54 AM8/22/12
to django...@googlegroups.com
I can see what you mean.
Meanwhile, I tried the following and it seems to work:

$("#myform").submit(function() {
var rdatefrom=$('#id_rdatefrom').val()
     var arr_sdateText = rdatefrom.split("/");
     startday = arr_sdateText[0];
     startmonth = arr_sdateText[1]; 
     startyear = arr_sdateText[2];
  
   $.get(""+startday+"/"+startmonth+"/"+startyear+"/") function(data) {

       msg=data.msg;
       if(msg!=='None'){
         alert(msg);
         return false;} 
       else {
         $("#myform").unbind('submit');
         $("#myform").submit(); }
    });
    event.preventDefault();
}); 
Is it wrong?
Reply all
Reply to author
Forward
0 new messages