Alright, I've been pretty frustrated with accessing $wpdb in a new PHP file in WordPress. Now, I've gone through and read all the answers I could find on the situation, and they're all universal in saying that all I need to do is add wp-load.php at the top of my php file which uses $wpdb and it should work. I've done that... However, it still isn't working. To make sure I'm not doing anything stupid, I've included both files in their entirety. Here's my code:
main-admin.php:
<?php
function my_plugin_menu(){
add_menu_page('my System', 'my System','administrator', 'my-plugin.php', 'my_plugin_action', plugins_url('my.png', __FILE__));
}
function my_plugin_action(){
?>
<form id="createItemForm" method="post">
<br/><br/>Create New Item
<table><tr>
<td>Enter Item Name:</td><td><input type="text" name="itemName" id="itemName"></td></tr><br><tr>
<td>Enter Item Value: </td><td><input type="text" name="itemValue" id="itemValue"></td></tr><br><tr>
<td><input type="submit" class="button button-primary" id="specialSubmit2" value="Create Item"></td></tr></table>
</form>
<script>
jQuery(function() {
jQuery('#createItemForm').submit(function(e){
e.preventDefault();
var itemName = jQuery( "input[id='itemName']" ).val();
var itemValue = jQuery("input[id='itemValue']").val();
alert(itemName + " " + itemValue);//Test that it works, it does
jQuery.ajax({
type: "POST",
url: "../path/to/submit.php",
data:{ name: itemName, value: itemValue },
success: function(data){
alert(data);
},
error: function (req, textStatus, thrownError) {
console.log(req.responseText);
}
});
});
});
</script>
<?php
}
?>
submit.php (within same folder):
<?php
require_once( 'path/to/wp-load.php' );
if(isset($_POST["name"]) && isset($_POST["value"])){
global $wpdb;
$varCheckCon = $wpdb->check_connection();
echo json_encode($varCheckCon);
}
?>
However, all it does is return me a 500 error, not even a "false" for not connected. It should be noted that the main-admin.php file itself is not the core WordPress plugin PHP file (that main file can access $wpdb without any problems). Rather, both main-admin.php and submit.php are new PHP files that I've made within a /admin subfolder within my WordPress plugin. The main-admin.php form appears as part of my dashboard menu in Wordpress; it is called from the main file.
I'm still something of a Wordpress beginner, so maybe it's something incredibly stupid that I'm missing?
Thanks for any help!