0

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);

?>
2
  • Check your in developer console while sending data. Commented May 12, 2017 at 6:40
  • this is because ajaxsql.php is returning an object not a string .. what you can do is use .each Commented May 12, 2017 at 6:50

1 Answer 1

1

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);
     });
},
Sign up to request clarification or add additional context in comments.

2 Comments

why response.data ?
because we used response as the response variable of ajaxsql.php .. you can use data but as you can see , its not good to read data.data ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.