I am learning how to use AJAX in the admin for a new plugin I am writing. In the PHP I have the function:
function ppmhnh_add_location(){
global $wpdb;
$values = array();
parse_str($_POST['form'], $values);
$sql = "INSERT INTO ppm_hoopsandhits_locations VALUES ('{$values['locale']}', '{$values['locale_city']}', '{$values['locale_state']}')";
$wpdb->query($sql);
echo "Added successfully!";
die();
}
add_action('wp_ajax_ppmhnh_new_location','ppmhnh_add_location');
The javascript uses this:
$('#newLocation').submit(function(e){
$('#wait-gif').show();
$('#btnAddLocation').attr('disabled', true);
data = {
action: 'ppmhnh_new_location',
form: $(this).serialize()
};
$.post( ajaxurl, data, function(response){
alert(response);
$('#wait-gif').hide();
$('#btnAddLocation').attr('disabled', false);
location.reload();
});
return false;
});
If I echo $sql in response, I get the correct SQL statement
INSERT INTO ppm_hoopsandhits_locations VALUES ('100 Midtown', 'Atlanta', 'GA')
However nothing is being added to the table.
Also, I would prefer just to refresh the part of the admin page where the table is displaying all of the values of the table rather than the whole page but I haven't figured that out yet.
Any help is appreciated!