-2

I am unable to stop the previous variable loading during the ajax call.

Please find onclick I am passing the variable and sending the value to ajax call to PHP script.

I having the data:

rnc.csv

DLRNC01
DLRNC02
DLRNC03
DLRNC04
DLRNC05
DLRNC06
DLRNC07
DLRNC08
DLRNC09
DLRNC10
DLRNC11
DLRNC12
DLRNC13
DLRNC14

code

<?php

         if (($handle = fopen("rnc.csv", "r")) !== FALSE) {

            $i=0;

        $values=array();

        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

                        ?>

                        <a href="javascript:void(0)"  onclick="pop(<?php echo "'".$data[0]."'"; ?>)" > <?php echo $data[0]; ?></a></br>
                <?php               
                }

            fclose($handle);

        }else{

            echo "File not found";
        }
?>

<div id="container1"></div>

<script>

        function pop(rnc)
        {
            $.ajax({
            url: 'ajax1.php',
            type: "POST",
            cache: false,
            //dataType:'json',
            data:{rnc:rnc},
            success: function(data){

                        $("#container1").html(data);

                        setInterval(function(){

                                console.log(rnc);

                                $("#container1").html(data);

                        },5000);
                }
            });
        }

</script>

ajax1.php

<?php echo $_POST['rnc']; ?>

when I am passing the value to the ajax1.php, after clicking the values of the file two to more , than the ajax call is echoing what ever the values having previous called displaying for every 5 seconds.

When I do console.log in the browser :

ajax.php:344 DLRNC01
ajax.php:344 DLRNC05
ajax.php:344 DLRNC07
ajax.php:344 DLRNC04
ajax.php:344 DLRNC01
ajax.php:344 DLRNC02
2 ajax.php:344 DLRNC05
ajax.php:344 DLRNC06
ajax.php:344 DLRNC07
ajax.php:344 DLRNC04
ajax.php:344 DLRNC01
ajax.php:344 DLRNC02

clicking the particular value, calling the previous value in the second time...

Please help me what is the solution..

Mahesh Cholleti
  • 19
  • 1
  • 10
  • You start an interval every time `pop` is called, but you never stop the previous intervals, so... they won't stop. Try... stopping them. – Kevin B Apr 17 '15 at 16:03
  • Hi @KevinB...Its a function pop() , so that i can get the value from the on onclick.. – Mahesh Cholleti May 07 '15 at 05:45
  • right, and every time you click, pop is executed, thus starting a new interval but not stoping the previous. – Kevin B May 07 '15 at 05:46
  • Set Interval is required , because the data from the back end is always updating for the 1 minute...so that users can monitor..pls tell how to stop the previous variable called in ajax call load() function. – Mahesh Cholleti May 07 '15 at 05:46
  • https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval – Kevin B May 07 '15 at 05:47
  • @KevinB ..thanks for the info link, here I din't any info to clear the previous called variable during the ajax call...can you please explain... – Mahesh Cholleti May 07 '15 at 06:19
  • @KevinB.. I even tried with different ajax types : $.ajax({ url: 'ajax1.php', type: "POST", cache: false, data:{rnc:rnc}, success: function(data){ $("#container1").html(data); setInterval(function(){ console.log(rnc); $("#container1").html(data); },2000); } }); If I check in the console also...its loading the previous variable ..please help... – Mahesh Cholleti May 07 '15 at 06:52

1 Answers1

-1

Your problem is that the value is loading again and again, right?

It seems to me your pop function is wrong:

function pop(vali)
{
    $("#home").load("rnc_value.php",{rnc:vali},function(){});
setInterval(function(){
$("#home").load('rnc_value.php',{rnc:vali},function(){});
}, 5000);
}

Why would you have a ajax load(), and then setInterval to load it again and again every 5 seconds?

Shouldn't it be? function pop(vali) { $("#home").load("rnc_value.php",{rnc:vali},function(){}); }

Although I don't understand what you want to load with simply returning the POST value..


If this is a caching issue, you could disable caching through $.ajaxSetup({ cache: false });

See Prevent browser caching of jQuery AJAX call result

Community
  • 1
  • 1
Rein Baarsma
  • 1,466
  • 13
  • 22
  • Maybe you can explain a bit better what you are trying to achieve? To me the code should simply return the value you've clicked on again and again in the html element with id="home" and if you click on another it will probably change back and forth, while the 2 intervals toggle eachother. I think your problem is solved if you remove the setInterval function, although I still don't understand what you want to achieve. – Rein Baarsma Apr 17 '15 at 16:05
  • Thanks for suggestion , but I need set interval function and the back end data will update automatically so that the users can monitor , the set interval function is required, but in the HOME tage I am displaying the content even though the previous values are displaying of two or more values are clicked...the previous values are passing to ajax call to PHP script...avoiding the previous values to and send the value to ajax call to php script...can you suggest me..how...? – Mahesh Cholleti Apr 19 '15 at 04:48