1

I get the complete result of the database inside the error statement. So there is data passing trough but I have no clue why its going to an error i'm doing wrong. Working for hours now and find no solution. Hope somebody can help me

This is my select_clienten.php

<?php
//database configuration
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '********';
$dbName = '********';

//connect with the database
$conn = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);

$sql = "SELECT * FROM clienten";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "<tr id=".$row["id"]."><td>".$row["roepnaam"]."</td>";
        echo "<td>".$row["achternaam"]."</td>";
        echo "<td>".$row["email"]."</td>";
        echo "<td><span class='glyphicon glyphicon-edit bewerk'></span></td>";
        echo "<td><span class='glyphicon glyphicon-trash verwijder'></span></td></tr>";
    }

} else {
    echo "0 results";
}
echo json_encode($data);
?>

This is my index.php.

<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>title</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery- ui.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" href="/css/custom.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

</head>
<body>

<script type="text/javascript">
$(document).ready(function() {
    var data;
    $.ajax(
       {type: 'get',
        url: './select_clienten.php',
        data: "",
        dataType: 'json',
        success:function(data)//we got the response of ajax :) 
        {
            $("#test").html(data);
        },
        error:function(data)
        {
            //no respons show error.
            alert  ("error!");
            alert(JSON.stringify(data)) //we give an alert of the error when there is no respons from ajax
        ;}
    }); //end of ajax function.

});
</script>
<div id="test">
<!-- show data json here -->
</div>
</body>
</html>

1 Answer 1

2

I think the problem is that you set dataType: 'json' in your ajax function but response is html like this:

echo "<tr id=".$row["id"]."><td>".$row["roepnaam"]."</td>"

You should save all your html in string

$data = "";
$data .= "<tr id=".$row["id"]."><td>".$row["roepnaam"]."</td>";
$data .= "<td>".$row["achternaam"]."</td>";
....

and send json response

echo json_encode(['data'=>$data]);

or remove dataType: 'json' and send html

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

1 Comment

Thank you very much you where right. I removed the datatype json and echo the data and it works.

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.