I have a ajax script that returns me an associative array list as follows:
array(24) {
["id"]=>
string(4) "3478"
["joindate"]=>
string(19) "2016-10-17 21:20:28"
["ip"]=>
string(12) "xxx.xxx.xx.xx"
...
}
array(24) {
["id"]=>
string(4) "3479"
["joindate"]=>
string(19) "2016-10-17 21:20:28"
["ip"]=>
string(12) "xxx.xxx.xx.xx"
...
}
<thead>
<tr>
<th><b><u>key 1</u></b></th>
<th><b><u>key 2</u></b></th>
<th><b><u>key 3</u></b></th>
<th><b><u>key 4</u></b></th>
<th><b>etc...</b></th>
</tr>
</thead>
<tbody>
<tr>
<td>value 1</td>
<td>value 2</td>
<td>value 3</td>
<td>value 4</td>
<td>etc...</td>
</tr>
<tr>
<td>value 1</td>
<td>value 2</td>
<td>value 3</td>
<td>value 4</td>
<td>etc...</td>
</tr>
</tbody>
data += "data=" +formation+' '+methode;
$.ajax({
url: "core/display_lead.php",
type: "post",
data: data, dataType: "html",
success : function(code_html, statut){
console.log(code_html);
},
error : function(resultat, statut, erreur){
console.log("La requête n'a pas aboutie...");
console.log(resultat);
console.log(statut);
console.log(erreur);
}
});
So first change your PHP code to send back a JSON respose instead of a var_dump
<?php
$all = array();
while ($row = mysql_fetch_array($req, MYSQLI_ASSOC)){
$all[] = $row;
}
echo json_encode($all);
Now in your javascript, tell the .ajax functionality to expect JSON as a response
data += "data=" +formation+' '+methode;
$.ajax({
url: "core/display_lead.php",
type: "post",
data: data,
dataType: "json", //<-- amended
success : function(data, statut){
console.log(data);
},
error : function(resultat, statut, erreur){
console.log("La requête n'a pas aboutie...");
console.log(resultat);
console.log(statut);
console.log(erreur);
}
});
Now using this and @krasipenkov answer and a little look at the data using the javascript debugger you should be almost all the way there. The data
parameter on your success
method should now be a javascript native data type i.e an array or an object depending on the data you built in PHP and easy enough to process
Afraid I really have to add this as well now I see your using the
mysql_
extension. Every time you use themysql_
database extension in new code a Kitten is strangled somewhere in the world it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning thePDO
ormysqli
database extensions. Start here