veysel Posted September 16, 2019 Share Posted September 16, 2019 Hello friends, i need a help i have index.php and response.php and i am try to get data from response.php per once second and i am getting data in script tag. I want to use incoming data in php tag and i will parse the data however i cant do that anyone can help me ? Quote Link to comment https://forums.phpfreaks.com/topic/309236-ajax-getting-data/ Share on other sites More sharing options...
chhorn Posted September 16, 2019 Share Posted September 16, 2019 The data in the script-tag won't update every second, you have to call the PHP-script every second. Quote Link to comment https://forums.phpfreaks.com/topic/309236-ajax-getting-data/#findComment-1569718 Share on other sites More sharing options...
veysel Posted September 16, 2019 Author Share Posted September 16, 2019 <html> <body> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { //açılışta çalışması için gonder(); //her 1 saniyede bir yenile var int=self.setInterval("gonder()",1000); }); function gonder(){ $.ajax( { type: "JSON", url: 'response.php', success: function (data) { alert(data); }, } ); } </script> </body> </html> <?php echo data; ?> // i want to use like that and i will parse it. is it possible ?  Quote Link to comment https://forums.phpfreaks.com/topic/309236-ajax-getting-data/#findComment-1569720 Share on other sites More sharing options...
Barand Posted September 16, 2019 Share Posted September 16, 2019 And what, exactly, is the problem you are having? Quote Link to comment https://forums.phpfreaks.com/topic/309236-ajax-getting-data/#findComment-1569724 Share on other sites More sharing options...
veysel Posted September 16, 2019 Author Share Posted September 16, 2019 yes i have no much knowledge this tech. maybe my question is weird Quote Link to comment https://forums.phpfreaks.com/topic/309236-ajax-getting-data/#findComment-1569725 Share on other sites More sharing options...
veysel Posted September 16, 2019 Author Share Posted September 16, 2019 i just want to use incoming data which is coming from response.php in php tag Quote Link to comment https://forums.phpfreaks.com/topic/309236-ajax-getting-data/#findComment-1569728 Share on other sites More sharing options...
Barand Posted September 16, 2019 Share Posted September 16, 2019 Here's an example <?php session_start(); if (isset($_GET['ajax'])) { // respond to the ajax request $val = $_SESSION['cumval'] ?? 0; ++$val; $_SESSION['cumval'] = $val; exit("$val <br>"); } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Sample</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> $().ready( function() { setInterval('gondor()', 1000) }) function gondor() { $.get( "" , // blank url - calls self {"ajax" : 1} , // data to pass to url function(resp) { $("#output").append(resp) }, "TEXT" ) } </script> <style type="text/css"> body { font-family: verdana,sans-serif; font-size: 12pt; padding: 20px 50px; } </style> </head> <body> <div id="output"> </div> </body> </html> Â Quote Link to comment https://forums.phpfreaks.com/topic/309236-ajax-getting-data/#findComment-1569730 Share on other sites More sharing options...
veysel Posted September 16, 2019 Author Share Posted September 16, 2019 (edited) thank you but i tried this and it is not what i want. this is the response.php content. i just want to get $dizi from response.php  # sample data $resp = <<<EOF {"class":"POLL","time":"2010-04-05T21:27:54.84Z","active":1, "tpv":[{"class":"TPV","tag":"MID41","device":"/dev/ttyUSB0", "time":1270517264.240,"ept":0.005,"lat":40.035093060, "lon":-75.519748733,"alt":31.1,"track":99.4319, "speed":0.123,"mode":3}], "sky":[{"class":"SKY","tag":"MID41","device":"/dev/ttyUSB0", "time":"2010-04-05T21:27:44.84Z","hdop":9.20,"vdop":12.1, "satellites":[{"PRN":16,"el":55,"az":42,"ss":36,"used":true}, {"PRN":19,"el":25,"az":177,"ss":0,"used":false}, {"PRN":7,"el":13,"az":295,"ss":0,"used":false}, {"PRN":6,"el":56,"az":135,"ss":32,"used":true}, {"PRN":13,"el":47,"az":304,"ss":0,"used":false}, {"PRN":23,"el":66,"az":259,"ss":40,"used":true}, {"PRN":20,"el":7,"az":226,"ss":0,"used":false}, {"PRN":3,"el":52,"az":163,"ss":32,"used":true}, {"PRN":31,"el":16,"az":102,"ss":0,"used":false} ] } ] } EOF; echo json_encode($dizi);  Edited September 16, 2019 by veysel Quote Link to comment https://forums.phpfreaks.com/topic/309236-ajax-getting-data/#findComment-1569733 Share on other sites More sharing options...
veysel Posted September 16, 2019 Author Share Posted September 16, 2019 here is the web interface   Quote Link to comment https://forums.phpfreaks.com/topic/309236-ajax-getting-data/#findComment-1569735 Share on other sites More sharing options...
veysel Posted September 16, 2019 Author Share Posted September 16, 2019 time is $dizi[16][0] latitude is $dizi[15][0] i am trying to do like above Quote Link to comment https://forums.phpfreaks.com/topic/309236-ajax-getting-data/#findComment-1569736 Share on other sites More sharing options...
veysel Posted September 17, 2019 Author Share Posted September 17, 2019 can anyone have an idea ? Â Quote Link to comment https://forums.phpfreaks.com/topic/309236-ajax-getting-data/#findComment-1569757 Share on other sites More sharing options...
Barand Posted September 17, 2019 Share Posted September 17, 2019 I haven't a clue what $dizi is - it doesn't appear in your code. But if your ajax call is receiving a response like that in your code then the code below is an example of how to grab the data from it EG <?php if (isset($_GET['ajax'])) { // respond to the ajax request $resp = <<<EOF {"class":"POLL","time":"2010-04-05T21:27:54.84Z","active":1, "tpv":[{"class":"TPV","tag":"MID41","device":"/dev/ttyUSB0", "time":1270517264.240,"ept":0.005,"lat":40.035093060, "lon":-75.519748733,"alt":31.1,"track":99.4319, "speed":0.123,"mode":3}], "sky":[{"class":"SKY","tag":"MID41","device":"/dev/ttyUSB0", "time":"2010-04-05T21:27:44.84Z","hdop":9.20,"vdop":12.1, "satellites":[{"PRN":16,"el":55,"az":42,"ss":36,"used":true}, {"PRN":19,"el":25,"az":177,"ss":0,"used":false}, {"PRN":7,"el":13,"az":295,"ss":0,"used":false}, {"PRN":6,"el":56,"az":135,"ss":32,"used":true}, {"PRN":13,"el":47,"az":304,"ss":0,"used":false}, {"PRN":23,"el":66,"az":259,"ss":40,"used":true}, {"PRN":20,"el":7,"az":226,"ss":0,"used":false}, {"PRN":3,"el":52,"az":163,"ss":32,"used":true}, {"PRN":31,"el":16,"az":102,"ss":0,"used":false} ] } ] } EOF; exit($resp); // return the ajax response } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Sample</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> $().ready( function() { gondor() }) function gondor() { $.get( "" , // blank url - calls self {"ajax" : 1} , // data to pass to url function(resp) { $("#time").html(resp.tpv[0].time) $("#latitude").html(resp.tpv[0].lat) $("#longitude").html(resp.tpv[0].lon) $("#altitude").html(resp.tpv[0].alt) $("#hdop").html(resp.sky[0].hdop) $("#vdop").html(resp.sky[0].vdop) }, "JSON" ) } </script> <style type="text/css"> body { font-family: verdana,sans-serif; font-size: 12pt; padding: 20px 50px; } #output { width:60%; margin: 50px auto; border: 2px solid gray; padding: 30px; } .data {width:150px; display: inline-block; text-align: right; color: #369; padding: 8px; } label {width:150px; display: inline-block; text-align: left; padding: 8px; } </style> </head> <body> <div id="output"> <label>Time</label>:<div id="time" class="data"></div><br> <label>Longitude</label>:<div id="longitude" class="data"></div><br> <label>Latitude</label>:<div id="latitude" class="data"></div><br> <label>Altitude</label>:<div id="altitude" class="data"></div><br> <label>HDOP</label>:<div id="hdop" class="data"></div><br> <label>VDOP</label>:<div id="vdop" class="data"></div> </div> </body> </html> Â Quote Link to comment https://forums.phpfreaks.com/topic/309236-ajax-getting-data/#findComment-1569758 Share on other sites More sharing options...
veysel Posted September 17, 2019 Author Share Posted September 17, 2019 Hello first of all thank you, i am going to try that you share i hope it will be okay this time Quote Link to comment https://forums.phpfreaks.com/topic/309236-ajax-getting-data/#findComment-1569759 Share on other sites More sharing options...
Barand Posted September 17, 2019 Share Posted September 17, 2019 1 minute ago, veysel said: i hope it will be okay this time Yeah, really sorry that my first reply wasn't what you wanted. But then again, the question you asked was nothing like what you wanted either and was a complete waste of time all round. And as I voulunteer my time here for free, I don't like having it wasted. If this wasn't what you wanted, learn how to ask better questions. Quote Link to comment https://forums.phpfreaks.com/topic/309236-ajax-getting-data/#findComment-1569760 Share on other sites More sharing options...
veysel Posted September 17, 2019 Author Share Posted September 17, 2019 oh sorry for my questions, i am going to be more clearly after that. thank you for your voulunteer and i am aware of it. sorry for my faulty question Quote Link to comment https://forums.phpfreaks.com/topic/309236-ajax-getting-data/#findComment-1569761 Share on other sites More sharing options...
veysel Posted September 18, 2019 Author Share Posted September 18, 2019 (edited) Hello Mr. Barand thank you so much, the problem is solved with your help. <html> <body> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> <script type="text/javascript"> $().ready( function() { setInterval('gondor()', 1000) gondor() }) function gondor() { $.get( "response.php" , function(data) { let obj = JSON.parse(data) $("#zaman").html(obj.tpv[0].time) $("#latitude").html(obj.tpv[0].lat) $("#longitude").html(obj.tpv[0].lon) $("#altitude").html(obj.tpv[0].alt) $("#fixtype").html(obj.tpv[0].mode) $("#hdop").html(obj.sky[0].hdop) $("#vdop").html(obj.sky[0].vdop) }, "JSON" ) } </script> </body> </html> <div class="col-md-4"> <div class="panel_s"> <div class="panel-heading" style="text-align: center;">Current Information</div> <div class="panel-body text-left"> <div class="row"> <label>Time(UTC) <?php for ($a = 0; $a<20; $a+=1) { echo " "; } ?>:</label> <?php for ($a = 0; $a<4; $a+=1) { echo " "; } ?> <span id="zaman"></span> </div><br>  Edited September 18, 2019 by veysel Quote Link to comment https://forums.phpfreaks.com/topic/309236-ajax-getting-data/#findComment-1569807 Share on other sites More sharing options...
Barand Posted September 18, 2019 Share Posted September 18, 2019 You shouldn't need the JSON.parse(data), you can use "data" directly EG $("#latitude").html(data.tpv[0].lat)  Quote Link to comment https://forums.phpfreaks.com/topic/309236-ajax-getting-data/#findComment-1569808 Share on other sites More sharing options...
veysel Posted September 18, 2019 Author Share Posted September 18, 2019 Hello, response.php file return : echo json_encode($resp); that's why i used it Quote Link to comment https://forums.phpfreaks.com/topic/309236-ajax-getting-data/#findComment-1569810 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.