-2

I have Below Form 5-10:-

<div id="post">
<form method="post" action="javascript:alert('success')" >
    <input id="postid" type="hidden" name="post" value="9" />
    <input id="unique" type="hidden" name="unique" value="<?php echo $unique; ?>" />
    <input type="submit" value="Submit" name="submit" />
</form>
</div>
<div id="post">
<form method="post" action="javascript:alert('success')" >
    <input id="postid" type="hidden" name="post" value="8" />
    <input id="unique" type="hidden" name="unique" value="<?php echo $unique; ?>" />
    <input type="submit" value="Submit" name="submit" />
</form>
</div>
<div id="post">
<form method="post" action="javascript:alert('success')" >
    <input id="postid" type="hidden" name="post" value="7" />
    <input id="unique" type="hidden" name="unique" value="<?php echo $unique; ?>" />
    <input type="submit" value="Submit" name="submit" />
</form>
</div>
<div id="post">
<form method="post" action="javascript:alert('success')" >
    <input id="postid" type="hidden" name="post" value="6" />
    <input id="unique" type="hidden" name="unique" value="<?php echo $unique; ?>" />
    <input type="submit" value="Submit" name="submit" />
</form>
</div>
<div id="post">
<form method="post" action="javascript:alert('success')" >
    <input id="postid" type="hidden" name="post" value="5" />
    <input id="unique" type="hidden" name="unique" value="<?php echo $unique; ?>" />
    <input type="submit" value="Submit" name="submit" />
</form>
</div>
<div id="post">
<form method="post" action="javascript:alert('success')" >
    <input id="postid" type="hidden" name="post" value="4" />
    <input id="unique" type="hidden" name="unique" value="<?php echo $unique; ?>" />
    <input type="submit" value="Submit" name="submit" />
</form>
</div>

AND I HAVE below jquery to perform ajax and delete that particular post, but it is not performing delete.

<script type="text/javascript">
$(document).ready(function(){
    $("#post").submit(function() {
            var postid = $('#postid').val();
            var unique= $('#unique').val();
            var str = 'unique='+ unique+ '&postid='+ postid;
            $.ajax({
                type: "POST",
                url: "delete.php",
                data: str,
                success: function(msg) {
                    $("#post").ajaxComplete(function(event, request, settings) {
                        if (msg == 'OK')
                        {
                            result = '<div style="color:red;">Something Went Wrong</div><br />';
                        } else {
                            result = msg;
                        }
                        $("#post").html(result);
                    });
                }
            });

            return false;

        });
});
</script>

my PHP delete code :-

<?php
if($_POST['unique'] === $_SESSION['unique']) {
    $delete = $mysqli->query("DELETE from post where postid='".$mysqli->real_escape_string($_POST['postid'])."'");
    echo "DELETED";
} else {
    echo "OK";
}
?>

It is not deleting post when submit function is pressed... I want to delete that particular postid whenever user submit any particular form...

4

1 回答 1

0

您需要更改父 div 以将类名设置为“post”,而不是让所有的 id 都相同。然后,您可以将所有具有“post”类的项目绑定到提交函数,并在函数中找到每个表单作为事件源并找到子项以获取值。在此处查看实时解决方案:http: //jsfiddle.net/vendettamit/nn9FV/ 更新代码在此处:

$(document).ready(function(){

    $("div.post").submit(function(e){
        console.log(e);
        var currForm = e.delegateTarget.firstElementChild;
            var postid = $(currForm).children('#postid').val();
            var unique= $(currForm).children('#unique').val();
            var str = 'unique='+ unique+ '&postid='+ postid;
        console.log(str)
            $.ajax({
                type: "POST",
                url: "delete.php",
                data: str,
                success: function(msg) {
                    $("#post").ajaxComplete(function(event, request, settings) {
                        if (msg == 'OK')
                        {
                            result = '<div style="color:red;">Something Went Wrong</div><br />';
                        } else {
                            result = msg;
                        }
                        $("#post").html(result);
                    });
                }
            });

            return false;

        });
});

并且您的更新 HTML 如下所示:

<div class="post">
<form method="post" action="javascript:alert('success')" >
    <input id="postid" type="hidden" name="post" value="9" />
    <input id="unique" type="hidden" name="unique" value="<?php echo $unique; ?>" />
    <input type="submit" value="Submit" name="submit" />
</form>
</div>
<div class="post">
<form method="post" action="javascript:alert('success')" >
    <input id="postid" type="hidden" name="post" value="8" />
    <input id="unique" type="hidden" name="unique" value="<?php echo $unique; ?>" />
    <input type="submit" value="Submit" name="submit" />
</form>
</div>
<div class="post">
<form method="post" action="javascript:alert('success')" >
    <input id="postid" type="hidden" name="post" value="7" />
    <input id="unique" type="hidden" name="unique" value="<?php echo $unique; ?>" />
    <input type="submit" value="Submit" name="submit" />
</form>
</div>
<div class="post">
<form method="post" action="javascript:alert('success')" >
    <input id="postid" type="hidden" name="post" value="6" />
    <input id="unique" type="hidden" name="unique" value="<?php echo $unique; ?>" />
    <input type="submit" value="Submit" name="submit" />
</form>
</div>
<div class="post">
<form method="post" action="javascript:alert('success')" >
    <input id="postid" type="hidden" name="post" value="5" />
    <input id="unique" type="hidden" name="unique" value="<?php echo $unique; ?>" />
    <input type="submit" value="Submit" name="submit" />
</form>
</div>
<div class="post">
<form method="post" action="javascript:alert('success')" >
    <input id="postid" type="hidden" name="post" value="4" />
    <input id="unique" type="hidden" name="unique" value="<?php echo $unique; ?>" />
    <input type="submit" value="Submit" name="submit" />
</form>
</div>
于 2013-07-01T11:05:00.810 回答