Jump to content

Ajax getting data


veysel
 Share

Recommended Posts

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 ?

Link to comment
Share on other sites

<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 ?

 

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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 by veysel
Link to comment
Share on other sites

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

image.png.fb7a5c0ae12ef358411fcfba7f75a451.png

<?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>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by veysel
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.