如何使用php,jquery和ajax在数据库中插入值

时间:2014-01-18 05:42:01

标签: php jquery ajax

我很难努力让这个工作,我不知道我做错了什么。我有一个注册页面,我想将数据插入到表单中,并使用jQuery和AJAX将其插入数据库。我对AJAX和jQuery不是很有经验所以要温柔! :P我将向您展示我拥有的文件...这是一个msg.php页面,当我提交数据时有时会在数据库中提交 大多数情况下并非如此,我想知道为什么会发生这种情况我是这个领域的新手

 <?
    php $id=$_GET['id'];
    $id1=$_SESSION['id'];
?>
<form method="post" class="msgfrm" id="msgfrm">
<input type="hidden" value="<?php echo $_GET['id']; ?>" name="rcvrid" id="rcvrid">
<input type="hidden" name="senderid" id="senderid" value="<?php echo $id1;?>" > 
<div class="msgdiv" id="chatbox"></div>
<div class="textdiv">
   <input type="text" name="msg" id="msg" class="textmsg">
   <input type="submit"  value="Send" onClick="sendChat()">
</div>
</form>


function sendChat()
{       
   $.ajax({
           type: "POST",
           url: "msg_save.php",
           data: {  
                    senderid:$('#senderid').val(),
                rcvrid:$('#rcvrid').val(),
                msg: $('#msg').val(),
            },
           dataType: "json",
           success: function(data){
           },
        });
}

msg_save.php文件

<?php
    require_once('include/util.php');
    $rcvrid=$_POST['rcvrid'];
    $senderid=$_POST['senderid'];
    $msg=$_POST['msg'];
    $sql="insert into message(rcvrid,senderid,msg) values($rcvrid,$senderid,'$msg')";
    mysql_query($sql);
?>

4 个答案:

答案 0 :(得分:2)

       $.ajax({
       type: "POST",
       url: "msg_save.php",
       data: " senderid="+$('#senderid').val()+"rcvrid="+$('#rcvrid').val()+"msg="+$('#msg').val(),
       dataType: "json",
       success: function(data){
       },
    });

请尝试此代码并发送数据,并使用php中的post方法获取数据,它将起作用

答案 1 :(得分:1)

如果你正在尝试聊天应用程序检查这个,它已经过时了但只是为了想法:

http://www.codeproject.com/Articles/649771/Chat-Application-in-PHP

使用mysqli_query而不是mysql_query推荐

<?php
$id=$_GET['id'];
//$id1=$_SESSION['id']; COMMENTED THIS AS I AM NOT IN SESSION. HARDCODED IT IN THE FORM AS VALUE 5
?>
<html>
    <head>
        <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
    </head>
    <body>
        <form method="post" class="msgfrm" id="msgfrm">
            <input type="hidden" value="<?php echo $_GET['id']; ?>" name="rcvrid" id="rcvrid">
            <input type="hidden" name="senderid" id="senderid" value="5" > 
            <div class="msgdiv" id="chatbox"></div>
            <div class="textdiv">
                <input type="text" name="msg" id="msg" class="textmsg">
                <input type="submit"  value="Send" >
            </div>
        </form>
        <script>
            $("#msgfrm").on("submit", function(event) {
                event.preventDefault();
                $.ajax({
                    type: "POST",
                    url: "msg_save.php",
                    data: $(this).serialize(),
                    success: function(data) {
                        $("#chatbox").append(data+"<br/>");//instead this line here you can call some function to read database values and display
                    },
                });
            });
        </script>
    </body>
</html>

<强> msg_save.php

<?php

//require_once('include/util.php');
$rcvrid = $_POST['rcvrid'];
$senderid = $_POST['senderid'];
$msg = $_POST['msg'];
echo $rcvrid.$senderid.$msg;
$con = mysqli_connect("localhost", "root", "", "dummy");
// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql = "insert into message(rcvrid,senderid,msg) values($rcvrid,$senderid,'$msg')";
mysqli_query($con,$sql);
mysqli_close($con);
echo "successful"
?>

答案 2 :(得分:0)

检查是否已插入jquery文件。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

然后在function sendChat()代码中添加<script>

答案 3 :(得分:0)

在提交按钮

<button type="submit" id="button">SAVE</button> 
<script>
        $(document).ready(function(){
            $("#button").click(function(){
                var firstname=$("#firstname").val();
                var lastname=$("#lastname").val();
                var email=$("#email").val();

                $.ajax({
                    url:'dbConfigAndInsertionQuery.php',
                    method:'POST',
                    data:{
                        firstname:firstname,
                        lastname:lastname,
                        email:email
                    },
                   success:function(data){
                       alert(data);
                   }
                });


            });
        });
    </script>