Forum Replies Created

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Will S

    (@stedmanw)

    Hi (@bcworkz),

    Thanks for your input! I tried following your suggestions:

    1. I have 2 methods for trying to echo out value from db. In the 2nd method i tried echoing a <h1> with id tag that i reference in the js file.
    2. I changed the js file so that upon “success” the code should get the php value echoed out and then set equal to div on html page.

    Please let me know if i’m getting close and what changes to make. I have to admit the syntax is a little challenging. I coded my whole site in vanilla js and so am used to that.

    Thanks!

    Will

    function save_flashcard_settings2() {
    	global $wpdb;
    
    	//$adaptValueFromDb = intval($_GET['adapt']); 
    
    	$query = "SELECT * FROM MLT_Exam_db";
     
    	$result = $wpdb->query($query);
    
            // METHOD 1
            echo $adaptValueFromDb;
    
            // METHOD 2
            echo "<h1 id='phpValue'>$adaptValueFromDb </h1>"; 
    }
    
    
    
    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:  
    
    					
    					
    					},
    				
    					error: function(xhr, textStatus, error) {
    						window.location.reload();
    					},
    					success: function(data) {
    		
    getPhpValue: document.querySelector('#phpValue').innerHTML;	
    
    getDivValue: document.querySelector('#retrieveAdapt').innerHTML;			
    
    getDivValue: getPhpValue;
    				
    					}
    				});
    			
    				e.preventDefault();	
    			} else {
    
    window.location.reload();
    
    			}
    		});
    	}
    });
    Thread Starter Will S

    (@stedmanw)

    I will post this reply/new code in new post in case better visibility for you to see compared to this old thread.

    Thread Starter Will S

    (@stedmanw)

    Hi Gerry,

    This was my unsuccessful attempt for a GET call to retrieve data from database and load onto wordpress webpage. If you can give me any hints, would be much appreciated!! I completed a course on php and mysql on Coursera but just couldn’t find anything specific to using a wordpress db with hooks, etc.

    -Will

    — WordPress HTML widget  with GET code—
    
    <body>
    <button id="getFromDbButton" onclick="getFromDbButtonFxn()" >Get from Database</button>
    
    <div>'adapt'= 1000 >>><span id="retrieveAdapt"> 0</span></div>
    
    
    </body>
    — functions.php  with GET code—
    
    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 — with GET code
    
    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();
    			}
    		});
    	}
    });
    Thread Starter Will S

    (@stedmanw)

    Hi Gerry,

    I was able to get everything to work using your suggestions. Thank you so much for your help!!!! I really appreciate it.

    The next thing I need to do is make an ajax GET request to be able to get data from db to my wordpress webpage. I will try to use the POST method as a template and see if I can make it work. Would you mind taking a look at what I put together for my functions.php and js files (assuming I can’t get it to work)?

    Thanks again Gerry! I’m relatively new to the forum but is there anything i can do for you like a review or something? Let me know. Really appreciate your help!

    -Will

    Thread Starter Will S

    (@stedmanw)

    I was able to flush the CDN cache and that worked!!

    Thanks so much George!! Really appreciate it!

    -Will

Viewing 5 replies - 1 through 5 (of 5 total)