jQuery AJAX with wordpress database connection

admin

Administrator
Staff member
I am trying to make an jQuery AJAX call on a button click.
At first is started creating my own AJAX code, everything worked exept for IOS.

So now i started using jQuery. Works like a charm with sending data back and forth,
Exept.. everytime I try making a connection to a database on my php file it gives me error 500 (Internal Server Error).
I tried it wordpress way ($wpdb) and mysqli, both not working.

Javascript:

Code:
jQuery(document).ready(function() {

var buttons = document.getElementsByClassName('vilbutton');


for(var i=0; i< buttons.length; i++){
    jQuery(buttons[i]).one('click',send);

}

function send(){

var id = this.id;
var count = 1;

jQuery.ajax({
    url: 'mydomain/krant.php',
    type: 'post',
    dataType: 'text',
    data: { id : id, count : count},
    crossDomain: true,
    success: function(data) {
        jQuery("#"+ id).children('.vilbsmall').children('.vilbsmallc').html("<span     class='vilbsmallct' style='height: 30px; width: 30px;'>" + data + "</span>");
    },
    error: function(xhr, desc, err) {
        alert("Foutcode: 1912");
        console.log(xhr);
        console.log("Details: " + desc + "\nError:" + err);
    }
}); // end ajax call
}
});

PHP:


Code:
header('Access-Control-Allow-Origin: *');


$id = $_POST['id'];
/* $con = mysqli_connect('mycredentials');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$sql = "SELECT id FROM buttons WHERE id = $id";

$test = mysql_fetch_assoc(mysqli_query($con,$sql));

$test = $test +1; */

global $wpdb;

$button_count = $wpdb->get_var("SELECT count FROM buttons WHERE id = $id");

$count = $_POST['count'];
$ip = $_SERVER['REMOTE_ADDR'];

echo $button_count;

I am stuck on this for 2 days.. help would be very appreciated!