[英]Ajax pass id using POST with php (Error Undefined variable: id)
我是AJAx新手。 我的問題是我有ajax函數在加載頁面時在php中傳遞變量ID,錯誤是未定義的變量:id,但是當我查看螢火蟲后,id成功地過去了。 這是我的ajax。
$('.btn_edit').click(function(e){
e.preventDefault();
var $this = $(this);
var id_id = $(this).attr('id');
alert(id_id);
$.ajax({
type: "POST",
url: "edit_query.php",
data:{id: id_id},
success: function() {
alert("Success Input");
這是我要傳遞的php頁面。
$id = $_POST['id'];
$sql = mysql_query("select * from user where uid = ".$id."");
$table = mysql_fetch_assoc($sql);
?>
$sql = mysql_query("select * from user where uid = ".$id."");
應該
$sql = mysql_query("select * from user where uid = $id ");
和
var id_id = $(this).attr('id');
alert(id_id);
$.ajax({
type: "POST",
url: "edit_query.php",
data:"id="+id_id,
success: function() {
alert("Success Input");
}
嘗試這個
$.post( "edit_query.php", { id: id_id })
.done(function( data ) {
alert( data );
});
嘗試這個
<?php
$id = $_POST['id'];
$sql = mysql_query('SELECT * FROM user WHERE uid = '.$id);
$row = mysql_fetch_assoc();
header('Content-Type: application/json');
echo json_encode($row);
exit;
$(function(){
var onClick, successHandler;
onClick = function (e) {
e.preventDefault();
$.post('edit_query.php',{id:$(this).attr('id')},successHandler,'json');
};
successHandler = function (json) {alert(json.uid);};
$('.btn_edit').click(onClick);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.