user3888775
user3888775

Reputation: 113

ajax jquery errors on json return value from php script

I'm having problems with this ajax/jquery programming. I've tried many different things but nothing has worked.

Ajax posts selItem to ajaxsql.php, this works! The sql query in ajaxsql.php works, cause it outputs this if i call the script directly in the browser: [{"forumname":"SDE forum","user":"michael","txt":"Jeg hedder Michael!"}]

The problem is that the ajax function shows an alert box with Error[object Object]

forum.php script:

<script type="text/javascript">

function ForumChat(selItem) {


 $.ajax({
        type: "POST",
        url: 'ajaxsql.php',
        data: { selectedItem : selItem.value },
        dataType: "json",
        success: function(data) {
            alert(data);
            $('#txtarea').html(data);
        },

        error: function(data) {
            alert('Error' + data);
        }
    });


}
</script>

ajaxsql.php script:

<?php
if(!isset($_SESSION))
{
session_start();
}
include('class.php');

//$sel = $_POST['selectedItem'];
$sel = "SDE forum";


$sql = " SELECT * FROM forum WHERE user = '".$_SESSION['currentuser']."' AND forumname = '".$sel."' ";


    $result = mysqli_query($_SESSION['con'], $sql);

        while($row = mysqli_fetch_array($result)) 
        {       
            $forumname = $row['forumname'];
            $user = $row['user'];
            $txt = $row['text'];

            $return[] = array("forumname" =>$forumname, "user" =>$user, "txt" =>$txt);

        }

        echo json_encode($return);

?>

Upvotes: 0

Views: 37

Answers (1)

Demonyowh
Demonyowh

Reputation: 1672

because ajaxsql.php returns object ..

what you can do in your ajax is

success: function(response) {
     $('#txtarea').html('');
     $.each(response.data, function(){
         console.log(this);
         $('#txtarea').append(data);
     });
},

Upvotes: 1

Related Questions