Results 1 to 4 of 4

Thread: Run php code onclick?

  1. #1
    Fanatic Member
    Join Date
    May 09
    Posts
    868

    Run php code onclick?

    Hi guys, so im using the current code to refresh a div tag every 10 seconds, the code im using is...

    Code:
    <script type="text/javascript">
    var auto_refresh = setInterval(
    function ()
    {
    $('#load_tweets').load('getnotifications.php').fadeIn("slow");
    }, 10000); // refresh every 10000 milliseconds</script>
    
    <body>
    </head>
    <body>
    <script type="text/javascript">
    that code works and refreshes my div tag, now im trying to make it so when a url is clicked it will run php code without refrshing the entire page, 9the php code updates my database)

    The code im using is

    Code:
    function doit()
    {
     
      $.get("updatenotifications.php");
        return false; } 
    
    
    
    </script>

    however, it seems when ever it refreshes or the page gets manually refreshed, it will run the function doit() and update my db..

    Is there a way i can make it only run the function onclick ? rather than whn ever its refreshed?

    Thanks,
    Jamie

  2. #2
    Addicted Member
    Join Date
    Feb 10
    Location
    Damascus - Syria
    Posts
    133

    Re: Run php code onclick?

    Hi

    is your code similar to this?

    HTML Code:
    <a href="some url" onclick="doit();">click here</a>
    if true, then the browser would navigate to the required URL, try something like this

    HTML Code:
    <a href="some url" onclick="return false;doit();">click here</a>
    the "return false" should fire when the user hits click, it doesn't mean anything inside the doit() function.
    Last edited by fjober; Jan 27th, 2012 at 10:27 PM.

  3. #3
    Fanatic Member
    Join Date
    May 09
    Posts
    868

    Re: Run php code onclick?

    Hi Mate

    Thanks for your reply,

    Altough, the code you provided does not seem to fix the issue it seems that every time the div gets refreshed it will run

    Code:
    <script type="text/javascript">
    
    function doit()
    {
     
      $.get("http://weloveradio.co.uk/site/wp-content/themes/main/updatenotifications.php");
        return false; } 
    
    
    
    </script>
    Its confusing me a lot! Any other ideas as why it could be causing it everytime the div tag gets refreshed?

  4. #4
    Addicted Member
    Join Date
    Feb 10
    Location
    Damascus - Syria
    Posts
    133

    Re: Run php code onclick?

    don't worry your problem is easy to solve , but let me please understand your required scenario, do you want the div content to refresh itself automatically? or just when user click on the link or both?

    anyway, this is a simple example where content will be refreshed only when user hits refresh link

    HTML Code:
    <script type="text/javascript">
        $(document).ready(function(){
            $(".refresh").click(function(){
                $.get("ajax.php", function(result){
                    $("#div").html(result).fadeIn('slow');
                });
            });
        });
    </script>
    
    <div id="div"></div>
    <a href="" onclick="return false;" class="refresh">refresh</a>
    and this is another example where content will refresh automatically every 10 seconds using timeout with timerID and will refresh when user hits refresh too

    note: when user hits refresh the timer would reset and wait new 10 seconds

    HTML Code:
    <script type="text/javascript">
    $(document).ready(function(){
        var autoRefresh = window.setTimeout("loadData()", 10000);
        $(".refresh").click(function(){
            window.clearTimeout(autoRefresh);
            loadData();
        });
    });
    function loadData()
    {
        $.get("ajax.php", function(result){
            $("#div").html(result).fadeIn('slow');
        });
        autoRefresh = window.setTimeout("loadData()", 10000);
    }
    </script>
    
    <div id="div"></div>
    <a href="" onclick="return false;" class="refresh">refresh</a>
    good luck,
    Feras Jobeir
    Last edited by fjober; Jan 28th, 2012 at 03:04 PM.

Posting Permissions

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