简体   繁体   中英

Unable to properly handle the data sent by AJAX request

I have a custom form on Wordpress (please don't suggest Contact Form 7) that allows users to donate to a charity. It validates the data using the jQuery Validate plugin. Once the data is deemed valid, it pushes the data to a 3rd party payment processor. This works great but I'd like to store the data on the form into the Wordpress DB (contact info and donation value).

I wasn't sure how to do so best & while PHP in the header of the template file would be easiest, since the submit action causes a redirect, I figured using AJAX was the best option. I followed this tutorial and now send JSON data to the AJAX "handler" file. My jQuery code (after validations etc) is below:

console.log("Donation Type: " + donationType + " Once-off");
string = JSON.stringify(submission);
console.log(string);

jQuery.ajax({
    url : "http://192.168.8.50/subsite/wp-admin/admin-ajax.php",
    type : 'post',
    data : {
        action : 'store_donation_to_db',
        post_id : post_id,
        fields : string
    },
    success : function( response ) {
        console.log( response );
    }
});

//ProcessDonation(submission);

Interpreting the AJAX data is the issue I'm facing. I've used the following:

add_action( 'wp_ajax_nopriv_store_donation_to_db', 'store_donation_to_db' );
add_action( 'wp_ajax_store_donation_to_db', 'store_donation_to_db' );

function store_donation_to_db() {
    //register log file path
    $path = plugin_dir_path( __FILE__). 'test-button-log.txt';
    //$handle = fopen($path,"w");


    // The message written to the file
    $new_line = "****************************************************************************\r\n";
    $log_entry = $_REQUEST;
    // Write the contents to the file, 
    // using the FILE_APPEND flag to append the content to the end of the file
    // and the LOCK_EX flag to prevent anyone else writing to the file at the same time
    //
    file_put_contents($path, $log_entry, FILE_APPEND | LOCK_EX);

    $variables = json_decode($_REQUEST['store_donation_to_db']);
    //file_put_contents($path, $variables, FILE_APPEND | LOCK_EX);
    $counter= 0;
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { 
        $string = "Hello";
        foreach($variables as $field)
        {
            $string .= $field." ".$counter;
            $counter++;
        }
        echo $string;
        die();
    }
    else {
        wp_redirect( get_permalink( $_REQUEST['post_id'] ) );
        exit();
    }
}

I got frustrated that the AJAX call would only return "Hello" so I added the line to write the $_REQUEST data into a text file, that data is below and looks like a JSON array:

store_donation_to_db{\"Title\":\"Mrs\",\"FirstName\":\"Daniel\",\"Surname\":\"Holland\",\"EmailAddress\":\"daniel@dfdsfdsf.com\",\"ContactNumber\":\"\",\"DateofBirth\":\"2017-07-10\",\"undefined\":\"0\",\"recurringCHK\":\"1\",\"DonationAmount\":\"1000\"}

Why am I unable to access the $variables array? Shouldn't the $_REQUEST variable be an array containing both post_id and fields as indexes with their corresponding values? I feel like I'm on the verge of getting this right and there's probably a stupid line I've got wrong (given how accurate the text file log is).

You are using AJAX with POST method, you can access the POST data using POST global variable in php.

$_POST['post_id] and $_POST['fields'] and then decode the json string and store it in DB.

Did you try this ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM