Note that there are some explanatory texts on larger screens.

plurals
  1. POupdating div without page refresh
    primarykey
    data
    text
    <p>i have a php script that counts button clicks to a txt file. this is working just fine.<br/> On my html page i am displaying the clicks as such :</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;form action="" method="post" id="form" &gt; &lt;button class="vote_right" name="clicks1" &gt;&lt;/button&gt; &lt;/form&gt; &lt;form action="" method="post" id="form2" &gt; &lt;button class="vote_left" name="clicks2"&gt;&lt;/button&gt; &lt;/form&gt; </code></pre> <p>the php</p> <pre><code>&lt;?php if( isset($_POST['clicks1']) ) { incrementClickCount1(); } function getClickCount1() { return (int)file_get_contents("count_files/clickcount1.txt"); } function incrementClickCount1() { $count = getClickCount1() + 1; file_put_contents("count_files/clickcount1.txt", $count); } if( isset($_POST['clicks2']) ) { incrementClickCount2(); } function getClickCount2() { return (int)file_get_contents("count_files/clickcount2.txt"); } function incrementClickCount2() { $count2 = getClickCount2() + 1; file_put_contents("count_files/clickcount2.txt", $count2); } ?&gt; </code></pre> <p>and the js (EDITED)</p> <pre><code>&lt;script type="text/javascript"&gt; var valueOne = "&lt;?php echo getClickCount1(); ?&gt;"; var valueTwo = "&lt;?php echo getClickCount2(); ?&gt;"; $('#one').html(valueOne); $('#two').html(valueTwo); $('.vote_right').click(function(){ $.ajax({ type: "POST", url: "counter.php", data: valueOne, cache: false, success: function(data) { $("#one").html(data); } }); &lt;/script&gt; </code></pre> <p>tried also adding </p> <pre><code>e.preventDefault(); </code></pre> <p>but then the button wont work. My problam is that i dont want the page to refresh when the button is clicked. insted i would like the div to refresh with the new data. <br/> I have tried usin ajax but with no success. its either the page refreshes or the data wont update. <br/> Any suggestions? </p>
    singulars
    1. This table or related slice is empty.
    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.
 

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