以下代码正常运行,我正在使用它,因为我需要更新数据库中的一些注释而不刷新页面。 但是,当我需要在同一事件中获取一些数据时需要做什么?
index.php 应该发生变更
<?php
$s = $GET['...']
$p = $GET['...']
print '
<form id="notesForm">
<textarea name="c">'.$note.'</textarea>
<input type="text" value="LAST_CHANGE_NEED_GIVE_BACK_FROM_DB">
<input type="hidden" name="s" value="'.$s.'">
<input type="hidden" name="p" value="'.$p.'">
<input type="submit" value="save note">
<div id="response"></div>
</form>
';
?>
脚本,它将数据发布到live_notes.php,可能因目标而被更改
$(document).ready(function(){
$('#notesForm').submit(function(){
event.preventDefault();
$('#response').html("<b>saving...</b>");
$.ajax({
type: 'POST',
url: 'notes/live_notes.php',
data: $(this).serialize()
})
.done(function(data){
$('#response').html(data);
})
.fail(function() {
alert("bad luck");
});
return false;
});
});
备注/ live_notes.php
<?php
$s = $_POST['s'];
$p = $_POST['p'];
$c = $_POST['c'];
// connecting DB
mysql_query("
UPDATE `poznamky`
SET
last_change = now(),
page = '$p',
content = '$c'
WHERE
page = '$p';
");
任何想法?
答案 0 :(得分:1)
<?php
$s = $_POST['s'];
$p = $_POST['p'];
$c = $_POST['c'];
// connecting DB
$blnSuccess = mysql_query("
UPDATE `poznamky`
SET
last_change = now(),
page = '$p',
content = '$c'
WHERE
page = '$p';
");
if($blnSuccess){
echo "success";
}
else {
echo "not success!";
}
在你的jquery中:
.done(function(data){
$('#response').html(data);
}
数据包含success
或not success!
作为ajax响应返回。
<强>更新强>
如果要从数据库返回一些数据,那么:
$result = mysql_query("
select *
from `poznamky`
");
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo $row['content'];
}
然后它会将content
表中的所有poznamky
字段返回到ajax中的数据。
答案 1 :(得分:1)
<?php
$s = $_POST['s'];
$p = $_POST['p'];
$c = $_POST['c'];
// connecting DB
$OK = mysql_query("
UPDATE `poznamky`
SET
last_change = now(),
page = '$p',
content = '$c'
WHERE
page = '$p';
");
$id = mysql_insert_id();
$res = mysql_query("select * from poznamky where id = $id");
$data = mysql_fetch_array($res);
//Print raw or JSON of $data like json_encode($data) etc
答案 2 :(得分:0)
如果你想在note.php中返回一些东西,你可以这样做:
$s = $_POST['s'];
$p = $_POST['p'];
$c = $_POST['c'];
// connecting DB
mysql_query("
UPDATE `poznamky`
SET
last_change = now(),
page = '$p',
content = '$c'
WHERE
page = '$p';
");
$select = "//use your select query and loop here and get all data in this variable";
exit($select);
并在jquery中:
.done(function(data){
//data has the response. You can do whatever you want to do with it.
}