0

This is a jQuery script I'm working on to post inputs to a database, assuming the user submitting the data is logged in, and hasn't made an existing entry for the item the user is submitting data for.

Currently it's not producing any results, and I'm not entirely sure why since I don't work with jQuery that often.

Here is a flow chart illustrating what I'm trying to accomplish:

enter image description here

function csv_three_script() {
    //Include Javascript library
    wp_enqueue_script(
        'csv3', 
        plugins_url( 
            '/js/demo.js', 
            __FILE__ 
        ), 
        array( 
            'jquery'
        )
     );
     wp_localize_script(
     'csv3',
     'MyAjax', array(
     'ajaxurl' => admin_url( 'admin-ajax.php')));
}
add_action( 'wp_enqueue_scripts', 'csv_three_script' );



function check_db(){
global $table;
global $quanid;
$hf_userid = get_current_user_id();
global $wpdb;
$wpdb->get_results( $wpdb->prepare("SELECT count( 1 ) FROM $table WHERE ItemID = '$quanid' AND user = '$hf_userid'", ARRAY_A));
}
add_action('wp_ajax_check_db', 'check_db');

function update_entry(){
global $quanid;
$price = isset($_POST[$quanid]);
$hf_userid = get_current_user_id();
global $table;
global $wpdb;
    $wpdb->update( $wpdb->prepare( '$table',
    array(
        'ItemID' => '$quanid',
        'Price' => $price,
        'user' => $hf_userid)));

}
add_action('wp_ajax_update_entry', 'update_entry');


function post_entry(){
global $quanid;
$price = isset($_POST[$quanid]);
$hf_userid = get_current_user_id();
global $wpdb;
$wpdb->insert( $wpdb->prepare( 
    '$table', 
    array( 
        'ItemID' => '$quanid',
        'Price'  => $price,
        'user'   => $hf_userid
    ), 
    array( 
        '%d', '%d', '%d'
    ) 
));
die();
return true;
}
add_action('wp_ajax_post_entry', 'post_entry');

jQuery(document).ready(function() {
    var data = {
        action: 'is_user_logged_in'
    };
    jQuery.post(ajaxurl, data, function(response) {
            if (response == 'yes') {
                // user is logged in
                jQuery.ajax({
                        type: 'POST',
                        url: MyAjax.ajaxurl,
                        data: {
                            "action": "check_db",
                            "user": user,
                            "ItemID": ItemID
                        },
                        function(data) {
                            if (data.exists) {
                                //existing entry found, update entry
                                function UpdateRecord(id) {
                                    jQuery.ajax({
                                        type: 'POST',
                                        url: MyAjax
                                            .ajaxurl,
                                        data: {
                                            "action": "update_entry",
                                            "ItemID": ItemID,
                                            "Price": Price,
                                            "user": user
                                        },
                                        success: function(
                                            response
                                        ) {
                                            alert
                                                (
                                                    "Price successfully submitted."
                                                );
                                        }
                                    });
                                }
                            } else {
                                //existing entry not found, create new entry
                                jQuery("#submit").click(
                                    function() {
                                        var name = jQuery(
                                            "#ItemID").val();
                                        jQuery.ajax({
                                            type: 'POST',
                                            url: MyAjax
                                                .ajaxurl,
                                            data: {
                                                "action": "post_entry",
                                                "ItemID": ItemID,
                                                "Price": Price,
                                                "user": user
                                            },
                                            success: function(
                                                response
                                            ) {
                                                alert
                                                    (
                                                        "Price successfully submitted."
                                                    );
                                            }
                                        });
                                    });
                            });
                    }
                }, 'JSON');
        } else {
            // user is not logged in
            alert("You must be logged in to submit prices.");
        }
    }
});
5
3

You use globals :( whenever you think that you need to use a global you should automatically stop coding and invest as much time as it takes to remove that need.

In your case it seems like you assume that global variables persist between Ajax request, but they don't. In addition your design of the code is broken as in client server configuration you can not assume that the server state remain the same between request, therefor when you need to do some operation you need to do it in one request and not to split it into 3. It is good design to let the user know that he can not perform an operation, but you need to actually recheck all conditions when getting to actually performing the operation even if you checked them when you displayed the UI to the user.

7
  • As far as I knew, this was one request as opposed to three. Then again as I stated in my question, I don't work with jQuery that often so I'm not privy to what you're talking about in terms of understanding how to modify my code, I only understand it in a very vague theoretical way.
    – Kreation
    Mar 26 '15 at 4:57
  • I do no not intend to troll you, but if you don't understand my answer then you need to step back and invest several hours in understanding how ajax works in theory on both client and server side, even before addressing jQuery or wordpress specific issues. Mar 26 '15 at 5:36
  • The person who flagged this as low quality, please explain why. Mar 26 '15 at 8:56
  • This answer amounts to RTFM, which I could have done on my own. It vaguely explains what I need to do, rather than addressing my specific question. There are no links to any helpful tutorial sites, no editing of my code, etc; void of what is standard boiler plate on any SE community.
    – Kreation
    Mar 26 '15 at 10:02
  • @Kreation Please note that more general are considered fine here. This answer does point out very specific issue you are having (using globals and general request design). You are not owed for someone to rewrite and fix your code. If people only have time for more general answer, they still spend that time to try help you, please appreciate that.
    – Rarst
    Mar 26 '15 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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