Pregunta

I've a comment box, i want to add a button into every comment, which will delete that comment from box when clicked. Here is how each comment is generated (just part):

echo '<div>'.$row['Login'].'<button id="jqhide">Hide</button></strong><br/>';

As you see, button has an id, when it's clicked, jquery listener start to work:

$("button#jqhide").click(function(){
    var smt = $(this).closest("div").find("p").html();
    $.post("send.php", smt);
});

in 'send.php':

$con = mysqli_connect(...);
$vrr = $_POST['smt'];
$sql = mysqli_query("DELETE FROM comments WHERE p_id=$vrr");
mysqli_query($con,$sql);

Here it is stuck, I mean $_POST['smt'] seems to be empty. All I want is to delete entry from MySQL by its 'p_id'. I'm pretty sure that I did a lot of mistakes, but that is how I try to learn something.

Addition:

I Tried to run just 'send.php' and here are errors:

Warning: mysqli_query() expects at least 2 parameters, 1 given in Z:\home\localhost\www\php\send.php on line 5

Warning: mysqli_query() [function.mysqli-query]: Empty query in Z:\home\localhost\www\php\send.php on line 6

send.php lines:

<?php 
$con = mysqli_connect(localhost, "bla", "eco", "frst_db");
$vrr = $_POST['smt'];
echo $vrr;
$sql = mysqli_query("DELETE FROM comments WHERE p_id=$vrr");
mysqli_query($con,$sql);
?>
¿Fue útil?

Solución

First of all be careful with SQL injection

$sql = mysqli_query("DELETE FROM comments WHERE p_id=$vrr");

Now let's get back to your problem.

var smt = $(this).closest("div").find("p").html();

Where is the p tag ?

add a p tag and will work, otherwise the p tag will always be null!!!

<div>1<button id="jqhide">Hide</button><p>some text to be sent via ajax </p></strong><br/></div>
    <script>
        $("button#jqhide").click(function(){
            alert($(this).closest("div").find("p").html());
    var smt = $(this).closest("div").find("p").html();
    $.post("send.php", smt);
});
</script>   

Otros consejos

Try this:

Change:

$.post("send.php", smt);

to:

$.post("send.php", { smt: smt } );
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top