单击按钮内部时,模态弹出不保存到数据库

时间:2014-01-16 16:16:17

标签: javascript php jquery ajax popup

我是jquery和ajax的新手,所以请耐心等待。 我有这个链接:

   <a href="#message" style="text-decoration:none" class="common2 simple3" >Message</a>

单击时显示弹出窗口:

  <div id="message" class="modalDialog">
       <div>        
            <h3>Create A Message</h3>
            <form id="msgForm" name="msgForm" action="#" method="post">
                <textarea  id = 'msgContent' cols="48" rows="10" ></textarea>
                    <br>
                    <div id="create_btn">
                        <a href='' id = 'send' class="common simple2" style='margin-left:50px;text-decoration: none;'>Send</a>
                    </div>
                    <div id="cancel_btn">
                        <a href="#close" class="common simple2" style='margin-left:40px;text-decoration: none;'>cancel</a>
                    </div>
             </form>
        </div>
   </div>

当我在textarea中输入文本并通过下面的脚本中的alert(msgContent)显示其内容时,它显示

  $(document).ready(function()
    {
        $("#send").click(function(e) 
        {   
            e.preventDefault();
            var msgContent = $("#msgContent").val();
            alert(msgContent);
                $.ajax({
                url: 'message.php?message='+ msgContent,
                type: 'GET',
                dataType: 'json',
                context: this,
                success: function(result)
                        {                                           

                            //if (result == true)
                            $(this).html('Send');   
                        } 
                });


        })
    })

但是当我尝试通过ajax将其传递到php页面时,它将无法通过。可能有什么不对?

这是message.php

   $message = $_POST['message'];
   $result = false;
   $sql="INSERT INTO MESSAGE_LOG (sender,recepient, message)
          VALUES($viewer,$viewed,$message)";
    if (!mysqli_query($connection,$sql))
    {
        die('Error: ' . mysqli_error($connection));
    }

2 个答案:

答案 0 :(得分:1)

您需要阅读$_GET

中的值
$message = $_GET['message'];

或者使用post方法,数据属性为:

 $(document).ready(function()
    {
        $("#send").click(function(e) 
        {   
            e.preventDefault();
            var msgContent = $("#msgContent").val();
            alert(msgContent);
                $.ajax({
                url: 'subscribe.php',
                type: 'POST',
                data: {message: msgContent},
                //dataType: 'json', from your php I don't that that you are looking for json response...
                context: this,
                success: function(result)
                        {                                           

                            //if (result == true)
                            $(this).html('Send');   
                        } 
                });


        })
    })

答案 1 :(得分:0)

你的JS应该是这样的:

$(document).ready(function() {
    $("#send").click(function(e)  {   
        e.preventDefault();
        var msgContent = $("#msgContent").val();
        $.ajax({
            url: 'message.php',
            type: 'POST',
            dataType: 'json',
            data: {message: msgContent},
            context: this,
            success: function(result) {                                           
                alert('Message has been sent');  
            } 
        });
    });
});

你的PHP:

$message = $_POST['message'];
$result = false;
$sql="INSERT INTO MESSAGE_LOG (sender,recepient, message)
      VALUES($viewer,$viewed,'$message')";
if (!mysqli_query($connection,$sql)) {
    die('Error: ' . mysqli_error($connection));
}