簡體   English   中英

使用POST和php進行Ajax傳遞ID(錯誤未定義變量:ID)

[英]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 );
  });

嘗試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM