www.webdeveloper.com
Results 1 to 10 of 10

Thread: Need help PHP click button counter refreshing the page

  1. #1
    Join Date
    Mar 2013
    Location
    Montreal
    Posts
    4

    Cool Need help PHP click button counter refreshing the page

    Hi people !

    I'm new out here and newbie in scripting. I succeeded doing the whole website but right now, I got a real problem.

    I made a click button counter with PHP (same as Facebook like button) and it works very good ! As you close the page, delete cookies, do anything, the count stays ! YEEEA !
    The issue here is when I click the button (being an 'a href' to my php script) it refreshes the page, and THIS, is annoying. I want it to work without refreshing; You click and you go !

    Heres the code;

    <?php
    if(!file_exists('medias/counter.txt')){
    file_put_contents('medias/counter.txt', '0');
    }
    if($_GET['click'] == 'yes'){
    file_put_contents('medias/counter.txt', ((int) file_get_contents('medias/counter.txt')) + 1);
    header('Location: ' . $_SERVER['SCRIPT_NAME']);
    die;
    }
    ?>
    <head>
    </head>

    <body>
    <p><a href="?click=yes"><img src="images/like.png"></a>| <?php echo file_get_contents('medias/counter.txt'); ?>like this.</p>
    </body>


    Can someone help me out on this one ?
    Would be greatly appreciated !!!

    Thank y'all !

    -eragraff

  2. #2
    Join Date
    Mar 2013
    Posts
    14
    Hey, Well here is your problem,
    Quote Originally Posted by eragraff View Post
    <a href="?click=yes"><img src="images/like.png"></a>
    The reason it is refreshing the page, well it actually is not refreshing the page, you are sending a command to the php file that is causing the page to come back to itself.

    I am guessing the page you are calling it from is index.php so because you are using a $_GET method it is causing the page to go back to itself instead of it processing the script from the current page without it having to goto another location.

    To be honest you will be better off doing this with Javascript as it processes the code right there on the page without it going to another page.

    Happy coding!

    Wolveriene401

  3. #3
    Join Date
    Mar 2013
    Location
    Montreal
    Posts
    4
    Well, thank you Wolveriene401 !
    That is exactly what I wanted to be confirmed ^^

    At this moment, I've tried many tricks to do it JS before PHP but never succeed I did the button, count was okay too without refreshing the page but i failed linkin' it to the server. So as I refresh the page or come back the the page, count was down to zero...

    Sorry for asking, but do you have a JS trick to refer me ?
    You would be my HERO hahaha

    Thank a lot !!!!

    eragraff

  4. #4
    Join Date
    Mar 2013
    Posts
    14
    Hey!,

    No problem

    I have created similar to the one your needing, I will see if I can locate it for you. I will get back to this post once I found it

    I may have to write one up again though.

    We will see, I think I did this with JS and php in one script, this included sending various codes to an iframe which would be hidden from view.

    The iframe will handle the server-side stuff for you.

    I will get back to you either later tonight when I return from work or in the morning tomorrow

    Happy coding!

    Wolveriene401

  5. #5
    Join Date
    Mar 2013
    Location
    Montreal
    Posts
    4
    You make my day
    I'll be waiting for your come-back.
    Infinite thanks !!!

    eragraff

  6. #6
    Join Date
    Mar 2013
    Posts
    14
    Hey,

    I have managed to create a working script for you !

    To be honest I do like this script you have here but I noticed 1 or 2 problems with it so I made some minor changes to get it to work properly.

    First things first before I can complete the script for you, I need to know the size of your like.png file in pixels height and width so that I can get the code to appear correctly.

    Perhaps attach the image to your next reply so that I can get it applied

    As soon as you do that I should have it complete with-in 5 mins of receiving it

    Happy Coding!

    Wolveriene401

  7. #7
    Join Date
    Mar 2013
    Posts
    14
    Hey,

    Actually scratch that previous post

    I have completed the code for you

    Put this part on the page you want the script to appear.

    PHP Code:
    <?php
    if(!file_exists('medias/counter.txt')){
    file_put_contents('medias/counter.txt''0');
    }
    elseif(
    $_GET['like'] == 1){
    file_put_contents('medias/counter.txt', ((int) file_get_contents('medias/counter.txt')) + 1);
    header("Location: like.php");
    }
    ?>
    <head>
    <!-- This script loads when the page loads and checks to see if the user has javascript enabled or not -->
    <script>
    window.setTimeout("jsload()", 100);
    </script>
    </head>

    <body>
    <!-- If javascript is enabled it will continue with the javascript vs PHP option -->
    <script>
    function jsload()
    {
    document.getElementById('nojs').style.display='';
    }
    </script>
    <label>
    <!-- This is the script that will parse out if the user does not have javascript enabled -->
    <noscript><p><a href="?like=1"><img height="32px" alt="Like this page" width="32px" src="images/like.png"></a>| <b><?php echo file_get_contents('medias/counter.txt'); ?></b> like this.</p></noscript>
    <div id="nojs" style="display:none"><a href="javascript:like()"><img src="images/like.png" alt="Like this page" width="32px" height="32px" border="0"></a>
    <input name="likethis" type="text" id="likethis" value="<?php echo file_get_contents('medias/counter.txt'); ?>" style="border:none; cursor:text;" readonly="true" size="8">
    <!-- This script sends a command to the iframe that is hidden to public view as well as upping the value of the text field which is hidden to make it look like actual text :) -->
    <script>
    function like()
    {
    document.getElementById('server-like').src="like.php?like=1";
    document.getElementById('likethis').value++
    a=a+1; 
    }
    </script>

    </label>
    </body><iframe src="like.php" id="server-like" style="display:none"></iframe>
    Put this code in a file named like.php.
    PHP Code:
    <?php
    if(!file_exists('medias/counter.txt')){
    file_put_contents('medias/counter.txt''0');
    }
    elseif(
    $_GET['like'] == 1){
    file_put_contents('medias/counter.txt', ((int) file_get_contents('medias/counter.txt')) + 1);
    header("Location: like.php");
    die;
    }
    ?>
    <head>
    </head>

    <body>
    <b><?php echo file_get_contents('medias/counter.txt'); ?></b> like this.</p>
    </body>

  8. #8
    Join Date
    Mar 2013
    Posts
    14
    Quote Originally Posted by wolveriene401 View Post
    Hey,

    Actually scratch that previous post

    I have completed the code for you

    Put this part on the page you want the script to appear.

    PHP Code:
    <?php
    if(!file_exists('medias/counter.txt')){
    file_put_contents('medias/counter.txt''0');
    }
    ?>
    <head>
    <!-- This script loads when the page loads and checks to see if the user has javascript enabled or not -->
    <script>
    window.setTimeout("jsload()", 100);
    </script>
    </head>

    <body>
    <!-- If javascript is enabled it will continue with the javascript vs PHP option -->
    <script>
    function jsload()
    {
    document.getElementById('nojs').style.display='';
    }
    </script>
    <label>
    <!-- This is the script that will parse out if the user does not have javascript enabled -->
    <noscript><p><a href="?like=1"><img height="32px" alt="Like this page" width="32px" src="images/like.png"></a>| <b><?php echo file_get_contents('medias/counter.txt'); ?></b> like this.</p></noscript>
    <div id="nojs" style="display:none"><a href="javascript:like()"><img src="images/like.png" alt="Like this page" width="32px" height="32px" border="0"></a>
    <input name="likethis" type="text" id="likethis" value="<?php echo file_get_contents('medias/counter.txt'); ?>" style="border:none; cursor:text;" readonly="true" size="8">
    <!-- This script sends a command to the iframe that is hidden to public view as well as upping the value of the text field which is hidden to make it look like actual text :) -->
    <script>
    function like()
    {
    document.getElementById('server-like').src="like.php?like=1";
    document.getElementById('likethis').value++
    a=a+1; 
    }
    </script>

    </label>
    </body><iframe src="like.php" id="server-like" style="display:none"></iframe>
    Put this code in a file named like.php.
    PHP Code:
    <?php
    if(!file_exists('medias/counter.txt')){
    file_put_contents('medias/counter.txt''0');
    }
    elseif(
    $_GET['like'] == 1){
    file_put_contents('medias/counter.txt', ((int) file_get_contents('medias/counter.txt')) + 1);
    header("Location: like.php");
    die;
    }
    ?>
    <head>
    </head>

    <body>
    <b><?php echo file_get_contents('medias/counter.txt'); ?></b> like this.</p>
    </body>
    Whoops!, sorry I forgot to remove a statement.

    I have Altered this above!

    Happy Coding!

    Wolveriene401

  9. #9
    Join Date
    Mar 2013
    Location
    Montreal
    Posts
    4
    AHAHAHA F*** YEAH !

    I've tried a 5 minuts integrating it and it workd !
    Still I have to do it properly because I did it quite fast putting codes anywhere in the page but .. MAN U ARE INSANE (in the good way )
    Thank you SOOOOOOO much !!!!!!! Sure I could never have done this by myself -_- haha

    I'm gonna try this more concrete later on, if I have any questions, I'll be back here !

    Infinite thanks again and again

    eragraff

  10. #10
    Join Date
    Mar 2013
    Posts
    14

    resolved

    You are very welcome eragraff

    Glad it worked for you!


    Happy Coding!

    Wolveriene401

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles