I am trying to add data from a form in phpmyadmin which works. The issue I have is that when I insert my new entry and submit the form, it adds my previous entry to the database; when I click the submit button again, it would add my new entry. It should enter the current entry the user enters rather than having to click the submit button twice to see my new entry. See my code below.
HTML
<form id="notes_maker" class="notes_section" type="POST">
<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" id="submit" />
<input type="button" name="cancel_note" value="Cancel" />
</div>
javascript
(function($) {
$(document).ready(function() {
$("#notes_maker").submit(function(){
event.preventDefault();
var formURL = "<?php echo admin_url('admin-ajax.php')?>";
var notes_editor = $("#notes_area").val();
$.ajax({
url: formURL,
type: 'POST',
dataType: 'text',
data: {
'action': 'notes',
'notes_editor1': notes_editor,
},
success: function(data) {
alert("it works");
}
})
});
});
})(jQuery);
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' );