Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The below shows how to write two of the 4 api calls you need to add to your website.</p> <p><strong>Main Page:</strong></p> <pre><code>&lt;?php include ('counter.php'); ?&gt; &lt;div class="count_right" id="one"&gt;&lt;/div&gt; &lt;div class="count_left" id="two"&gt;&lt;/div&gt; &lt;button class="vote_right" name="clicks1" onclick="addCount1()"&gt;&lt;/button&gt; &lt;button class="vote_left" name="clicks2" onclick="addCount2()"&gt;&lt;/button&gt; </code></pre> <p><strong>JS:</strong></p> <pre><code>&lt;script type="text/javascript"&gt; var valueOne = //use ajax (GET) to call the page for count1; var valueTwo = //use ajax (GET) to call the page for count2; $(document).ready(function(){ $('#one').html(valueOne); $('#two').html(valueTwo); }); function addCount1(){ //use ajax (POST/PUT) to call a page that adds 1 to your text file thing for count1 } function addCount2(){ //use ajax (POST/PUT) to call a page that adds 1 to your text file thing for count2 } &lt;/script&gt; </code></pre> <p><strong>New API Page (for count1):</strong></p> <pre><code>&lt;?php class IntegerValue implements JsonSerializable { public function __construct($number) { $this-&gt;number = (integer) $number; } public function jsonSerialize() { return $this-&gt;number; } } echo json_encode(new IntegerValue(getClickCount1()), JSON_PRETTY_PRINT); ?&gt; </code></pre> <p><strong>New API page (for count2):</strong></p> <pre><code>&lt;?php class IntegerValue implements JsonSerializable { public function __construct($number) { $this-&gt;number = (integer) $number; } public function jsonSerialize() { return $this-&gt;number; } } echo json_encode(new IntegerValue(getClickCount2()), JSON_PRETTY_PRINT); ?&gt; </code></pre> <p><strong>How PHP and JavaScript work</strong></p> <p>On initial page load: The process flow looks like this:</p> <blockquote> <p>Server Side code (php) -> sends data to client -> browser begins rendering and executing JS</p> </blockquote> <p>On form Submit:</p> <blockquote> <p>Client Executes code (javascript) -> Sends data to server -> Server gets data and processes (PHP)</p> </blockquote> <p>Because of this if you don't want the page to "refresh" you'd have to use JavaScript and the best way of handling this is <code>AJAX</code> if you use a form you are going to get a refresh also known as a POST-BACK. Otherwise with <code>AJAX</code> you can easily just send a <code>POST</code> or a <code>GET</code> and it won't refresh.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload