KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have a code to display a random quote. One person wrote a function to implement all of this. But the update data via AJAX for some reason does not work. When you press the button "New Quote", nothing happens. Maybe someone knows why? What needs to be fixed in the following code, so that when you click "New quote" a new quote is loaded? The PHP /wp-content/themes/%your_theme%/js/ajax-load-quote.php <?php /* uncomment the below, if you want to use native WP functions in this file */ // require_once('../../../../wp-load.php'); $array = file( $_POST['file_path'] ); // file path in $_POST, as from the js $r = rand( 0, count($array) - 1 ); return '<p>' . $array[$r] . '</p>'; ?> The HTML Structure In a page content, a widget or a template file: <div id="randomquotes"> <p>I would rather have my ignorance than another man’s knowledge, because I have so much more of it.<br /> -- Mark Twain, American author & Playwright</p> </div> <a id="newquote" class="button" href="#" title="Gimme a new one!">New Quote</a> This you can obviously adjust to your liking, but for the sake of this example, this is what we're going with. We will generate the above via a shortcode later. The jQuery /wp-content/themes/%your_theme%/js/ajax-load-quote.js function ajaxQuote() { var theQuote = jQuery.ajax({ type: 'POST', url: ajaxParams.themeURI+'js/ajax-load-quote.php', /* supplying the file path to the ajax loaded php as a $_POST variable */ data: { file_path: ajaxParams.filePath }, beforeSend: function() { ajaxLoadingScreen(true,'#randomquotes'); }, success: function(data) { jQuery('#randomquotes').find('p').remove(); jQuery('#randomquotes').prepend(dat
Tags (comma-separated)
Save Edits
Cancel