如何使用php,jquery和ajax在数据库中插入值 [英] how to insert value in database using php, jquery and ajax

查看:68
本文介绍了如何使用php,jquery和ajax在数据库中插入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使它正常工作,我不知道自己在做错什么.我有一个注册页面,我想将数据插入表单中,然后使用jQuery和AJAX将其插入数据库.我对AJAX和jQuery的经验不是很丰富,所以要保持温柔! :P我将向您展示我拥有的文件...当我有提交数据时,这是一个msg.php页面,有时会在数据库中发布提交` 大多数情况下不是,所以我想知道为什么会发生这种情况,所以我在这个领域是新手

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

 <?
    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文件

msg_save.php file

<?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);
?>

推荐答案

如果您尝试使用聊天应用程序,请检查它是否旧,但这只是出于想法:

http://www.codeproject.com/Articles/649771/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"
?>

这篇关于如何使用php,jquery和ajax在数据库中插入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆