Search code examples
phpjquerybootstrap-modalconfirmation

Form not submitting after bootstrap confirmation


Firstly I will apologise my knowledge of jquery/javascript is not very good so my mistake maybe very obvious to some but I am still learning so be gentle!

I have a form which allows the members to delete posts they have made from the database. The form works fine and using php deletes the data from the database as I want. I wanted to create a popup that gets the user to confirm thats what they would like to do. I don't want to use the bog standard browser specific onclick method I wanted something a bit more customisable. So I am trying to use bootbox, I currently have the confirmation box popping up onclick but when you select yes, nothing else happens. I suspect the result from the jquery is not firing the form but from what I have read my code for that part is good?

I have pasted below my php, jquery and html

PHP:

<?php
$deletepost = 'none';
$deleteposterror = 'none';

if(isset($_POST['delete'])) {

    $del_topic_id = $_POST['topic_id_value'];

    // sql to delete a record
    $pass_fail = "DELETE FROM topics WHERE topic_id ='$del_topic_id';";

    $pass_fail .= "DELETE FROM posts WHERE topic_id_post ='$del_topic_id'";

    if (mysqli_multi_query($conn, $pass_fail)) {

    $deletepost = 'show';

    }else{

    $deleteposterror = 'show';
    echo "ERROR: Could not able to execute $pass_fail. "  . mysqli_error($conn);    

    }

}
?>

HTML & PHP in while loop:

<?php


            $sql1 = "SELECT * FROM topics WHERE topic_by = '".$id."' ORDER BY topic_date DESC ";
            $result1 = mysqli_query($conn, $sql1);

            while($row = $result1->fetch_assoc()) {

            $topic_id = $row ['topic_id'];
            $topic_subject = $row ['topic_subject']; 
            ?>



            <div class='chat_post_titles' >
            <div class='row'>
            <div class='col-md-7 chat_post_text_align_left'>
            <a href="../Chat/post.php?topic_id=<?php echo $topic_id?>"><?php echo $topic_subject?>.</a>
            </div>
            <div class='col-md-2 chat_post_text_align_right'>
            <?php
            $my_date = $row['topic_date'];
            $date = DATE("G:i:s d/m/Y",strtotime($my_date));
            ?>
            <div class='hide_text'>Post Date: </div>
            <?php echo $date?>
            </div>
            <div class='col-md-2 chat_post_text_align_centre'>
            <div class='hide_text'>Delete Post: </div>

            <form method="post" class="delete_post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
            <input type="hidden" name="topic_id_value" id="topic_id_value" value="<?php echo $topic_id?>">
            <button type="submit" name="delete" id="delete" class="btn btn-danger buttonElement">Delete</button>
            </form>
            </div>
            </div>
            </div>
            <?php
            }
            ?>

JQUERY at bottom of page:

</body>
<?php include("../PHP/js.php"); ?>
<script type="text/javascript" src="../Members/updatedetails.js"></script>
<script type="text/javascript" src="../Members/bootbox.min.js"></script>
<script>

$('form').on('click','button.buttonElement',function (e) {
    var form = $(this).closest('form'); // <-- add this

    e.preventDefault();

    bootbox.confirm({
        message: "Are you sure you want to delete your post?", 
        title: "Delete Post",
        buttons: {
            cancel: { 
                label: "No", 
                className: "btn btn-default"
            },
            confirm: { 
                label: "Yes", 
                className: "btn btn-default"
            }
         },
         callback: function(result) {
             if(result == true) {
                 form.submit();
             }
         }
    });
 });

</script>

</html>

Solution

  • Finally worked out why my code wasn't working in my php I was using if(isset($_POST['delete'])) but when using javascript submit() function this name attr is removed. So I used the name of the hidden input like so if(isset($_POST['topic_id_value'])) and it worked as normal.

    Still thanks to the guys that gave me advice