itsme
itsme

Reputation: 400

Getting Ajax reponse as null in php

I have a form in that I am trying to do inline editing and adding using AJAX call.

Firstly I am displaying data in HTML table. And then if enter data into text boxes and click on add button record adding displaying data in HTML table. After I click edit button data showing in the textboxes fine.

But I am getting the ajax response as null.

I couldn't figure it out.

This is my AJAX code PHP file:

$(function() {
  $(".scrollingTable  tbody  a").click(function() {
    //debugger;
    var link = $(this).attr('href');
    var arr = link.split('=');
    var id = arr[1];
    //alert(id);
    $.ajax({
      url: "insertgr.php",
      type: "POST",
      data: {
        cntid: id
      },
      success: function(datas) {
        var data = $.parseJSON(datas);
        $("#num").val(data.id);
        $("#namegr").val(data.vndr_cntname);
        $("#designation").val(data.designation);
        $("#mobilegr").val(data.vndr_cntmobile);
        $("#maildgr").val(data.vndr_cntmail);
      }
    });
  });
});

$(function() {
  $('.txtcbt a').click(function() {
    debugger;
    var cntname, designation, mobile, email, vndrid, id, cid;
    cid = $("#num").val();
    cntname = $("#namegr").val();
    designation = $("#designation").val();
    mobile = $("#mobilegr").val();
    email = $("#maildgr").val();
    vndrid = "<?php echo $selectid; ?>";
    //alert(cid);
    if (cntname == "" || designation == "" || mobile == "" || email == "") {
      alert("fields should not be empty");
    } else {
      $.ajax({
        url: "insertgr.php",
        type: "POST",
        data: {
          id: cid,
          name: cntname,
          dgnation: designation,
          mobileno: mobile,
          emailid: email,
          vid: vndrid
        },
        success: function(html) {
          var dat = $.parseJSON(html);
          alert(html);
          alert("it came to success");
          $("#num").val("");
          $('#namegr').val("");
          $('#designation').val("");
          $('#mobilegr').val("");
          $('#maildgr').val("");
        }
      });
    }
  });
});

This is file AJAX is calling:

<?php
    require('Assests/connection/connection.php');
    error_reporting(0);
    $vcntlist = "";
    if (!empty($_POST['cntid'])) {
        $id = $_POST['cntid'];
        $result = mysqli_query($conn, "SELECT `id`, `vndr_cntname`, `designation`, `vndr_cntmobile`, `vndr_cntmail`,`vndr_id` FROM `vndr_cntdtls`where id=$id");
        $rowcount = mysqli_num_rows($result);
        if ($rowcount > 0) {
            $row = mysqli_fetch_array($result);
            $vcntid = $row['id'];
            $cntname = $row['vndr_cntname'];
            $cntdesignation = $row['designation'];
            $cntmobile = $row['vndr_cntmobile'];
            $cntmail = $row['vndr_cntmail'];
        }
    }
    if (!empty($_POST['name']) && !empty($_POST['dgnation']) &&
        !empty($_POST['mobileno']) && !empty($_POST['emailid']) &&
        !empty($_POST['vid'])) {
        $id = $_POST['id'];
        $name = $_POST['name'];
        $degination = $_POST['dgnation'];
        $mobile = $_POST['mobileno'];
        $email = $_POST['emailid'];
        $vndrid = $_POST['vid'];
        if (empty($_POST['id'])) {
            $query = mysqli_query($conn, "INSERT INTO `vndr_cntdtls`(`vndr_cntname`, `designation`, `vndr_cntmobile`, `vndr_cntmail`, `vndr_id`) VALUES ('$name','$degination','$mobile','$email',$vndrid)");
        } else {
            $update1 = mysqli_query($conn, "UPDATE `vndr_cntdtls` SET `vndr_cntname`='$name',`designation`='$degination',`vndr_cntmobile`='$mobile',`vndr_cntmail`='$email' WHERE id=$id") or die(mysqli_error($conn));
        }
        $result = mysqli_query($conn, "SELECT  DISTINCT `id`, `vndr_cntname`, `designation`, `vndr_cntmobile`, `vndr_cntmail`,vc.vndr_id FROM `vndr_cntdtls` vc INNER JOIN vendors v ON vc.vndr_id=$vndrid") or die(mysqli_error($conn));
        $rowcount = mysqli_num_rows($result);
        if ($rowcount > 0) {
            while ($row = mysqli_fetch_array($result)) {
                $vcntid = $row['id'];
                $cntname = $row['vndr_cntname'];
                $cntdesignation = $row['designation'];
                $cntmobile = $row['vndr_cntmobile'];
                $cntmail = $row['vndr_cntmail'];
            }
        }
    }
    echo json_encode($row);
?>

Upvotes: 1

Views: 71

Answers (1)

Bryan Bojorque
Bryan Bojorque

Reputation: 168

There could be a lot of ways it is not working.

First: You need to put a application/json in your php so it can be compatible with the browser you are using.

header('Content-Type: application/json');
echo json_encode($row);

Second: Why are you doing a while loop and assigning it into an unused variable?

You can simplify it by doing:

$row = mysqli_fetch_array($result);
header('Content-Type: application/json');
echo json_encode($row);

Unless it is multiple rows then:

$rowcount = mysqli_num_rows($result);
$rows = array();
if ($rowcount > 0) {
  while ($row = mysqli_fetch_array($result)) {
  $rows[] = $row;
 }
}
header('Content-Type: application/json');
echo json_encode($row);

Third null values usually appears when a variable you are trying to use is not initialized. By having the error_reporting turned off it does not display the error.

error_reporting(true);

Fourth Check also the logs for database error, I assume it has something to do with MySQL query not having to reach the $row initialization.

Fifth I believe you need to have it fetched as associative array for it to be useful $row = mysqli_fetch_assoc($result);

Upvotes: 1

Related Questions