I am using the WordPrewss $wpdb
class to insert data into the database. The ajax part works fine but it's not addin the data into the database.
<script>
$("#notes_maker").submit(function(){
event.preventDefault();
var formURL = "<?php echo admin_url('admin-ajax.php')?>";
var notes_editor = $("#notes_area").val();
//var note_timestamp = $(".note_timestamp").text();
$.ajax({
url: formURL,
data: {
'type': 'POST',
'action': 'notes',
'notes_editor1': notes_editor,
'dataType': 'text',
//'note_timestamp': note_timestamp,
},
success: function(data) {
alert("it works");
}
})
});
</script>
<form id="notes_maker" >
<div >1.40</div>
<div data-section="notes" id="notes">
<textarea name="notes_area1" id="notes_area">
this is some text
</textarea>
<input type="submit" name="submit" value="Save Note"/>
<input type="button" name="cancel_note" value="Cancel"/>
</div>
</form>
functions.php file
function my_ajax_notes() {
if(isset($_REQUEST)) {
$car = $_REQUEST['notes_editor1'];
echo $car;
global $wpdb;
$wpdb->insert(
$wpdb->prefix.`activity_notes`,
[
'text' => $car
]
);
}
}
add_action('wp_ajax_notes', 'my_ajax_notes');
//add_action('wp_ajax_nopriv_notes', 'my_ajax_notes');
CodePudding user response:
You need to change ajax function
$.ajax({
url: formURL,
'type': 'POST', // put this out here
'dataType': 'text', // put this out here
data: {
'action': 'notes',
'notes_editor1': notes_editor,
},
success: function(data) {
alert("it works");
}
});
and also make sure if the endpoint allows the dataType you are sending
and may want to read about network inspection where you can see what is going on with your requests
functions.php
function my_ajax_notes() {
if ( isset( $_REQUEST ) ) {
$car = $_REQUEST['notes_editor1'];
echo $car;
global $wpdb;
$wpdb->insert(
$wpdb->prefix . 'activity_notes',
array(
'text' => $car,
)
);
}
}
add_action( 'wp_ajax_notes', 'my_ajax_notes' );
Please check once your table name whether the prefix is added or not. If not then you have to add a prefix to your table name. It is working fine in my local and data has been inserted.