I have a quote.php file with the following code:
<div id="quote-section">
<div id="quote">
A famous man called <span id="quote_author">Confucious</span>, once said <span id="quote_txt">man who make mistake on elevator, wrong on many levels</span>
</div>
<input type="button" data-id="0" value="Quote 1">
<input type="button" data-id="1" value="Quote 2">
<input type="button" data-id="2" value="Quote 3">
<input type="button" data-id="3" value="Quote 4">
</div>
Then the following file called cusomjavascript.js with the following code:
jQuery(document).ready(function ($) {
jQuery("#quote-section :button").click(function () {
var data = {
"quote": jQuery(this).data("id")
};
jQuery.post("/quote-service.php", data, function (response) {
var decoded_response = JSON.parse(response);
jQuery("#quote_txt").html(decoded_response.quote);
jQuery("#quote_author").html(decoded_response.author);
});
});
});
The php that returns the values should be written in a file called, service-quote.php:
//use this as the number of the quote to return
$int_quote_no = filter_var($_POST['quote'], FILTER_SANITIZE_NUMBER_INT);
//here write the rest of the code that will return the quote: