0

my code is :

$('form').submit(function(){
                var email           = $('#email').val();
                var dataString      = 'email='+email;
                var datadata;
                $.ajax({
                    type     : "get",
                    url      : "checkEmail.php",
                    data     : dataString,
                    dataType : "html",
                    success  : function(result){
                                 datadata = result;

                              }   
                });

          alert(datadata);
          return false;
            });

why this datadata is undefine ?

i develope a from. and i must check duplicate email by ajax befor submit. it return catect result but form will submited.

Tarun Baraiya
  • 506
  • 1
  • 9
  • 30

2 Answers2

1

the ajax call is asynchronous, which means when the alert is fired, the ajax call hasn't completed yet.

try putting the alert on the ajax success function like this:

$('form').submit(function(){
                var email           = $('#email').val();
                var filename        = '<?php echo basename($_SERVER['PHP_SELF'], '.php'); ?>';
                var dataString      = 'email='+email+'&userlevel='+filename<?php echo isset($_GET['ID']) ? "+'&uid='+".$_GET['ID'] : '';  ?>;
                var datadata;
                $.ajax({
                    type     : "get",
                    url      : "checkEmail.php",
                    data     : dataString,
                    dataType : "html",
                    success  : function(result){
                                 datadata = result;
                                 alert(datadata);

                              }   
                });


          return false;
        });
Moshe Shaham
  • 15,448
  • 22
  • 74
  • 114
  • but i want return true false on submit is that possible ? – Tarun Baraiya Feb 21 '13 at 08:51
  • 1
    you can submit the form from the success: $('form').submit(). or make the ajax call synchronous with: "async: false". see here: http://stackoverflow.com/questions/1572610/ajax-jquery-synchronous-callback-success – Moshe Shaham Feb 21 '13 at 09:14
0

This is because the onsuccess is a async call.. you are trying to get datadata while it has not been defined yet. Create a function where you define datadata @ the onsuccess and pass it result so it will be called when it is available

GiveMeAllYourCats
  • 890
  • 18
  • 23