WP Hook + JQuery Post to WP DB not working
-
Hello,
I’ve spent literally 12 hours in the last 2 days trying to figure this out— I tried watching relevant you tube videos, searching StackOverflow posts, going over documentation for php/WP functions/WP hooks/JQuery and trying everything i can think of but unfortunately not able to figure out.
Ultimately, I’m trying to have the following sent from my webpage to my database, when pressing a button on page:
- Date/Timestamp
- Current value in body: <p id=”adapt”>30</p>
- Current array value in script:
MyMLT_Test_Array[1]
- Logged in UserID
#1 (Date/Timestamp) and #4 (Logged in UserID) I’ve been able to successfully send to my database.
#2 (Current value in body) and #3 (Current array value in script) I have NOT been able to send to database. I keep getting empty values of “0” in db. I tried changing type to string vs number and verifying with Alert (“typeof __”), declaring variable in JS script first, adjusting the %s and %d in functions.php and changing datatype in db from VARCHAR to INT. Nothing is working.
Below is the relevant code for what I’m trying to do:
- functions.php code using hooks to post data to db
- javascript script to create object to post to db
- Portion of html widget inserted into Elementor page on WP site with button, id of value to insert, array value to insert.
If anybody can take a look at this, it would be super appreciated! I really did spend so much time myself trying to figure this out before coming to you all. I feel like I’m so close—I thought it was a simple data type issue.
Thanks!
Will S.
// THIS IS MY functions.php file from my WP site function register_flashcard_scripts() { wp_register_script('flashcard_settings', get_stylesheet_directory_uri() . '/scripts/save_flashcard_2.js', array('jquery')); wp_localize_script('flashcard_settings', 'FlashcardApp', array( 'ajaxurl' => admin_url('admin-ajax.php'), 'user_id' => get_current_user_id() )); wp_enqueue_script('flashcard_settings'); } add_action('wp_enqueue_scripts', 'register_flashcard_scripts'); function save_flashcard_settings() { global $wpdb; $date = date("Y-m-d H:i:s"); // MySQL Date/Time format $flashcard_subject = isset($_POST['adapt']) ? $_POST['adapt'] : ''; $array_value = isset($_POST[MyMLT_Test_Array[1]]) ? $_POST[MyMLT_Test_Array[1]] : ''; $user_id = isset($_POST['user_id']) ? $_POST['user_id'] : ''; $sql = "INSERT INTO MLT_Exam_db (user_date, array_values, adapt, user_id)"; $sql .= "VALUES (%s, %d, %d, %d)"; $query = $wpdb->prepare($sql, $date, $array_value, $flashcard_subject, $user_id); $result = $wpdb->query($query); wp_send_json_success('Data was saved successfully'); } add_action('wp_ajax_save_flashcard_settings', 'save_flashcard_settings'); add_action('wp_ajax_nopriv_save_flashcard_settings', 'save_flashcard_settings');
// THIS IS MY JS SCRIPT jQuery(document).ready(function ($) { var $flashcard_exit_btn = $('#Load_Pkg_1_button'); var flashcard_subject = 0; if ($flashcard_exit_btn.length !== 0) { $flashcard_exit_btn.on('click', function (e) { if (confirm('Press "CANCEL" to resume Test. Press "OK" to exit and save Flashcard results to your PROGRESS page.') == true) { $.ajax({ type: 'POST', dataType: 'json', url: FlashcardApp.ajaxurl, aync: false, data: { action: 'save_flashcard_settings', flashcard_subject: document.getElementById('adapt').textContent, array_value: document.getElementById(MyMLT_Test_Array[1]).textContent, user_id: FlashcardApp.user_id }, error: function (xhr, textStatus, error) { scrollToTop(); }, success: function (data) { window.location.reload(); scrollToTop(); } }); alert(typeof flashcard_subject); e.preventDefault(); } else { } }); } });
// THIS IS A PORTION OF MY ELEMENTOR HTML WIDGET CODE <body> <p id="adapt">30</p> <button id="Load_Pkg_1_button" class="Flashcard_Setting_Buttons_All" onclick="LoadPkg_1_Exams_Button_Fxn()">Load Pkg 1 Exams</button> <script> let MyMLT_Test_Array = [ [0], [10], [20], [30], [40], ]; let array_values = MyMLT_Test_Array[1]; </script> </body>
- The topic ‘WP Hook + JQuery Post to WP DB not working’ is closed to new replies.