I'm trying to submit form data without refreshing the page using jQuery. I've added all the following code in correct places but the page refreshes and nothing happened, neither it echo's the success data nor it inserts any data into sql db. Here is what I'm trying to do;
HTML:
$profile_comments = '
<form action="#" method="post">
<table border="0" cellspacing="'.$theme['borderwidth'].'" cellpadding="'.$theme['tablespace'].'" class="sideboxes_tborder">
<tr>
<td class="sideboxes_thead">Post Comments</td>
</tr>
<tr>
<td class="sideboxes_trow">
<input type="text" class="textbox_comment" name="message" id="pc_message" tabindex="1" autocomplete="off" />
</td>
</tr>
<tr>
<td class="sideboxes_trow" align="left">
<input type="hidden" name="uid" value="'.$memprofile['uid'].'" />
<input type="hidden" name="from_uid" value="'.$mybb->user['uid'].'" />
<input type="submit" class="button" id="comment_submit" name="submit" value="Post Comment" tabindex="2">
</td>
</tr>
<tr>
<td class="sideboxes_trow"><div id="show_profile_comments" style="overflow: auto; max-height: 313px;"></div></td>
</tr>
</table>
</form>';
jQuery:
jQuery.noConflict();
jQuery(document).ready(function($)
{
$("#comment_submit").click(function()
{
var message = $( "#pc_message" ).val(),
if (message == '')
{
alert( "Message is missing!!" );
return;
}
$.ajax(
{
type : "post",
dataType: "html",
url : "pro_profile.php?action=do_comment",
data : "message=" + message,
success : function(response)
{
$('#show_profile_comments').html(response);
}
document.getElementById('pc_message').value = '';
document.getElementById('pc_message').focus();
if (response.error)
{
alert(response.error);
}
});
});
});
PHP:
if ($_POST['action'] == "do_comment")
{
$uid = intval($mybb->input['uid']);
$insert_array = array(
"uid" => $uid,
"from_uid" => intval($mybb->input['from_uid']),
"approved" => '1',
"message" => $db->escape_string($mybb->input['message']),
"dateline" => TIME_NOW
);
$db->insert_query("pp_comments", $insert_array);
$query = $db->simple_select("pp_comments", "*", "uid='{$uid}'");
$c = $db->fetch_array($query);
echo $c['message'];
}
Please help!