简体   繁体   中英

Ajax pass id using POST with php (Error Undefined variable: id)

Im new In AJAx. My problem is I have ajax Function to pass variable ID in php when the page is load the error is Undefined variable: id but when I look in firebug post id is past successfully . Here is my 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");                                 

and this is my php page to pass.

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

should be

$sql = mysql_query("select * from user where uid = $id ");

and

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

try this

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

try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM