I am struggling with this error for a while now.
The plugin inserts data to the table.
Here's the code :
File : plugin/_add.php
require_once('../../../wp-load.php');
global $wpdb;
$table_name = $wpdb->prefix . 'westpac_store';
$result = $wpdb->insert(
$table_name,
array(
'itemModel' => $_POST['itemModel'],
'itemCat' => $_POST['itemCat'],
'itemManu' => $_POST['itemManu'],
'itemImg' => $_POST['itemSpec'],
'itemSpec' => $_POST['itemDesc'],
'itemDesc' => $_POST['itemImg'],
'itemPrice' => $_POST['itemPrice'],
'itemPriceDiscount' => $_POST['itemDiscount'],
)
);
return ( $result == true ? 'success' : 'error' );
File : plugin/_common.js
jQuery(document).ready(function() {
//Add Item Submit
jQuery(document).on('submit','#westpac-store-add',function(event){
event.preventDefault();
var $form = jQuery(this);
// let's select and cache all the fields
var $inputs = $form.find("input, select, button, textarea");
// serialize the data in the form
var itemData = $form.serialize();
$inputs.prop("disabled", true);
jQuery.ajax({
cache: false,
type: 'POST',
url: store.directory + '_add.php',
data: itemData,
success: function(data) {
console.log(data);
},
error: function(data) {
alert(data.error + 'error');
}
});
$inputs.prop("disabled", false);
});
});
Facts:
1. the _add.php
is not the main plugin file.
2. its an admin interface.
3. the plugin works, the data saves in the database unless there is a data type mismatch from the input and in the database.
Problem is I can't get the actual return
value from _add.php
, the console.log(data) always return ""
even though the return
in _add.php
is fixed with string returns.
It should be at least any of the string 'success' or 'error'.
Please help!