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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21200520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:54:12  来源:igfitidea点击:

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

phpjqueryajax

提问by mishraoft

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. I'm not very experienced with AJAX AND jQuery so be gentle! :P I 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

我正在努力让它发挥作用,我不知道我做错了什么。我有一个注册页面,我想将数据插入表单并使用 jQuery 和 AJAX 将其插入到数据库中。我对 AJAX 和 jQuery 不是很有经验,所以要温柔!: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);
?>

回答by Karthick Kumar

       $.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方法获取数据,它将起作用

回答by Susheel Singh

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

回答by Super Model

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>

回答by Sp0T

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.

然后包括您的function sendChat()内部<script>标签。