我正在尝试使用 jQuery 提交表单数据而不刷新页面。我已将以下所有代码添加到正确的位置,但页面刷新后没有任何反应,既不回显成功数据,也不将任何数据插入 sql 数据库。这就是我正在尝试做的事情;
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'];
}
请帮忙!
最佳答案
如果这应该成功
document.getElementById('pc_message').value = '';
document.getElementById('pc_message').focus();
将你的 JS 代码更改为此
jQuery.noConflict();
jQuery(document).ready(function($)
{
$("#comment_submit").on('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();
},
error : function(response)
{
alert(response.responseText);
}
});
return false;
});
});
关于php - 使用 jQuery 提交和显示数据 - 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19606380/