简体   繁体   English

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

[英]Modal Pop Up not saving to db when button inside is clicked

I am new with jquery and ajax, so please be patient. 我是jquery和ajax的新手,所以请耐心等待。 I have this link: 我有这个链接:

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

that shows this pop up when clicked: 单击时显示此弹出窗口:

  <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>

when I entered text in the textarea and show its content by alert(msgContent) in the script below, it shows 当我在文本区域中输入文本并在下面的脚本中通过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');   
                        } 
                });


        })
    })

but when I try to pass it to a php page through ajax, it won't pass. 但是,当我尝试通过ajax将其传递到php页面时,它不会传递。 What could be wrong? 有什么事吗

this is message.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));
    }

You need to read the value from $_GET : 您需要从$_GET读取值:

$message = $_GET['message'];

Or use the post method, with data attribute: 或使用具有data属性的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');   
                        } 
                });


        })
    })

Your JS should be this: 您的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');  
            } 
        });
    });
});

And your PHP this: 而你的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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM