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" class="notes_section">
<div class="note_timestamp">1.40</div>
<div data-section="notes" class="js-tabs o-wrapper" 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');
2
Answers
You need to change ajax function
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
https://developer.chrome.com/docs/devtools/network/#load
As per my understanding of your question I have reproduced your issue and made some changes to the code. Here it is:
HTML:
script:
Then I added the blog_activity_notes table to my database. Here blog is my prefix.
functions.php
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.