PDA

View Full Version : Resolved Ajax with PHP



Deadweight
05-08-2014, 02:20 PM
I am not entirely sure where to post this but i have a sample code. I am trying to use ajax to get information from a php page. However, I think I am either doing something wrong with the php or AJAX (Mostly the ajax I'm guessing).

Sample php page called ip.php:


$advert = array(
'ajax' => 'Hello world!',
'advert' => 'adverts'
);
echo json_encode($advert);


Sample ajax with html:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Get Ping</title>
<script src="../js/jquery.js"></script>
<script>
$(function(){
$.ajax({
url : 'ip.php',
type : 'POST',
data : data,
dataType : 'json',
success : function (result) {
$('#check').text(result['ajax']) // "Hello world!" displayed
},
error : function () {
$('#check').text("error");
}
})
})
</script>
</head>
<body>

<div id="check"></div>

</body>
</html>


Any help would be wonderful.
Thanks.

jscheuer1
05-08-2014, 03:32 PM
Get rid of the highlighted:


$(function(){
$.ajax({
url : 'ip.php',
type : 'POST',
data : data,
dataType : 'json',
success : function (result) {
$('#check').text(result['ajax']) // "Hello world!" displayed
},
error : function () {
$('#check').text("error");
}
})
})

Next time use your error console, it identified that line right away.

Deadweight
05-08-2014, 07:20 PM
Ah yeah thanks; however, it still out puts 'error' on the webpage instead of Hello World

jscheuer1
05-08-2014, 07:30 PM
Not here. I get Hello World! (I'm using wamp, so I cannot link you to a demo, sorry.)

Your php code is invalid without php tags though, just saying. If you use ip.txt:


{"ajax":"Hello world!","advert":"adverts"}

for the url it will work. (jQuery version 1.8.3)



If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.

Deadweight
05-08-2014, 08:26 PM
Website URL: http://thebcelements.com/dhtml_test/.
Also there are php tags
The whole php page


<?php

$advert = array(
'ajax' => 'Hello world!',
'advert' => 'adverts',
);
echo json_encode($advert);
?>

jscheuer1
05-08-2014, 08:38 PM
On ip.php, get rid of everything except the PHP. That means remove:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Pinger</title>

</head>
<body>


and:


</body>
</html>

If you don't have anything like that in the file, the server must be adding it. I doubt that though. Could happen I guess.

And, although it might not be necessary, remove any leading or trailing whitespace. Here is the full code of my ip.php file:


<?php
$advert = array(
'ajax' => 'Hello world!',
'advert' => 'adverts'
);
echo json_encode($advert);
?>

Deadweight
05-08-2014, 10:08 PM
Thanks again. If i wanted to send data to the php i would add data back and send it to the php page how would i do that?
EG add this to the php file:


$ip = $_REQUEST["ip"];


and this to the jquery:
data: {ip:"0.0.0.0"} //I know this is wrong.

jscheuer1
05-09-2014, 01:04 AM
That's OK (using a valid object). You can also use a string (its syntax is like a query string without the preceding ? character):


data: 'ip=0.0.0.0'

Deadweight
05-09-2014, 01:45 AM
Okay I've tried that and I am receiving another error.

HTML URL: http://thebcelements.com/dhtml_test/
PHP URL: http://thebcelements.com/dhtml_test/ip.php

JQuery:

$(function(){
$.ajax({
url : 'ip.php',
type : 'POST',
data: "ip=www.google.com",
dataType : 'json',
success : function (result) {
$('#check').text(result['ajax']) // "Hello world!" alerted
},
error : function () {
$('#check').text("error");
}
})
})

PHP:

<?php
$ip = $_REQUEST["ip"];
//$ip = "108.163.135.166";
exec("ping -n 5 $ip", $output, $status);

$check = 0==$status?"Success":"Failed";

$advert = array(
'ajax' => $ip.' - '.$check
);

echo json_encode($advert);
echo print_r($output);
echo '<br />'.$ip.' - '.$check.' - '.$status;
?>

jscheuer1
05-09-2014, 02:05 AM
If you are instructing jQuery to make the AJAX request one of dataType json, ip.php must output a valid json string and only a valid json string. It cannot if it includes any preceding or trailing output that is not a part of the valid json string. In this particular case, the following must be removed:


echo print_r($output);
echo '<br />'.$ip.' - '.$check.' - '.$status;

If you want to keep that code there for diagnostic purposes, it has to be turned off somehow during the actual AJAX json request. Or only turned on if - say a diagnostic param is passed to the file.