我正在使用jQuery和$ .post()。聊天.php的代码片段如下:
<?php
$msg=$_POST['msg'];
mysql_connect("localhost","root");
mysql_select_db("user");
mysql_query("INSERT INTO space (name,msg,serial) VALUES('Test','$msg','1')");
?>
这是我的HTML文件的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Shout!</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
var status=1;
function action() {
if(status==1) {
$("#Layer1").hide("slow");
$("#Layer3").hide("fast");
$("#Layer4").hide("slow");
$("#close").attr("src","open.jpg");
status=0;
}
else if(status==0) {
status=1;
$("#Layer1").show("slow");
$("#Layer3").show("fast");
$("#Layer4").show("slow");
$("#close").attr("src","close.jpg");
}
}
function sendline() {
var msg=$("#msg").val();
$.post("chat.php",{msg:msg});
$("#msg").val(" ");
}
function typeyo() {
var text=$("#msg").val();
$("#Layer6").html(text);
}
</script>
<style type="text/css">
<!--
body {
background-color: #000000;
}
#Layer1 {
position:absolute;
width:200px;
height:115px;
z-index:1;
left: 199px;
top: 3px;
}
#Layer2 {
position:absolute;
width:69px;
height:64px;
z-index:2;
left: 570px;
top: 543px;
}
#Layer3 {
position:absolute;
width:131px;
height:91px;
z-index:3;
left: 487px;
top: 327px;
}
.style1 {
color: #FFFFFF;
font-family: "Segoe UI";
font-weight: bold;
}
#Layer4 {
position:absolute;
width:99px;
height:38px;
z-index:4;
left: 744px;
top: 485px;
}
#Layer5 {
position:absolute;
width:274px;
height:70px;
z-index:5;
left: 422px;
top: 62px;
}
#Layer6 {
width:638px;
height:356px;
z-index:5;
left: 352px;
top: 105px;
}
-->
</style></head>
<body>
<div class="style1" id="Layer3">
<textarea name="textarea" cols="30" rows="5" id="msg" ></textarea>
</div>
<div id="Layer1">Hello World!<img src="body.jpg" width="842" height="559" /></div>
<div id="Layer2"><img src="close.jpg" alt="Go Online/Offline" name="close" width="63" height="64" id="close" OnClick="action()"/></div>
<div id="Layer4">
<input type="button" value="Send Line" onclick="sendline()" /></div>
<div id="Layer6" style="color:white;font-family:Segoe UI;font-size:16px;width:500px; height:400px; overflow:auto;"></div>
</body>
</html>
现在,在我提供的HTML代码上使用chat.php将$ .post()发布变量msg.Im似乎存在一些问题。
这里发送“msg”似乎有问题。 chat.php文件很好,因为如果我们直接运行它,而不是通过$ .post()调用它完美地工作
请帮忙!谢谢!
答案 0 :(得分:3)
您尝试通过POST发送的msg变量未初始化,
在sendline
函数的开头添加以下行:
var msg = $("#msg").val();
注意:在没有事先处理的情况下,在MySQL查询中插入POST变量会有很大/很大的安全问题。
答案 1 :(得分:0)
更新:我建议您使用javascript调试器,在function sendline()
的开头设置断点并逐步执行代码。使用哪一个取决于您的浏览器
Firefox - &gt;例如Firebug
IE7 - &gt;例如IE Developer Toolbar
IE8 + - &gt;只需按F12
即可打开IE随附的开发者工具。
除了darma's answer之外:你的php脚本容易出现sql注入(故意/恶意以及无意的注入)。正确使用prepared, parametrized statements或escape the data。
工作示例:
的test.html:
<html>
<head><title>test.html</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function sendline() {
var msg = $('#msg').val();
// --- add some tests on msg here, e.g. msg=="" ---
$.post(
"chat.php",
{'msg':msg},
function(data, textStatus, req) { $('#reply').text('reply: ' + data); }
);
}
</script>
</head>
<body>
<div>
<textarea id="msg" rows="5" cols="30" name="textarea"></textarea>
<button onclick="sendline()">send line</button>
</div>
<div id="reply"> </div>
</body>
</html>
chat.php:
<?php
// --- add some tests on $_POST['msg'] here
// e.g. isset($_POST['msg']) and 0<strlen(trim($_POST['msg'])) ---
// you might want to use a slightly more sophisticated error handling than "or die(mysql_error())" ...but this is only an example.
$mysql = mysql_connect("localhost","localonly", "localonly") or die(mysql_error());
mysql_select_db("test", $mysql) or die(mysql_error());
$msg=mysql_real_escape_string($_POST['msg'], $mysql);
$sql = "INSERT INTO space (name,msg,serial) VALUES('Test','$msg','1')";
// mysql_query($sql, $mysql) or die(mysql_error());
echo htmlspecialchars($sql);
update2:您的php脚本中仍然没有任何错误处理。任何mysql_ *函数都可能由于各种原因而失败;测试结果。您需要“看到”这些错误,例如通过将它们写入日志文件或其他东西......
尝试
<?php
define('LOGERRORS', 1);
function dbgLog($text) {
if (LOGERRORS) {
error_log(date('Y-m-d H:i:s : ').$text."\n", 3, 'error.log');
}
}
if ( !isset($_POST['msg']) ) {
dbgLog('script called without post parameter "msg"');
die();
}
$mysql = mysql_connect("localhost","root");
if ( !$mysql ) {
dbgLog('database connection failed: '.mysql_error());
die();
}
$result = mysql_select_db("user", $mysql);
if ( !$result ) {
dbgLog('database selection failed: '.mysql_error($mysql));
die();
}
$msg=mysql_real_escape_string($_POST['msg'], $mysql);
$sql = "INSERT INTO space (name,msg,serial) VALUES('Test','$msg','1')";
dbgLog('sending query: '.$sql);
$result = mysql_query($sql, $mysql);
if ( !$result ) {
dbgLog('query failed: '.mysql_error($mysql));
die();
}