javascript - Reading variables from PHP and performing a select query using js -
i have small project, , i'm trying variables js file using ajax.
this js file:
function get_sites() { $.ajax({ method: "post", url: "server/newemptyphp1.php", datatype: "json", data: {type: "tasks"}, success: function (data) { } }); }
and php file:
if (isset($_post["type"])) { $type = $type = $_post["type"]; $returned_value = ""; //default value switch ($type) { case "tasks": $returned_value = display_tasks($conn); break; } $conn->close(); echo $returned_value; } function display_tasks($conn) { $query = "select * `site`;"; $result = mysqli_query($conn, $query); $html = ""; $final_result = array(); if ($result) { $row_count = mysqli_num_rows($result); ($i = 0; $i < $row_count; ++$i) { $row = mysqli_fetch_array($result); $task = array("id" => $row["id"], "av" => $row["availability"], "lng" => $row["lng"], "lat" => $row["lat"]); $final_result["site"][] = $task; } } print_r(json_encode($final_result)); return json_encode($final_result); }
the output:
{"site":[{"id":"1","av":"1","lng":"16.963777","lat":"42.548664"},{"id":"2","av":"0","lng":"16.96376","lat":"42.548685"}]}
as can see, array php file, can't handle well, how can variable in js file previous array?
you can try json decode. or getting data in response have below:
... success: function (data) { jquery.each(data, function () { console.log(this); jquery.each(this, function () { console.log("av=" + this.av + " --> id=" + this.id + " --> lat=" + this.lat + " --> lng=" + this.lng); }); }); } ...
Comments
Post a Comment