1

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
  • Could you explain what what it is you want. Showing WP code does not help . So if the ajax works then the PHP script that gets called is the problem. I see nothing in your code regarding "inserting data into a database". Got an sql query? Where are the parameters required to do the sql are being passed to the ajax function? Commented Sep 21, 2022 at 6:35
  • @Misunderstood in wordpress, it uses the $wpdb object and used the insert statement as you can see above in the code Commented Sep 21, 2022 at 8:38

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

so this worked but it means I have to replace ` $wpdb->insert( $wpdb->prefix . 'activity_notes',` with ` $wpdb->insert('wp_activity_notes',,`. Why is this?
In the wp-config.php file, you can define a database table prefix. By default, the prefix is "wp_", but you'll need to check on the actual value and use it to define your database table name. This value is found in the $wpdb->prefix variable. I think your database table prefix is different so you have to put just the table name(wp_activity_notes).
yes taking out the $preix and just adding it as 'wp_table_name' worked.
0

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


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.