使用带有php的POST传递Ajax(错误未定义变量:id)

时间:2013-11-29 06:41:46

标签: javascript php jquery mysql ajax

我是AJAx的新人。我的问题是我有ajax函数在页面加载时在php中传递变量ID错误是未定义变量:id但是当我查看firebug post 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); 

    ?>

3 个答案:

答案 0 :(得分:1)

$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");
     }

答案 1 :(得分:0)

试试这个

$.post( "edit_query.php", { id: id_id })
  .done(function( data ) {
    alert( data );
  });

答案 2 :(得分:0)

试试这个

edit_query.php

<?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;

your.js

$(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);
});