Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Im trying to read data from mysql database and pass it to my javascript file. I have search alot on internet and have found examples that doesnt work in my case.

.html file

<!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'>
<script language='JavaScript' type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<title>Display Page</title>
</head>

<body>
<button type='button' id='getdata'>Get Data.</button>

<div id='result_table'>

</div>

<script type='text/javascript' language='javascript'>

$(document).ready(function(){
$('#getdata').click(function(){
    alert("hello");
    $.ajax({
            url: 'db.php',
            type:'POST',
            dataType: 'json',
            success: function(output_string){
                    alert(output_string);
                },
                    error: function (xhr, ajaxOptions, thrownError){
                    alert(xhr.statusText);
                    alert(thrownError);
                    }
    }); 

});
});
</script>
</body>
</html>

and .php file

<?php
echo 'hello';
        $user = 'root';
        $pass = '123';
        $host = 'localhost';
        $db = 'internetProgrammeringProj';

        $connect = mysql_connect($host,$user,$pass);
        $select = mysql_select_db($db,$connect);

        $query = $_POST['query'];
        $mysql_query = mysql_query("SELECT * FROM ratt ");
        $temp = "";
        $i = 0;
        while($row = mysql_fletch_assoc($mysql_query)){
            $temp = $row['id'];
            $temp .= $row['namn'];
            $temp .= $row['typ'];
            $temp .= $row['url'];
            $temp .= $row['forberedelse'];

            $array[i] = $temp; 
            $i++;
        }



        echo json_encode($array);   
?>

alert(xhr.statusText); gives parsererror

and

alert(thrownError); gives SyntaxError: JSON.parse: unexpected character

firebug doesnt display any error in console.

QUESTION: How do i get my program to get the content from the database and pass it with json to display it with alert in ajax?

share|improve this question
The SyntaxError: JSON.parse: unexpected character suggests that jQuery failed to parse the response text as JSON because it's not valid JSON. Use Firebug to see what response text is being returned and post that here as well. – Anthony Grist Feb 21 at 11:49
check your array use vat_dump to get it – rajesh kakawat Feb 21 at 12:32

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.