1

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

    ?>
7
  • do you see your POST data in console...? Commented Nov 29, 2013 at 6:45
  • show the form as well.. Commented Nov 29, 2013 at 6:49
  • Write : success: function(data) { alert(data);} ; and in php: echo $id; Commented Nov 29, 2013 at 6:51
  • remove e.preventDefault(); from top and add it at the end Commented Nov 29, 2013 at 6:53
  • @Sudhir Yes I see it and the output is correct even I try to alert the sucessful data. Commented Nov 29, 2013 at 6:59

3 Answers 3

1
$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");
     }
Sign up to request clarification or add additional context in comments.

2 Comments

I think your Example is all same.
send your data as data:"id="+id_id
0

try this

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

9 Comments

I try this I running but I try to echo $_POST['id'] there is value but in console it is empty. How can it be empty whilw when u echo it there is value. error is Notice: Undefined index: id in C:\xampp\htdocs\administrator\edit_query.php on line 8
@AmitSoni actually the edit_query.php is the the code which is this
$id = $_POST['id']; $sql = mysql_query("select * from user where uid = ".$id.""); $table = mysql_fetch_assoc($sql);
only this $id = $_POST['id'];
try print_r($_POST); and see what you are getting
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.