1
$('#registerForm').submit(
    function()
    {
        callAjaxSubmitMethod(this);
        //event.preventDefault();
    }
);

It is the function inside my dom ready.

function callAjaxSubmitMethod(form)
{
    $.ajax({
        type: "POST",
        url: "lib/registration_validate.php",
        data: $("#registerForm").serialize(),

        success: function(response)
        {
            alert("s"+response.status);
        },

        error:function(response)
        {
            alert("e"+response);
        }
    });
}

It is actual function definition.

My php contents are

<?php
include 'configdb.php';
session_start();
global $connection;
echo "oops";
if(isset($_POST['submit']) && $_POST['email']!='' && $_POST['password']!='')
{   //use empty....
   $email= $_POST['email'];
   $sql1 = "SELECT * FROM Users WHERE emailID = '$email'";
   $result1 = mysqli_query($connection,$sql1) or die("Oops");
   if (mysqli_num_rows($result1) > 0)
   {
        $_SESSION['email_exist']="Email Already Exists";
        //die({'status':'Email Already Exists'});
        //echo var_dump($_SESSION);
        //echo '{status:0}' ;
        //exit;
   }
  }
  ?>

I din't get any alerts. If i enable event.preventDefault() I am getting out of memory error in mozilla and sundefined alert in chrome.

Gibbs
  • 21,904
  • 13
  • 74
  • 138
  • Please give Proper code. PHP code we need full. – Pratik Joshi Apr 19 '15 at 14:05
  • 1
    As _always_ when you have an issue with AJAX requests: 1. check the http servers error log file. 2. check your browsers development console for javascript errors. 3. check the network tab inside your browsers development console and take a look at the AJAX request sent and its result. In 99% of all cases you will see a clear error description somewhere. From there it should be possible to fix that error and get a step further. But unless you _look_ at the error and _read_ it, everything you try is just _guessing_. – arkascha Apr 19 '15 at 14:06
  • Do u get 404 error in Network console in firebug? – Pratik Joshi Apr 19 '15 at 14:09
  • No I dint get any 404 in Network tab – Gibbs Apr 19 '15 at 14:11
  • Echo Something from PHP dude :) You have it commented. – Pratik Joshi Apr 19 '15 at 14:12
  • @arkascha Thanks for your points. I just checked Network tab. I see form data in the request header. In response my html is there. – Gibbs Apr 19 '15 at 14:12
  • @jQuery.PHP.Magento.com No dude. I just tried it. It doesn't work man. `echo '{status:0}' ;` is there and uncommented too – Gibbs Apr 19 '15 at 14:13
  • Debugging 101. Get rid of POST by changing the params to `$_GET` and then pass in the query manually. Then, you'll be able to figure out off the issue is server-side or client-side. Debugging something shouldn't be about "here's a load of potential problems, what's the issue" - you need to narrow it down to what you *know* and then work from there. In this case, knowing whether it's client-side or server-side is a significant issue. – Jimbo Apr 19 '15 at 14:43

1 Answers1

1

Use

if(isset($_POST['submit']) && $_POST['email']!='' && $_POST['password']!='')
{   //use empty....
   $email= $_POST['email'];
   $sql1 = "SELECT * FROM Users WHERE emailID = '$email'";
   $result1 = mysqli_query($connection,$sql1) or die("Oops");

   $response = array();

   if (mysqli_num_rows($result1) > 0)
   {
        $_SESSION['email_exist']="Email Already Exists";

        $response['status']='Email Already Exists';
   }
   else{
        $response['status']='Allis OK';
   }
   echo json_encode($response); die;
}

And in ajax , add:

dataType:'json'

Edit

For your info , how to set dataType :

$.ajax({
        type: "POST",
        dataType:'json',
        url: "lib/registration_validate.php",
        data: $("#registerForm").serialize(),

        success: function(response)
        {
            alert("s"+response.status);
        },

        error:function(response)
        {
            alert("e"+response);
        }
    });
Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
  • Thanks. `uncaught Exception: outofmemory` in mozilla's console tab. 0 requests in Net tab. But i am getting s [object][object] in chrome. Chrome is fine. i can parse json there. – Gibbs Apr 19 '15 at 14:21
  • Sure , and check http://stackoverflow.com/questions/25903625/error-handling-out-of-memory-exception-in-browser – Pratik Joshi Apr 19 '15 at 14:25
  • use dataType:"json" as i told – Pratik Joshi Apr 19 '15 at 14:25
  • Thanks for SO link. I din't use much data in my html. just only one form is there. – Gibbs Apr 19 '15 at 14:28
  • @GopsAB , hi calm down, listen , check Edited answer, – Pratik Joshi Apr 19 '15 at 14:30
  • Thanks. `localStorage.clear();` then checked `localStorage` in mozilla it says `0 items in the same storage` same error again. – Gibbs Apr 19 '15 at 14:37
  • Buddy its related to localstorage , you either need to ask a new question on it Or you can edit with your Localstorage code and Add localstorage tag in Question, – Pratik Joshi Apr 19 '15 at 14:38
  • And I am getting this error if i call that ajax function only. If i comment out that function, i don't get any errors – Gibbs Apr 19 '15 at 14:41
  • Thanks. I ll add a new one – Gibbs Apr 19 '15 at 14:42
  • Hi Check edit : use `echo json_encode($response); die;` – Pratik Joshi Apr 19 '15 at 14:43
  • Somehow that error is diappeared. But i dont get any alert in mozilla. No error in console and Net tab. In Net tab, i can see .js and .html calls. I get alert in chrome – Gibbs Apr 19 '15 at 15:02
  • Few errors. die("oops") is not a JSON and I started returning proper JSON like die('{"X":"Y"}') – Gibbs Apr 21 '15 at 05:50