i try to call a php function with Ajax. This is my JavaScript code in my html file:
<script type="text/javascript">
function ajax(){
$.ajax({
type:"POST",
url: "SQLCommunication.php",
dataType: "JSON",
success : function(json){
json = jQuery.parseJSON(json);
alert(json.value);
}
}
)
}
$("#btn_refresh").click(function(){
ajax();
});
</script>
I don't know if i have to specify which PHP function i actually want to call? I also don't know how i do that.
My PHP function:
header('Content-Type: application/json');
function readValue(){
$conn = establishConnection();
if($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT datetime, value FROM tempvalues";
$result = $conn->query($sql);
if($result->num_rows > 0){
$row = $result->fetch_assoc();
$arr["datetime"] = $row["datetime"]; //return datetime and value as array
$arr["value"] = $row["value"];
if(is_ajax()){
return json_encode($arr);
} else {
return $arr;
}
}
$conn->close();
}
So the problem is now, that nothing happens if i press the button.
print_r(json_encode($arr))readValue()function in your PHP code andechoits result.parseJSONas I knowreadValuefunction in your controller.is_ajax()?