I have a issue with JSON response. In am getting undefined values in JSON response. Here is my JS and PHP Code. I have tried every thing but still it giving me undefined value and when i try to console(response) then it displaying complete html. Please Help me if anybody can just annoying this thing for 2 hours
P.S: Sorry For Bad English. Here is the Code Of JS and PHP
//Custom JS To Show Modal Update Box
function updateStudentShow($id){
$.ajax({
method:"GET",
url:"view.php",
data:{id:$id},
datatype:"JSON",
success:function(data){
$("#update_name").val(data.name);
$("#update_about").val(data.des);
alert(data.name);
console.log(data);
}
});
$("#update").modal("show");
}
//PHP CODE
<?php
include("connect.php");
$response=array();
if(isset($_GET['id'])){
$id=mysqli_real_escape_string($_GET['id']);
$q="select * from student_details where id='$id'";
}
else{
$q="select * from student_details";
}
$result_students=mysqli_query($con,$q);
while($row_students=mysqli_fetch_assoc($result_students)){
if(isset($_GET['id'])){
$response=$row_students;
}
else{
$id=$row_students['id'];
$name=$row_students['name'];
$des=$row_students['des'];
$image=$row_students['image'];
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $des; ?></td>
<td><img width="60" height="60" src="<?php echo $image; ?>"></td>
<td style="width:20%;">
<a href="javascript:void(0)" onclick="deleteStudent(this.id)" id="<?php echo $id; ?>">
<span style="padding-right:10%;" class="glyphicon glyphicon-trash"></span>
</a>
<a href="javascript:void(0)" onclick="updateStudentShow(this.id)" id="<?php echo $id; ?>">
<span style="padding-right:10%;" class="glyphicon glyphicon-pencil"></span>
</a>
<span class="glyphicon glyphicon-download-alt"></span>
</td>
</tr>
<?php } } ?>
<!--Loop Closed-->
<?php
if(isset($_GET['id'])){
$response=json_encode($response);
echo $response;
}
if(mysqli_num_rows($result_students)==0){
?>
<tr>
<td colspan="5">No Records</td>
</tr>
<?php } ?>
答案 0 :(得分:0)
You can't use json_encode()
on a table row. Notice up near the top of your html/php, you're doing $row_students=mysqli_fetch_assoc($result_students)
followed by $response=$row_students;
and then at the bottom you're doing $response=json_encode($response);
. See the documentation for json_encode()
, which says that the argument value
that you pass to json_endcode()
can be any type except a resource.
It then links you to the appendix, where you can find out what counts as a resource. The response from mysqli_fetch_assoc()
is indeed a resource. It can't be passed to json_encode()
.
答案 1 :(得分:0)
You are outputting the data as html table before echoing your json response.
Your jquery is expecting json only.
To test the output of your script, simply visit the php script in a browser and add the id parameter
http://host/.../view.php?id=1
Overall I think you need to split your code into two files.
lets call the html output file view.php and the data output file data.php
view.php will contain the javascript which will use data.php in the ajax call to get its json data.
data.php will contain the code to retrieve records from the database and output them as json.