I have a HTML page with javascript function:
<script type="text/javascript">
/* <![CDATA[ */
function voteCounter(messageID)
{
$.get("./_vote/counter_ajax.php?messageID="+messageID, function(data){
alert(data);
});
}
/* ]]> */
</script>
My PHP file 'counter_ajax.php':
<?php
require "../lib/config.php";
$STB = $config['dbTableNames']['votes'];
$DBServer = $config['dbConnection']['host'];
$DBUser = $config['dbConnection']['user'];
$DBPass = $config['dbConnection']['pass'];
$DBName = $config['dbConnection']['name'];
$messageID = $_GET['messageID'];
$db = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
$resultUp = $db->query("SELECT SUM(voteUp) AS voteUp FROM $STB WHERE messageID = '$messageID'")->fetch_object()->voteUp;
$resultDown = $db->query("SELECT SUM(voteDown) AS voteDown FROM $STB WHERE messageID = '$messageID'")->fetch_object()->voteDown;
echo json_encode("Positive: " . $resultUp . " .... Negative: " . $resultDown);
?>
If I run PHP file in browser is working, the result is filled with text and variables:
Positive: 3 .. Negative: 2
But the response in HTML in alert box is this, without variables:
Positive: .. Negative:
What is the right approach?
alert(messageID);and check if it is correct.alert(JSON.parse(data)). The same result, without variables.console.log(data);To see all your returned json.console.log(data);I receive in Firebug in Firefox the result with variables: Positive: 3 .. Negative: 2