I'm having problem in getting the response in a div of an ajax request which is made by onclick event by a form & a php page.
The code:
<html>
<head>
<script language="javascript">
function commentRequest(counter) {
new Ajax.Request("comment.php",
{
method: 'post',
postBody: 'commentbox='+ $F('commentbox_'+counter)+'&idc2='+$F('idc2_'+counter),
onComplete: showResponse2
});
}
function showResponse2(counter)
{
document.getElementById('showcomments_'+counter).innerHTML= counter.responseText;
}
</script>
</head>
<body>
<form id="form1" method="post" action="insert.php">
<textarea name="text1" id="text1"></textarea>
<input type="submit" name="nbbutton" value="Submit"/>
</form>
<?p开发者_运维技巧hp
$con = mysql_connect("localhost","myuser","Muddser@1");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
MySQL_select_db("mydb", $con);
$result = mysql_query("SELECT * FROM table1 ORDER BY sl_no desc");
while($row = mysql_fetch_array($result))
{
echo "$row['text1']";
}
$id=$row['sl_no'];
$queryout="SELECT * FROM comments WHERE idc='$idc'";
$resultout=mysql_query($queryout) or die(mysql_error());
while($row1 = mysql_fetch_array($resultout))
echo "<div id='showcomments_$idc'>";
echo "<div id='comment'><form name='comment' id='commentfrm'onSubmit='return false;'>";
echo "<input type='text' value='Post a Comment' name='commentbox' id='commentbox_$idc'
tabindex='30' size='56'>";
echo "<input type='hidden' name='idc2' value='$idc' id='idc2_$idc'>";
echo " <input type='submit' name='submit' value='Post' id='commentbtn'
tabindex='40' onClick='commentRequest($idc)'>";
echo "</form></div>";
echo "$row1['comment']";
echo "</div>";
</body>
</html>
In the above thing, I submit some value of a textfield in a table & I get the output on the same page as the form is on.
in these outputs, I've made a provision of commenting on each outputs.
to comment I've created a form which calls an ajax function commentRequest($idc) as u can see it in the head tag.
after commeting by ajax, I wanna show the response of the form submission in tht very page in the div named showcomments_$idc.
Here, though I'm able to submit the value bt I'm totally unable to show the response in the desired div...
Plz help me if ne1 can...
I need it so badly as I've been stuck in between of my project...
Thanks in advance...
Regards, Muddser
In your showResponse2()
function you are treating counter
as both string (when searching for element id) and object containing AJAX response (when assigning respose text). Change your function commentRequest
to:
function commentRequest(counter) {
new Ajax.Request("comment.php",
{
method: 'post',
postBody: 'commentbox='+ $F('commentbox_'+counter)+'&idc2='+$F('idc2_'+counter),
onComplete: function(response) {
document.getElementById('showcomments_'+counter).innerHTML= response.responseText;
}
});
}
精彩评论