php - 使用 jQuery 提交和显示数据 - 问题

标签 php jquery html mysql

我正在尝试使用 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/

相关文章:

python - Scrapy/XPath 从不同的标签中提取文本 (<p> OR <li>)

javascript - 页脚高度根据网址?

php - 执行 Mysql 查询时网站无法加载

jquery - CSS - 让一个 div 填充剩余的垂直空间

javascript - 通过选择的 Jquery 更新选择

javascript - jQuery:自动完成,单击按钮时不关闭

php - MYSQL语法错误

php - xampp 数据库备份与 'data' 文件夹

php - Paypal 直接支付API

javascript - JS增加/减少多个输入数字变量