0

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
?>
kamesh
  • 2,374
  • 3
  • 25
  • 33
  • See the following link [jquery-ajax-in-getting-json-value-from-php][1] [1]: http://stackoverflow.com/questions/19029703/jquery-using-ajax-to-send-data-and-save-in-php/19029778#19029778 – ديناصور الأمة Dec 13 '13 at 05:08

3 Answers3

2

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.

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
1

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
Achrome
  • 7,773
  • 14
  • 36
  • 45
1

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

Catalin Sterian
  • 96
  • 1
  • 10