我正在尝试使用 PHP 和 MySQL 创建一个动态 AJAX 函数,但到目前为止收效甚微。其目的是更新数据库中的记录而不刷新页面或更改到另一个页面。
在带有表单的页面上,我有以下代码:
// jQuery
<script type="text/javascript">
<?php
$sql = "SELECT * from pm_schedule";
$result = $pdo->query($sql);
foreach ($result as $row)
{
echo
"$(document).ready(function() {
$('#updatebtn".$row['id']."').click(function() {
$('#result".$row['id']."').show('slow').delay(4000).hide('slow')
$.post('process.php', $('#updateform".$row['id']."').serialize())
});
return false;
});";
}
?>
</script>
// form
$sql = "SELECT * from pm_schedule";
$result = $pdo->query($sql);
foreach ($result as $row)
{
echo
'<form id="updateform'.$row['id'].'">
<div class="tbl_header">'.$row['task'].'</div>
Due Date:
<script>
$(function() {
$( "#datepicker'.$row['id'].'" ).datepicker({ minDate: -0,
dateFormat: \'dd/mm/yy\', maxDate: new Date(2013, 1,22) })
});
</script>
<input type="text" id="datepicker'.$row['id'].'" style="width: 100px; height: 10px;" value="'.$row['duedate'].'" name="duedate"/>
Status:
<select style="width: 125px;" name="status">
<option>'.$row['status'].'</option>
<option>----</option>
<option>Pending</option>
<option>In Progress</option>
<option>Complete</option>
</select>
<input type="hidden" name="id" value="'.$row['id'].'">
<input type="submit" id="updatebtn'.$row['id'].'" value="Update"
style="width: 100px;"/>
</form>
<div id="result'.$row['id'].'" style="display: none; color: red">
Update successful!
</div>
<p>';}
在负责处理的页面(process.php)上,我有以下代码:
<?php
$name = mysql_real_escape_string($_POST["name"]);
$status = mysql_real_escape_string($_POST["status"]);
$id = mysql_real_escape_string($_POST["id"]);
$sql = "UPDATE pm_schedule SET name=?, status=?, id=? WHERE id=?";
$q = $conn->prepare($sql);
$q->execute(array($name,$status,$id));
?>
我究竟做错了什么?