0

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:

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:

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!

2
  • I see you have a custom file called krant.php and you're trying to use the $wpdb object. Is this part of a theme? template? plugin? or did you just make a php file and assume you could use the global $wpdb object? Commented Sep 15, 2014 at 18:45
  • this is part of my theme, it gets even weirder, like i said i already had it working the including database connection on another file in the same directory. This file could succesfully connect using $wpdb Commented Sep 15, 2014 at 18:48

2 Answers 2

1

By including wp-load.php, you'll have access to objects like $wpdb.

require_once( '/path/to/your/files/wp-load.php' );

http://davidwalsh.name/wordpress-recent-posts

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! Works! I have no clue why it worked on the other file without the require
@user3708994 because if u access a wordpress file directly you cannot use any of the wordpress functions unless u load wp-load.php
0

The reason you are getting a 500 Internal error is because there is an error in your PHP script.
You are using $wpdb Object which is Undefined. To access $wpdb, include the wp-load.php.

require_once( 'path-to-file/wp-load.php' );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.