I'm creating a chat system using Codeigniter and I need to update a textarea with a field in the database each second.
For that I'm trying to use jquery for the timer but I don't know how I can update the textarea with data from the database, with a call from the jquery function.

Can you please help?

My thanks in advanced

JQUERY

$.getJSON('chatmessage.php', function(data)
{
    var elm = '<p>From' + data.from + 'Message ' + data.message + ' </p>';
    $('#chatmessagebox').append(elm);
}


and chatmessage.php


connect to the database and get the message
store message in $content and echo it out.

$content = array(
    'from'=>'someone',
    'message'=>'this is a message'
);
echo json_encode($content);

this is for single messages




if you are getting multiple messages from the database then you will need something like

JQUERY

$.getJSON('chatmessage.php.php', function(data)
{   
    $.each(data, function(i,data)
    {
        var elm = '<p>From' + data.from + 'Message ' + data.message + ' </p>';
        $('#chatmessagebox').append(elm);
    }
});

chatmessage.php

    foreach(record from the database)
    {
        $content = array(
            'from'=>'someone',
            'message'=>'this is a message'
        );
        $anarray[$i] = $content;
        $i++;
    }
    echo json_encode($anarray);
}

this is just the bare bones but hopefully it will point you in the right direction. There maybe other ways of doing this but this is the way I do it at the moment.

Thanks a lot for your answer.
I've been trying to implement this solution in my code but it's not working. I must be doing something wrong. Can you please help?

The jquery section:

<script>
$(document).ready(function () {
function myDate(){  

$.ajax({
        url: "valueschat.php",
        type: "POST",
        data: "key=value",
        dataType: "json",
        success: function (data) {
            alert("SUCCESS:");
            for(var key in data) {
                $('#tarChat').append(key);
                $('#tarChat').append('=' + data[key] + '<br />');
            }
        },
        });

} 

myDate();

setInterval(function(){   myDate();}, 1000);
});
</script>

valueschat.php:

<?php
define('DB_SERVER', 'blablabla');
define('DB_USER', 'blablabla');
define('DB_PASSWORD', 'blablabla');
define('DB_NAME', 'blablabla');


if (isset($_GET['term'])){
    $return_arr = array();

try {
    $conn = new PDO("mysql:host=".DB_SERVER.";port=8889;dbname=".DB_NAME, DB_USER, DB_PASSWORD);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $conn->prepare('SELECT writtentext from mainchat');

$stmt->execute(array('term' => '%'));

while($row = $stmt->fetch()) {
    $return_arr[] =  $row['writtentext'];
}

} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}



echo json_encode($return_arr);
}

Ok, the problem is solved. Both the jquery section and the 'valueschat.php' were wrong.

$stmt = $conn->prepare('SELECT writtentext from mainchat');

was replaced with

$stmt = $conn->query('SELECT writtentext from mainchat');

and

success: function (data) {
            alert("SUCCESS:");
            for(var key in data) {
                $('#tarChat').append(key);
                $('#tarChat').append('=' + data[key] + '<br />');
            }
        },
        });

was replaced with

$('#tarChat').val(data);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.