0

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);
            }
        });
    };
1
  • You're just returning a string, you don't need to json_encode it, just make your dataType : "text" Commented Mar 25, 2013 at 20:22

2 Answers 2

1
$.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

Sign up to request clarification or add additional context in comments.

Comments

0

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);

1 Comment

no error! i'm wondering why its working fine on html form! and not working with ajax request :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.