简体   繁体   English

使用AJAX / JSON / Jquery在数据库中插入数据

[英]Inserting a data on a database using AJAX/JSON/Jquery

I'm trying to create a small chat application but for the sake of minifying the bytes being transferred is there any other way on writing this javascript that is less heavy than this code? 我正在尝试创建一个小型聊天应用程序,但是为了减少所传输的字节数,编写此JavaScript的代码是否比此代码轻?

Here is my javascript: 这是我的JavaScript:

function sendChatText() {

                if (sendReq.readyState == 4 || sendReq.readyState == 0) {
                    sendReq.open("POST", 'includes/getChat.php?last=' + lastMessage, true);
                    sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                    sendReq.onreadystatechange = AjaxRetrieve();  
                    var param = 'message=' + document.getElementById('txtA').value;
                    param += '&name='+user;
                    param += '&uid='+uid;
                    param += '&rid='+document.getElementById('trg').value;
                    sendReq.send(param);
                    document.getElementById('txtA').value = '';
                }                           
            }

Can this also be done on a JSON format too? 也可以使用JSON格式吗? because I think some says that json is lighter.. not sure though 因为我认为有人说json更轻..虽然不确定

here is my php code 这是我的PHP代码

$con = new PDO("mysql:host=". db_host .";dbname=chat_db", db_username , db_password);
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT * FROM users WHERE id = :rid LIMIT 1";
$stmt=$con->prepare($sql);
$stmt->bindValue( 'rid',$_POST['rid'], PDO::PARAM_STR);
$stmt->execute();
    foreach($stmt->fetchAll()as $res)
        {
            $usern = $res['username'];
            $user_lvl = $res['ulvl'];
        }
$text=$_POST['message'];
$sql4 = "INSERT INTO $tblname2(msgid,username,message_content,message_time,recipient)VALUES(:aid,:a,:b,NOW(),:c) ";
                    $stmt5 = $con2->prepare($sql4);
                    $stmt5->bindParam(':aid',$tblpre,PDO::PARAM_STR);
                    $stmt5->bindParam(':a',$_POST['name'],PDO::PARAM_STR);
                    $stmt5->bindParam(':b',$text,PDO::PARAM_STR);
                    $stmt5->bindParam(':c',$usern,PDO::PARAM_STR);
                    $stmt5->execute();

As user2401175 saies. 如user2401175所说。 Why not use a framework, thats what they are here for. 为什么不使用框架,这就是它们的用途。

jQuery is really simple and easy to understand. jQuery非常简单易懂。

You could try adding this, just before your "" tag. 您可以尝试在“”标签之前添加此标签。

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

Under this include of jQuery, you may now use the jQuery Post method to do your ajax request. 在包括jQuery的情况下,您现在可以使用jQuery Post方法执行ajax请求。

In html Use 在html中使用

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

then Create javascript object like this 然后像这样创建javascript对象

var changePacket = {
date1:value1,
data2:value2
}

And send Ajax request 并发送Ajax请求

$.ajax({
    url: "phpFile.php",
    dataType: 'json',
    type: 'POST',
    data: {json:JSON.stringify(changePacket)},
    success: function(response) {
     alert('hip hip hurray');
    },
    error: function(response) {
     alert('some thing wrong happend');
    }
});

In php 在PHP中

$json = $_POST['json'];
$data = json_decode($json);

now user your variable like this $date->data1 and $date->data2 现在使用您的变量,例如$date->data1$date->data2

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

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