如何正确发送序列化数据到PHP [英] How to correctly send serialized data to php
问题描述
所以我正在开发一个基于php / jquery的聊天。大多数功能都有效,但是当我尝试提交消息时,jQuery似乎没有与PHP正确连接,没有任何反应。当我从图片中删除jQuery时,PHP脚本可以自行运行,所以我确定这是jQuery搞砸了。
我将一个值硬编码到 data:
并且消息显示正常(每条消息都是该值,如预期的那样)。这是我用
So I am working on a php/jquery powered chat. Most of the features work, but when I try to submit a message, the jQuery doesn't seem to properly connect with the PHP and nothing happens. The PHP script works fine on it's own when I removed jQuery from the picture, so I'm certain it's the jQuery that's messing things up.
I hardcoded a value into data:
and the messages would appear fine (with every message being that value, as expected). It's when I replace it with
$("#cbox_input").serialize(),
事情已经停止发布。
以下是聊天中使用的代码。
that things stopped posting.
Here is the code used in the chat.
<div id="cbox" align="left">
<script>
$(document).ready(function() {
setInterval(function(){getUpdates()}, 2000);
function getUpdates() {
$("#cbox").load("/lib/chat_post.php");
}
$("#submitmsg").click(function (event) {
event.preventDefault();
$.ajax({
type: 'POST',
url: '/lib/chat_new.php',
data: $("#cbox_input").serialize(),
success: function(){getUpdates()}
});
});
});
</script>
</div>
<form name="cbox_input" method='post' id="cbox_input">
<input name="usermsg" id='usermsg' type="text" size="63" maxlength="255" />
<input name="submitmsg" id='submitmsg' type="submit" />
</form>
这是 chat_new.php
以防万一它就是那里的东西。在一件事情上。
And here is chat_new.php
in case it is something there that is throwing a wrench in things.
ob_start();
session_start();
require "/home3/himowa/public_html/life/inc/functions.php";
$username = $_SESSION['login'];
$ug = $_SESSION['user_group'];
if ($_POST['submitmsg']){
if ($username) {
$text = $_POST['usermsg'];
$text = stripslashes($text);
$text = mysqli_real_escape_string($db,$text);
if ($text) {
$sql = <<<SQL
INSERT INTO chat_entries(author,text)
values('$username','$text')
SQL;
$result = $db->query($sql);
}
}
if (!$username) {
$ip = $_SERVER['REMOTE_ADDR'];
$sqlm = <<<SQL
SELECT *
FROM `chat_guests`
WHERE ip = '$ip'
SQL;
$resultm = $db->query($sqlm);
$countm = $resultm->num_rows;
if($countm == 0) {
$sqlg = <<<SQL
INSERT INTO chat_guests(ip)
values('$ip')
SQL;
$resultg = $db->query($sqlg);
}
$resultm = $db->query($sqlm);
$countm = $resultm->num_rows;
if($countm == 1) {
while ($rowm = mysqli_fetch_array($resultm)) {
$gid = $rowm['id'];
}
$username = "Guest" . $gid;
$text = $_POST['usermsg'];
$text = stripslashes($text);
$text = mysqli_real_escape_string($db,$text);
if ($text) {
$sql = <<<SQL
INSERT INTO chat_entries(author,text)
values('$username','$text')
SQL;
$result = $db->query($sql);
}
}
}
}
推荐答案
( #cbox_input)。序列化(),
("#cbox_input").serialize(),
事情已停止发布。
这是聊天中使用的代码。
that things stopped posting.
Here is the code used in the chat.
<div id="cbox" align="left">
<script>
( document )。ready( function (){
setInterval( function (){getUpdates()}, 2000 );
function getUpdates(){
(document).ready(function() { setInterval(function(){getUpdates()}, 2000); function getUpdates() {
( #cbox)。load( / lib / chat_post.php);
}
("#cbox").load("/lib/chat_post.php"); }
这篇关于如何正确发送序列化数据到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!