简体   繁体   English

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

[英]how to insert value in database using php, jquery and ajax

I am struggling very hard to get this to work and I don't know what I'm doing wrong. 我很难努力让这个工作,我不知道我做错了什么。 I have a register page that I want to take the data inserted into the form and INSERT it to the database with jQuery and AJAX. 我有一个注册页面,我想将数据插入到表单中,并使用jQuery和AJAX将其插入数据库。 I'm not very experienced with AJAX AND jQuery so be gentle! 我对AJAX和jQuery不是很有经验所以要温柔! :PI will show you the files that I have...this is a msg.php page when i have submit data sometimes post submit in database` mostly not so i want to know that why it s happening i am new in this field :PI将向您显示我拥有的文件...这是一个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 file 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);
?>
       $.ajax({
       type: "POST",
       url: "msg_save.php",
       data: " senderid="+$('#senderid').val()+"rcvrid="+$('#rcvrid').val()+"msg="+$('#msg').val(),
       dataType: "json",
       success: function(data){
       },
    });

please try this code and send data ,and use post method in php to get data,it will work 请尝试此代码并发送数据,并使用PHP中的post方法获取数据,它将工作

if you are trying chat application check this, it is old but just for idea: 如果你正在尝试聊天应用程序检查这个,它是旧的,但只是为了想法:

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

use mysqli_query instead of mysql_query recommended 使用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 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"
?>

check that whether you have inserted jquery file or not. 检查是否已插入jquery文件。

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

Then include your function sendChat() inside <script> tags. 然后在<script>标记内包含function sendChat()

On submit button 在提交按钮上

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

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

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