Can't get Post value ID from row table to PHP using Jquery

I know that my question may be duplicate, but I've looked through a ton of questions with the same problem, but none of the solutions worked for me.

It's pretty simple, get the item-id value and POST it to del.php. I can't get the POST value in the php after the click.

<?php
  include '../include/db.php';

  $result = $mysqli->query("SELECT * FROM usuario WHERE nivel = '1'");

  while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td class='item-id'>" . $row['id'] . "</td>";
  echo "<td>" . $row['nome']. "</td>";
  echo "<td>" . $row['email']. "</td>";
  echo "</tr>";
  }

  ?>

  <script type="text/javascript">
    $("tr").click(function() {

    var id = $(this).find('.item-id').text();

    $.post('del.php',{id: id},function(json){

    window.location.href = "http://localhost/unesc/3/admin/del.php";
    }, "json")
});

</script>

del.php

<?php 

if (isset($_POST['id'])){
    $id = $_POST['id'];
    echo $id;
} else {
    echo "erro";
}

 ?>

The del.php just get the $_post['id'] and echo it. The window.location is there so i won't have to enter the address manually. The trigger may not be necessarily a Click, it can be a Button, woks just fine for me too.

EDIT: got the POST running with $.ajax, but now, with firebug i noticed that the post in del.php keeps returning empty.

EDIT2: got the del.php post response, but as an error, it says that 'id' don't exist.

Upvotes: 1

Views: 1017

Answers (3)

user8175839
user8175839

Reputation:

If THIS is what you need here is the code of both pages I used

del2.php

<script type="text/javascript" language="javascript" src="jquery.min.js"></script>

<?php
//include '../include/db.php';
include 'includeconnect.php';

//$result = $mysqli->query("SELECT * FROM usuario WHERE nivel = '1'");
$result = mysql_query("SELECT * FROM usuario WHERE nivel = '1'");

while($row = mysql_fetch_array($result))
{  
echo "
<table border='1'>
<tr>";
echo "<td class='item-id'>" . $row['id'] . "</td>";
echo "<td>" . $row['nome']. "</td>";
echo "<td>" . $row['email']. "</td>";
echo "</tr>
</table>
</br>";
}


echo"<div id='show_responce'>I am a text on page del2.php and I am pretty much useless apart from showing you where the responce will display</div>"
?>

<script type="text/javascript">
$("tr").click(function() {

var rep = $("#show_responce");

var id = $(this).find('.item-id').text();

var dataString = 'id=' + id;
    $.ajax({
        type: 'post',
        url: 'del.php',
        data: dataString,
        success: function(html) {
                 rep.empty().append(html);
                 rep.fadeIn("slow")
        }
    });     

});

</script>

Here is your modified del.php

<?php 

if (isset($_POST['id'])){
$id = $_POST['id'];
echo "I am coming from del.php and the ID you clicked on is $id";
} else {
echo "error";
}

?>

By the way because of my account's low reputation I could not ask you what was you trying to do with this window.location but whatever. Instead of what you asked for which was basically to make sure that del.php gets the value of id as you will see I displayed it on del2.php which is the page I am making the request from.

Upvotes: 1

Nurik Nurmetov
Nurik Nurmetov

Reputation: 79

Possible errors:

  1. }, "json") here last argument means, that function expects you return valid JSON string from del.php file.

  2. After window.localtion.href you can't access previous data, because it redirects to another page, and thus you loose all your previous data.

Upvotes: 0

Gowthaman D
Gowthaman D

Reputation: 578

    $( "table > td" ).find( ".item-id" ).val();

    or



<?php
while($row = mysqli_fetch_array($result))
  {
  echo "<tr id='row'>";
  echo "<td  class='item-id'>" . $row['id'] . "</td>";
  echo "<td>" . $row['nome']. "</td>";
  echo "<td>" . $row['email']. "</td>";
  echo "</tr>";
  }

?>
<script>
        var id = [];
    var values = [];
    $("#row > .item-id").each(function(index){
       id.push($(this).attr("id")); // [a,b,c,....]
       values.push($(this).text()); //[ Dummy1, Dummy2, Dummy3, Dummy4,..]
    });

</script>

Upvotes: 0

Related Questions