Desire
Desire

Reputation: 593

How to handle json response from php?

I'm sending a ajax request to update database records, it test it using html form, its working fine, but when i tried to send ajax request its working, but the response I received is always null. where as on html form its show correct response. I'm using xampp on Windows OS. Kindly guide me in right direction.

<?php
    header('Content-type: application/json');
    $prov= $_POST['prov'];
    $dsn = 'mysql:dbname=db;host=localhost';
    $myPDO = new PDO($dsn, 'admin', '1234');

    $selectSql = "SELECT abcd FROM xyz WHERE prov='".mysql_real_escape_string($prov)."'";
    $selectResult = $myPDO->query($selectSql);

    $row = $selectResult->fetch();
    $incr=intval($row['votecount'])+1;

    $updateSql = "UPDATE vote SET lmno='".$incr."' WHERE prov='".mysql_real_escape_string($prov)."'";
    $updateResult = $myPDO->query($updateSql);

    if($updateResult !== False) 
    {
    echo json_encode("Done!");
    } 
    else 
    {
    echo json_encode("Try Again!");
    }
    ?>




function increase(id)
    {
         $.ajax({
            type: 'POST',
            url: 'test.php',
            data: { prov: id },
            success: function (response) {

            },
            complete: function (response) {
                var obj = jQuery.parseJSON(response);
                alert(obj);
            }
        });
    };

Upvotes: 0

Views: 246

Answers (2)

Adil Shaikh
Adil Shaikh

Reputation: 44740

$.ajax({
            type: 'POST',
            url: 'test.php',
            data: { prov: id },
            dataType: 'json',
            success: function (response) {
                // you should recieve your responce data here
                var obj = jQuery.parseJSON(response);
                alert(obj);
            },
            complete: function (response) {
                //complete() is called always when the request is complete, no matter the outcome so you should avoid to recieve data in this function
                var obj = jQuery.parseJSON(response.responseText);
                alert(obj);
            }
        });

complete and the success function get different data passed in. success gets only the data, complete the whole XMLHttpRequest

Upvotes: 1

Joshua Burns
Joshua Burns

Reputation: 8572

First off, in your ajax request, you'll want to set dataType to json to ensure jQuery understands it is receiving json.

Secondly, complete is not passed the data from the ajax request, only success is.

Here is a full working example I put together, which I know works:

test.php (call this page in your web browser)

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
  // Define the javascript function
  function increase(id) {
    var post_data = {
      'prov': id
    }

    $.ajax({
      'type': 'POST',
      'url': 'ajax.php',
      'data': post_data,
      'dataType': 'json',
      'success': function (response, status, jQueryXmlHttpRequest) {
        alert('success called for ID ' + id + ', here is the response:');
        alert(response);
      },
      'complete': function(jQueryXmlHttpRequest, status) {
        alert('complete called');
      }
    });
  }

  // Call the function
  increase(1); // Simulate an id which exists
  increase(2); // Simulate an id which doesn't exist
</script>

ajax.php

<?php
$id = $_REQUEST['prov'];

if($id == '1') { 
  $response = 'Done!';
} else {
  $response = 'Try again!';
}

print json_encode($response);

Upvotes: 0

Related Questions