I’m writing a WordPress plugin and want to use ajax to submit data. When using ajax to submit a form in the admin panel, I get this error:
Fatal error: Call to a member function insert() on a non-object in
/home1/crave/public_html/wp-content/plugins/MiniCMS/add_contenttype.php
on line 13
Here’s the script being called. Error line is annotated.
<?php
global $wpdb;
$name = $_POST["name"];
$id = '1';
$text_inputs = $_POST["text_inputs"];
$paragraph_inputs = $_POST["paragraph_inputs"];
$map_inputs = $_POST["map_inputs"];
$file_inputs = $_POST["file_inputs"];
$contentTypeTable = $wpdb->prefix . "minicms_content_type";
//This is line 13, the problem child:
$wpdb->insert( $contentTypeTable, array(
'name' => $name,
'id' => $id,
'text_inputs' => $text_inputs,
'paragraph_inputs' => $paragraph_inputs,
'map_inputs' => $map_inputs,
'file_inputs' => $file_inputs
));
?>
Does anyone know why I’m not getting $wpdb to work?
You need to use ajax in WordPress way.
From WordPress Doc:
First add some javascript that will trigger the AJAX request:
Then, set up a PHP function that will handle that request:
Reference:
1. http://codex.wordpress.org/AJAX_in_Plugins
2. http://wp.tutsplus.com/articles/getting-started-with-ajax-wordpress-pagination/