无法获取 AJAX 表单来提交 MySQL 数据
Can't get AJAX form to commit MySQL data
我有两个文件:
<?php
include('db.php');
?>
<!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>Generic Title</title>
<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(".edit_tr").click(function()
{
var ID=$(this).attr('id');
$("#time_"+ID).hide();
$("#character_"+ID).hide();
$("#message_"+ID).hide();
$("#status_"+ID).hide();
$("#time_input_"+ID).show();
$("#character_input_"+ID).show();
$("#message_input_"+ID).show();
$("#status_input_"+ID).show();
}).change(function()
{
var ID=$(this).attr('id');
var time=$("#time_input_"+ID).val();
var character=$("#character_input_"+ID).val();
var message=$("#message_input_"+ID).val();
var status=$("#status_input_"+ID).val();
var dataString = 'id='+ ID +'&time='+ time + '&character='+ character + '&message='+ message+ '&status='+ status;
$("#character_"+ID).html('<img src="load.gif" />');
if(time.length>0 && message.length>0 && character.length>0 && status.length>0)
{
$.ajax({
type: "POST",
url: "table_edit_ajax.php",
data: dataString,
cache: false,
success: function(html)
{
$("#time_"+ID).html(time);
$("#character_"+ID).html(character);
$("#message_"+ID).html(message);
$("#status_"+ID).html(status);
}
});
}
else
{
alert('Enter something.');
}
});
$(".editbox").mouseup(function()
{
return false
});
$(document).mouseup(function()
{
$(".editbox").hide();
$(".text").show();
});
});
</script>
<style>
body
{
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}
.editbox
{
display:none
}
td
{
padding:7px;
}
.editbox
{
font-size:14px;
width:135px;
background-color:#ffffcc;
border:solid 1px #000;
padding:4px;
}
.edit_tr:hover
{
background:url(edit.png) right no-repeat #80C8E5;
cursor:pointer;
}
th
{
font-weight:bold;
text-align:left;
padding:4px;
}
.head
{
background-color:#333;
color:#FFFFFF
}
</style>
</head>
<body bgcolor="#dedede">
<div style="margin:0 auto; width:750px; padding:10px; background-color:#fff; height:800px;">
<table width="100%">
<tr class="head">
<th>Time</th><th>Character</th><th>Message</th><th>Status</th>
</tr>
<?php
$sql=mysql_query("select * from script");
$i=1;
while($row=mysql_fetch_array($sql))
{
$id=$row['id'];
$character=$row['character'];
$time=$row['time'];
$message=$row['message'];
$status=$row['status'];
if($i%2)
{
?>
<tr id="<?php echo $id; ?>" class="edit_tr">
<?php } else { ?>
<tr id="<?php echo $id; ?>" bgcolor="#f2f2f2" class="edit_tr">
<?php } ?>
<td width="10%" class="edit_td">
<span id="time_<?php echo $id; ?>" class="text"><?php echo $time; ?></span>
<input type="text" value="<?php echo $time; ?>" class="editbox" id="time_input_<?php echo $id; ?>" />
</td>
<td width="25%" class="edit_td">
<span id="character_<?php echo $id; ?>" class="text"><?php echo $character; ?></span>
<input type="text" value="<?php echo $character; ?>" class="editbox" id="character_input_<?php echo $id; ?>"/>
</td>
<td width="55%" class="edit_td">
<span id="message_<?php echo $id; ?>" class="text"><?php echo $message; ?></span>
<input type="text" value="<?php echo $message; ?>" class="editbox" id="message_input_<?php echo $id; ?>"/>
</td>
<td width="10%" class="edit_td">
<span id="status_<?php echo $id; ?>" class="text"><?php echo $status; ?></span>
<input type="text" value="<?php echo $status; ?>" class="editbox" id="status_input_<?php echo $id; ?>"/>
</td>
</tr>
<?php
$i++;
}
?>
</table>
</div>
</body>
</html>
和
<?php
include("db.php");
if($_POST['id'])
{
$id=mysql_escape_String($_POST['id']);
$time=mysql_escape_String($_POST['time']);
$character=mysql_escape_String($_POST['character']);
$message=mysql_escape_String($_POST['message']);
$status=mysql_escape_String($_POST['status']);
$sql = "update script set time='$time',character='$character',message='$message',status='$status' where id='$id' ";
mysql_query($sql)
or die("display_db_query:" . mysql_error());
}
?>
它们似乎没有抛出错误,但编辑也不会提交到数据库。我哪里出错了?
db.php 包含具有所有正确"东西"的连接脚本(显示所有正确的字段,UPDATE 似乎不想正确播放)
显示它应该做什么(大部分代码都是借用的)的工作演示是 http://demos.9lessons.info/table_edit/TableEdit.htm
提前感谢您对此的任何帮助,我是一个被难倒的菜鸟。
字符是MySQL中的保留字。如果你想使用它,你需要用反引号转义它
UPDATE script SET time='$time', `character` = '$character' ...
相关文章:
- 无法获取 AJAX 表单来提交 MySQL 数据
- 提交按钮,调用js函数,然后发布到mysql
- 将Jquerys的日期选择器提交到mysql时遇到问题
- 在不提交表单的情况下,在两个texbox中比较mysql数据库中的值
- Php:如何在不提交表单的情况下从mysql数据库中获取文本框值
- PHP/MySQL-数据库数据的复选框,提交2个参数
- 预装内容;如果连接正常,则不需要访问Internet即可显示onclick+mysql提交的新内容
- 如何提交表格,并在每个不同的'选择mysql-query'并将答案插入mysql数据库
- 提交按钮未将表单数据发送到 mysql - jquery 问题
- 这里我有一个HTML表单和PHP MySQL表,在表中输入值并按下提交按钮后,不会出现任何输出
- 如何添加图像到mysql数据库,当用户提交他们的图像
- 我如何提交一个没有提交按钮的“文件”输入,然后在同一脚本中做mysql更新
- 如何通过使用PHP,Angular.js和MySQL的单个用户提交在多行插入值
- 如何根据数据库下拉列表的mysql计数限制或停止表单提交?
- 如何提交mysql查询时,点击链接,使用onclick
- Textarea不能用wysiwyg编辑器提交到MySQL
- 单选按钮值mysql没有提交
- 地理编码不工作在php mysql提交表单
- 如何检查在PHP文件中使用MySQL提交成功的数据
- 仅当提交的分数高于现有分数时,才更新mysql数据库的分数