-3

This is the table I want to show data in td through id.

<tbody>
<td id="show_file"></td>
</tbody>

And This is the function which sends a request to EditDeleteLecture.php to retrieve data from the database.

function readRecords(){
    var readrecords = "readrecords";
    $.ajax({
        url:"ajax/EditDeleteLecture.php",
        type:"POST",
        data:{readrecords:readrecords},
        success:function(data,status){
            var user1 = JSON.parse(data);           
            $("#show_file").val(user1.file_name);
        }
    });
}

And this code is written in the EditDeleteLecture.php.

if(isset($_POST['readrecords'])){
    $displayquery = "select content.file_name,content.cont_id AS ID,
                            content.description,content.course_code,
                            content.upload_date,course.course_name 
                    from content 
                        join class on class.course_code=content.course_code 
                        join course on class.course_code=course.course_code 
                        join faculty on faculty.faculty_name=class.faculty_name 
                    where faculty.faculty_phone='$single_user' 
                    order by ID desc"; 

    $result = sqlsrv_query($conn,$displayquery);
    while ($row = sqlsrv_fetch_array($result)) {            
        $response1 = $row;
    } 
    echo json_encode($response1);
}

console.log(data) shows

this.{"0":"waqsa.pdf",
    "file_name":"waqsa.pdf",
    "1":1193,
    "ID":1193,
    "2":"aaaa",
    "description":"aaaa",
    "3":"CS-311",
    "course_code":"CS-311",
    "4":"2018-10-22",
    "upload_date":"2018-10-22",
    "5":"Programming Fundamental",
    "course_name":"Programming Fundamental"
} 
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
John Merry
  • 31
  • 10
  • 2
    What is is working here? [Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server?](http://jayblanchard.net/basics_of_jquery_ajax.html) – Jay Blanchard Oct 22 '18 at 14:18
  • 1
    Where is `$single_user` defined? – Patrick Q Oct 22 '18 at 14:19
  • $single_user is the name of SESSION variable. – John Merry Oct 22 '18 at 14:20
  • 1
    What session variable? Please include all relevant code in your question. Also explain in detail exactly what the difference is between the current result/behavior and the desired result/behavior. Are you getting errors? Have you checked for errors? – Patrick Q Oct 22 '18 at 14:22
  • No, It don't gives errors. the echo json_encode($response1); returns data. but I don't know how to display data in the table. – John Merry Oct 22 '18 at 14:25
  • And the session is assigned to $single_user. here is the code $single_user=$_SESSION['FT_id']; – John Merry Oct 22 '18 at 14:26
  • So your PHP is working correctly and this is just a Javascript question? – Patrick Q Oct 22 '18 at 14:27
  • 1
    To load something into a `` Examples here https://stackoverflow.com/questions/2163558/how-to-insert-text-in-a-td-with-id-using-javascript _HINT_ You dont use `.val()` as ``'s dont have a value attribute – RiggsFolly Oct 22 '18 at 14:28
  • yes. PHP works correctly – John Merry Oct 22 '18 at 14:29
  • I doubt php is working correctly, you're overwritting the result when looping the rows – Cid Oct 22 '18 at 14:29
  • Would you show us the result of `console.log(data);` in `success` ? `$.each(data ...` might be what you are looking for. – Cid Oct 22 '18 at 14:32
  • @Cid console.log(data) shows this.{"0":"waqsa.pdf","file_name":"waqsa.pdf","1":1193,"ID":1193,"2":"aaaa","description":"aaaa","3":"CS-311","course_code":"CS-311","4":"2018-10-22","upload_date":"2018-10-22","5":"Programming Fundamental","course_name":"Programming Fundamental"} – John Merry Oct 22 '18 at 14:35
  • 1
    John. Feel free to edit your question with extra information like that. Nobody can read stuff like that in a comment – RiggsFolly Oct 22 '18 at 14:36
  • Also use `sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)` then you will only get ONE version of the columns i.e. not the numerically indexed bits – RiggsFolly Oct 22 '18 at 14:40
  • [.text()](http://api.jquery.com/text/) may be what you are looking for – Cid Oct 22 '18 at 14:44
  • @RiggsFolly SQLSRV_FETCH_ASSOC gives an error "call to undefined function" – John Merry Oct 22 '18 at 14:46
  • Thats very odd You did out it like this right? `while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC) ) {` – RiggsFolly Oct 22 '18 at 14:48
  • @Cid thanks my problem solved by writing text(). thank you so much. – John Merry Oct 22 '18 at 14:52
  • 1
    You are welcome. Give a try to more searches before asking, this is how you can progress. – Cid Oct 22 '18 at 14:54
  • @Cid but it doesn't shows the date. – John Merry Oct 22 '18 at 15:15

1 Answers1

0

Thank you all. the problem is solved. i have just wite $("#show_file").text(user1.file_name); instead of

$("#show_file").text(user1.file_name);
John Merry
  • 31
  • 10