hi
i am very new to ajax .when a user post comment i want to diaplay immediately ie something like dani web quick reply please tell how do tat in ajax

i have same question, i am creating ajax based comment posting script, i did Adding script to database section, but i am unable to show comments immediately after adding, also i cant call the javascript function on the page, the page already called by ajax . what should i do? please tell me, i have seen w3schools but there is no such information. help needed plz.

Hi,

You will need to call two functions in AJAX.
1. for inserting the comment in database and returning the last inserted id
2. For displaying the comment using the returned id.

but i am unable to call the function in a page which is returned by responseText.

start very simple. On the page that you are calling, just echo "hello world" and start there. If that doesn't work then post your ajax function(s). Otherwise, post the code on the page that you are calling and you ajax function(s).

see what i am doing:

first i call the comment page where comment form is designed, then on submit button another page will be called this is submit_comment.php, then on submit_comment, i want to load another function .. see the example i am doing here:

if($result = mysql_query( "insert into comments
set comment = '$newcomment',
gameid = '$id',
userid = '$userid',
ipaddress = '$ipaddress',
date = '$todaysdate'"))
{
echo "Comment Added Successfuly:)";


?>
<br />
<script type="text/javascript">


show_comments();



</script>

right, exactly. You have a combination insert/update statement there. You need to go with one or the other. But we'll come back to that, for now comment everything out and echo "hello world", to verify that your javascript ajax functions are working properly.

but javascript is not working in that page even alert() is also not working ...

      if($result = mysql_query( "insert into comments
                        set comment = '$newcomment',
                        gameid = '$id',
                        userid = '$userid',
                        ipaddress = '$ipaddress',
                        date = '$todaysdate'"))
                        {
                        echo "Comment Added Successfuly:)";



                        ?>
                        <br /> 
                        <script type="text/javascript">
alert("Adnan");
</script>

javascript will not work in that page. It's just text that you are receiving from an ajax call. You can get the output from the ajax call, then paste it into your document including the javascript inside of a function, must be in a function, and then call that function, but javascript has already ran on the page along with any onload events, you need an event to trigger a function if you want to run javascript after the page has already loaded, ajax doesn't change that.

I have solved my problem myself ;) ....

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.