1

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!

7
  • Generally just use the WordPress way of doing AJAX as described at the codex article AJAX in Plugins. Commented Oct 7, 2014 at 14:01
  • that doesnt solve the problem and evaded it
    – Roi
    Commented Oct 7, 2014 at 14:03
  • Never claimed that it will solve you problem. Just telling you that your life will be easier if you do it that way. Commented Oct 7, 2014 at 14:08
  • actually I used that documentation to produce the external ajaxing, everything works just the return thing. although I manage to figure it out.
    – Roi
    Commented Oct 7, 2014 at 14:10
  • Good for you. It still is better to do it different, but do as you like. I just wanted to give you that hint. Commented Oct 7, 2014 at 14:13

1 Answer 1

2

I solved it

instead of using

return ( $result == true ? 'success' : 'error' );

use

echo ( $result == true ? 'success' : 'error' );

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.