Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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
?>
share|improve this question
    
See the following link [jquery-ajax-in-getting-json-value-from-php][1] [1]: stackoverflow.com/questions/19029703/… – ديناصور الأمة Dec 13 '13 at 5:08
up vote 2 down vote accepted

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.

share|improve this answer

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
share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.