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

时间:2022-09-26 08:47:13

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不太熟悉,所以请小心点!警:我会把我有的文件给你看。这是一个味精。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);
?>

4 个解决方案

#1


1  

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

#2


2  

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

#3


0  

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.

然后在

#4


0  

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>

#1


1  

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

#2


2  

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

#3


0  

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.

然后在

#4


0  

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>