WP Hook + JQuery GET from WP DB
-
Hello,
I was able to get the issue (POST request) from my previous forum question working with much appreciated help from Gerry @metamezzo in order to get data from wordpress page posted into a database table. Thank you so much Gerry for your help!! Now, I’m trying to get the GET request working in order to get data from database table into my wordpress page.
If someone can take a look and see where I’m going wrong, it would be very appreciated! As I mentioned in previous post, I’ve spent so much time trying to figure this out myself but almost everything that I find is just posting using a form. There seems to be very little on how to use WordPress hooks beyond the documentation, which I’ve tried in vain to figure out.
Below is the relevant code for what I’m trying to do: get the value ‘1000’ from ‘adapt’ column of database table (MLT_Exam_db) and retrieve it and display it on wordpress page <div> where id = #retrieveAdapt.
- Portion of?html widget?inserted into Elementor page on WP site with field in which to display data from db.
- functions.php?code using hooks to GET data from db
- javascript script?to create object to GET data from db
Thanks!
Will S.
// ——— HTML Widget ——— //left out top part of html document <body> <button id="getFromDbButton" onclick="getFromDbButtonFxn()" >Get from Database</button> <div>Value retrieved from db: <span id="retrieveAdapt"></span></div> </body>
// ——— functions.php ——— //left out the code for child hello theme function register_flashcard_scripts2() { wp_register_script('flashcard_settings2', get_stylesheet_directory_uri() . '/scripts/getFromDB.js', array('jquery')); wp_localize_script('flashcard_settings2', 'FlashcardApp2', array( 'ajaxurl' => admin_url('admin-ajax.php'), 'user_id' => get_current_user_id() )); wp_enqueue_script('flashcard_settings2'); } add_action('wp_enqueue_scripts', 'register_flashcard_scripts2'); function save_flashcard_settings2() { global $wpdb; $adaptValueFromDb = intval($_GET['adapt']); // TRYING TO GET 'adapt' VALUE FROM DB $query = "SELECT adapt FROM MLT_Exam_db WHERE adapt=' .$adaptValueFromDb. '"; $result = $wpdb->query($query); } add_action('wp_ajax_save_flashcard_settings2', 'save_flashcard_settings2'); add_action('wp_ajax_nopriv_save_flashcard_settings2', 'save_flashcard_settings2');
// ——— getFromDB.js ——— jQuery(document).ready(function ($) { var $flashcard_exit_btn2 = $('#getFromDbButtonFxn'); if ($flashcard_exit_btn2.length !== 0) { $flashcard_exit_btn2.on('click', function (e) { if(confirm('Warning:')) { $.ajax({ type: 'GET', dataType: 'json', url: FlashcardApp2.ajaxurl, aync: false, data: { action: 'save_flashcard_settings2', // RUN PHP SCRIPT: adaptValueFromDb: document.querySelector('#retrieveAdapt').innerHTML, adaptValueFromDb: $adaptValueFromDb }, error: function(xhr, textStatus, error) { window.location.reload(); }, success: function(data) { window.location.reload(); } }); e.preventDefault(); } else { window.location.reload(); } }); } });
- The topic ‘WP Hook + JQuery GET from WP DB’ is closed to new replies.