0

i was asked to perform ajax post request to get student_name and the student_gender from the last row.. I also want to get the whole data from the database as well. the student name and gender from the last row are "yyyyty" and "F" respectively. i want to the whole data and the extracted data(student_name and gender) to be separately in the console page.There was error when i performed ajax post request to the php script which states

   Status Code: 200

ErrorThrown: SyntaxError: Unexpected token { in JSON at position 1634

jqXHR.responseText:

[{"student_id":"1","student_name":"Ashfur","student_gender":"F","student_age":"19","student_religion":"Muslim","student_course_id":"1"},{"student_id":"2","student_name":"Irfan","student_gender":"M","student_age":"17","student_religion":"Islam","student_course_id":"4"},{"student_id":"3","student_name":"Alice","student_gender":"F","student_age":"21","student_religion":"Chinese","student_course_id":"2"},{"student_id":"4","student_name":"Mohit","student_gender":"M","student_age":"20","student_religion":"Christian","student_course_id":"6"},{"student_id":"5","student_name":"Susy","student_gender":"F","student_age":"27","student_religion":"Chirstian","student_course_id":"5"},{"student_id":"6","student_name":"Ida","student_gender":"F","student_age":"23","student_religion":"Islam","student_course_id":"3"},{"student_id":"7","student_name":"Abdul","student_gender":"M","student_age":"22","student_religion":"Islam","student_course_id":"1"},{"student_id":"8","student_name":"Ernest","student_gender":"M","student_age":"25","student_religion":"Chinese","student_course_id":"4"},{"student_id":"9","student_name":"Wei Ling","student_gender":"F","student_age":"23","student_religion":"Chinese","student_course_id":"2"},{"student_id":"10","student_name":"Ashtae","student_gender":"M","student_age":"23","student_religion":"Islam","student_course_id":"4"},{"student_id":"11","student_name":"Jasmine","student_gender":"F","student_age":"23","student_religion":"Chinese","student_course_id":"2"},{"student_id":"65656","student_name":"yyyyty","student_gender":"F","student_age":"65","student_religion":"anything","student_course_id":"009090"}]{"student_name":"yyyyty","student_gender":"F"}

my code is found below

in html file

<html>
<head>
<script type="text/javascript" src="/Cesium-1.34/ThirdParty/jquery-1.11.3.min.js"></script> 
</head>
<div id="resulte"</div>
<script type="text/javascript">
showData();
function showData()
{
    $.ajax({
        type: "post",
        url: "student.php",
        dataType: "json",
        data: {
            lastOnly: "true",
        },      
        success: function(data){
            console.log(data);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert('An error occurred... Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information!');
            $('#resulte').html('<p>Status Code: '+jqXHR.status+'</p><p>ErrorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p><div>'+jqXHR.responseText + '</div>');
            console.log('jqXHR:');
            console.log(jqXHR);
            console.log('textStatus:');
            console.log(textStatus);
            console.log('errorThrown:');
            console.log(errorThrown);
        },

    });
};

</script>
</body>
</html>

in the php script

        <?php

$conn = mysqli_connect('localhost','root','netwitness') or die ("Could not connect database");
$db = mysqli_select_db($conn,'abdpractice') or die ('Could not select database');

$result = mysqli_query($conn,"select * from student");
$json_array = array();
while ($row = mysqli_fetch_assoc($result))
{
    $json_array[] = $row;
}
    echo json_encode($json_array);
if (!isset($_POST["lastOnly"])){
} else {
    $response = [];
    $response['student_name'] = $json_array[count($json_array)-1]['student_name'];
    $response['student_gender'] = $json_array[count($json_array)-1]['student_gender'];

    echo json_encode($response);    
}
?>

my question is how to solve the error and how to get student name and the gender from the last row using ajax post parameters and show those data separatley from the whole data.. i want to echo 2 times which are showing the whole data by default and showing the extracted data... is it possible

  • Your json is invalid (seen from the js side) because on the php side you have two echo's : first time the array from the DB query, and the other one in your 'else' clause. – YvesLeBorg Aug 23 '17 at 05:28
  • but i want all the data to be displayed by default.. then when i performed the ajax post request, i want that extracted data to be shown –  Aug 23 '17 at 05:30
  • no matter what you want, you are getting what you sent : invalid json. Find a way to encode your stuff properly so that the front-end can get the data and do its thing. – YvesLeBorg Aug 23 '17 at 05:32

1 Answers1

1

You are sending invalid json responses from server.

echo json_encode($json_array[count($json_array)-1]['student_name']);
echo json_encode($json_array[count($json_array)-1]['student_gender']);
echo json_encode($json_array);

Together this will made an invalid json string. Instead of echoing them, first store them in an array then echo the whole array.

$response = [];
$response['student_name'] = $json_array[count($json_array)-1]['student_name'];
$response['student_gender'] = $json_array[count($json_array)-1]['student_gender'];
$response['whole_result'] = $json_array;

echo json_encode($response);

Hope this help.

MD Ruhul Amin
  • 4,386
  • 1
  • 22
  • 37
  • this `$response = {}` should be `$response = []` – vher2 Aug 23 '17 at 04:36
  • @vher2,@ruhul thank you so much!!!!! I am going to update my question as my updated code got problem –  Aug 23 '17 at 05:17
  • @ YvesLeBorg sorry this is my first time using this forum..... i did not read the rules of this forum........... Should i post the updated questions in this same question or should i create another questions –  Aug 23 '17 at 05:24
  • @Tariq : prefer a new question to updating an answered question. Also, read this answer correctly : you did not implement what was suggested (correctly) by ruhul. – YvesLeBorg Aug 23 '17 at 05:29
  • @ YvesLeBorg i know that... but i want to show all the data by default then show the ajax post request data –  Aug 23 '17 at 05:33
  • @ YvesLeBorg i have updated my php script and the error i got –  Aug 23 '17 at 05:40
  • I think you did not get the point. You've to understand what a `json` string is. Here in your latest code you are echoing again two json strings. It's totally wrong. `.... echo json_encode($json_array); ......... echo json_encode($response);` Why two echo ? – MD Ruhul Amin Aug 23 '17 at 08:37
  • @ruhul can you perhaps look at this link?? https://stackoverflow.com/questions/45853676/ajax-post-values-is-empty this is another question.. can you please help me –  Aug 24 '17 at 09:18