kamesh
kamesh

Reputation: 2424

Getting JSON value from php using jquery ajax

Hi friends can anyone me for this plz. im new to this chapter..i am trying to get JSON format value from PHP but while im running i got this output "SyntaxError: JSON.parse: unexpected characte".. i think i had done mistake in php conversion ...plz help me friends

my .html file

<html>
<head>
    <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    <title>Display Page</title>
</head>
<body>
    <button type='button' id='getdata'>GetData.</button>
    <button type='button' id='get'>sample</button>
    <script type='text/javascript'>
    $('#get').click(function() {
        alert("laksjdflk");
    });
    $(document).ready(function() {
        $('#getdata').click(function() {
            $.ajax({
                url:'neww.php' ,
                dataType:'json' ,
                success:function(output_string) {
                    alert(output_string);
                },
                error:function(xhr,ajaxOptions,thrownError){
                    alert(xhr.statusText);
                    alert(thrownError);
                }
            });
        });
    });
    </script>
</body>
</html>

generatephp.php

<?php
    mysql_connect("localhost","root","");
    mysql_select_db("androidlogin");

    $sql=mysql_query("SELECT* FROM trysave");

    $temp="";
    $i=0;
    while($row=mysql_fetch_assoc($sql)){
        $temp=$row['stringss'];
        $temp.=$row['integerr'];
        $array[i]=$temp;
        i++;
    }
    echo json_encode($array);// this will print the output in json
?>

Upvotes: 0

Views: 1363

Answers (3)

Catalin Sterian
Catalin Sterian

Reputation: 96

i wold use something different ...

php:

<?php
    mysql_connect("localhost","root","");


    $sql=mysql_query("SELECT stringss, integerr FROM androidlogin.trysave");

    $temp=array();
    $i=0;
    while($row=mysql_fetch_assoc($sql)){
        $temp[] = $row;
           }
    echo json_encode($temp);// this will print the output in json
?>

//you do not need the $i variable since you will get in java str.length = that $i

 <script type='text/javascript'>
    $('#get').click(function() {
        alert("laksjdflk");
    });

    $(document).ready(function() {
        $('#getdata').click(
        function() {
jQuery.getJSON( "neww.php") //no get vars

.done(function(a) {
//console.clear();
console.log(a);
show_data(a);
})

.fail(function(a) {
console.log( "error" );
});

});
});


function show_data(a){

for(var i=0; i<a.length; i++)
var stringss = a[i].stringss;
var integerr = a[i].integerr;
var nuber_temp = i;
//do stuff with them ...

}
    </script>

if problem persists ... try http://php.net/manual/ro/function.urlencode.php

Upvotes: 1

Rohan Kumar
Rohan Kumar

Reputation: 40639

This may because of Undefined array variable notice you have to define array in case no records found

Also you missed a $ before variable i which gives error(treated as CONSTANT, and which is undefined in your code), i should be $i like,

$array=array();// define here to prevent from "Notice"
while($row=mysql_fetch_assoc($sql))
{
    $temp=$row['stringss'];
    $temp.=$row['integerr'];
    $array[$i]=$temp;// use $ before i
    $i++;// use $ before i
}
echo json_encode($array);// this will print the output in json

One more thing you have mentioned PHP file name as generatephp.php and you are using url:'neww.php' , in $.ajax(), you have to check your code once.

Upvotes: 2

Achrome
Achrome

Reputation: 7821

Obvious problems (cough MySQL_*) aside, your PHP file should specify in the response headers that the output will be of type JSON. The output defaults to text/html and Javascript cannot parse it as a valid JSON object.

You can do it like this

<?php
header('Content-type: application/json');
// Rest of the code remains intact

Upvotes: 1

Related Questions