skip to Main Content

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


  1. 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

    https://developer.chrome.com/docs/devtools/network/#load


    Login or Signup to reply.
  2. As per my understanding of your question I have reproduced your issue and made some changes to the code. Here it is:

    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>
    

    script:

     (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);
    

    Then I added the blog_activity_notes table to my database. Here blog is my prefix.
    enter image description here

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search