0

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:
Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
shaun
  • 17
  • 6
  • Please clarify your question. What you exactly want? – Sanjay Chaudhari May 02 '16 at 13:09
  • **Hint**: Assuming your `quote.php` loads your JS, notice that you are calling `quote-service.php` here (not `service-quote.php`). You also need an Array of quotes in your Quote Service and then echo its `$int_quote_no` index with proper JSON headers (check [this](http://stackoverflow.com/questions/4064444/returning-json-from-a-php-script/25062961#25062961)). Anyway, judging by the comments in your code, it looks like you're trying to get your homework done here... Please, give it a try! – Jordi Nebot May 02 '16 at 13:11
  • im a front end dev, trying to learn php, got this out of a php book, as a challenge, need help on it, don't know how and where to start. I want to know how do I approach this question? – shaun May 02 '16 at 14:53

1 Answers1

0

If you are a Frontend Dev who already knows Javascript, learning the basics of PHP should be easy...

I assume you understand what's going on in your HTML (quote.php) and JS (customjavascript.js). Basically, when you click one of the four buttons, JS launches an AJAX request (via jQuery's .post()) passing to your Quote's Service the ID of the expected quote.

In your service's file (quote-service.php) you already have a variable with the requested quote ID, called $int_quote_no.

Your client's code expects the quote to be a JS object with two keys:

  1. quote
  2. author

You can represent this object in your PHP as an Array, like this:

$quote_object = [
  'quote'  => 'Later equals never.',
  'author' => 'Uncle Bob'
];

In your case, you need four quote objects, so you could create a multidimensional array:

$quotes = [
  [
    'quote'  => 'Later equals never.',
    'author' => 'Uncle Bob'
  ],
  [
    'quote'  => 'Talk is cheap. Show me the code.',
    'author' => 'Linus Torvalds'
  ],
  // Add two more quotes...
];

Then you need to echo a JSON object to your client with the requested quote. It would be something like this (remember the link I added to my previous comment regarding the JSON headers):

header('Content-type: application/json');
echo json_encode($quotes[ $int_quote_no ]);

Hope this helps. Btw, I'd recommend you to get started with PHP with some online resource on this topics. There are lots of them:

Community
  • 1
  • 1
Jordi Nebot
  • 3,355
  • 3
  • 27
  • 56