Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

My implementation doesn't seem to work. Can you point out what might be the problem or point me to a better solution? When I check the checkboxes and click the delete button, it doesn't seem to do anything... pls help me. Thanks!

<div id="container" class="page">
    <img id="disclaimer" class="page" src="images/DISCLAIMER.png" alt="" />
    <img id="logo" class="page" src="images/MI-LOGO.png" alt="" />
    <div id="postItDiv" class="page">
        <?php
        $cxn = mysqli_connect('localhost', 'root', 'root', 'TimePost') or die(mysqli_error());
        $query = "SELECT PostID, ClientName, VideoName, PostTime, Post, Date FROM tblTimePosts";
        $result = mysqli_query($cxn, $query) or die(mysqli_error());

        while($row = mysqli_fetch_assoc($result)) {
            $post = $row['Post'];
            $time = $row['PostTime'];
            $idNo = $row['PostID'];
            $postColor = rand(1, 4);
            echo '<div id="post1"><p class="postParagraph">Post ID No.' . $idNo . '<br />' . $post . ' at ' . $time . ' seconds mark</p><div class="close"><form method="POST" action="videoJudge.php"><input type="checkbox" name="checkbox[]" id="checkbox[]" value="' . $idNo . '" /></form></div></div>';
        }

        if($_SERVER['REQUEST_METHOD'] == "POST" && $_POST['btnPost']) {
            $postColors = rand(1, 4);
            $toPost = $_POST['textPost'];
            $date = date("y-m-d");
            $postTime = $_POST['display'];
            $postTime = floor($postTime);
            $insertPostQuery = "INSERT INTO tblTimePosts VALUES('','Mimagazine Asia','Chelsea','$postTime','$toPost','$date')";
            $query3 = "SELECT PostID FROM tblTimePosts";

            $result = mysqli_query($cxn, $insertPostQuery) or die(mysqli_error());
            $result3 = mysqli_query($cxn, $query3) or die(mysqli_error());
            if($result > 0) {
                while($row = mysqli_fetch_assoc($result3)) {
                    $idNo2 = $row['PostID'];
                }
                echo '<div id="post1"><p class="postParagraph">Post ID No.' . $idNo2 . '<br />' . $toPost . ' at ' . $postTime . ' seconds mark</p><div class="close"><form method="POST" action="videoJudge.php"><input type="checkbox" name="checkbox[]" id="checkbox[]" value="' . $idNo2 . '" /></form></div></div>';
            }
            else
                echo "Add Failed";
        }

        /* if($_POST['chkDelete']) {
            for($i=0;$i<5;$i++){
                $del_id = $checkDelete[$i];
                $sql = "DELETE FROM $tblTimePosts WHERE PostID='$del_id'";
                $resulta = mysqli_query($cxn,$sql);
            }
        } */

        if($_POST['btnDelete']) { // from button name="delete"
            /* $checkbox = $_POST['checkbox']; //from name="checkbox[]"
            $countCheck = count($_POST['checkbox']);
            echo $countCheck;
            for($i=0;$i<$countCheck;$i++){
                $del_id = $checkbox[$i];
                $sql = "DELETE FROM tblTimePosts WHERE PostID=".$del_id."";
                $resulta = mysqli_query($cxn,$sql) or die(mysqli_error());
            } */

            $tbl_name = 'tblTimePosts';
            foreach($_POST['checkbox'] as $id => $value) {
                $sql = 'DELETE FROM `' . $tbl_name . '` WHERE `PostID`=' . (int) $id;
                mysqli_query($cxn, $sql);
            }
            header('Location: videoJudge.php');
        }
        ?>
    </div>
    <div id="videoContainer" class="page">
        <video id="video" poster="images/cover.png" controls>
            <source src="video/animationTest.m4v" />
        </video>
    </div>
    <div id="etc" class="page">
        <form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
            <textarea name="textPost" id="textPost"> 
            </textarea>
            <input type="submit" name="btnPost" id="btnPost" value="Post" />
            <div id="tae">
                <input type="text" name="display" id="display" value="0" />
            </div>
        </form>
    </div>
    <div id="delButtonDiv">
        <form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
            <input type="submit" value="Delete" name="btnDelete" />
        </form>
    </div>
</div>

HERE IS MY CODE NOW AFTER FOLLOWING ALL YOUR COMMENTS BUT STILL DIDNT WORK. :(

<?php
            $cxn = mysqli_connect('localhost','root','root','TimePost') or die(mysqli_error());
            $query = "SELECT PostID, ClientName, VideoName, PostTime, Post, Date FROM tblTimePosts";
            $result = mysqli_query($cxn,$query) or die(mysqli_error());
            while($row = mysqli_fetch_assoc($result)) {
                $post = $row['Post'];
                $time = $row['PostTime'];
                $idNo = $row['PostID'];
                $postColor = rand(1,4);
                echo '<div id="post1"><p class="postParagraph">Post ID No.'.$idNo.'<br />'.$post.' at '.$time.' seconds mark</p><div class="close"><form method="POST" action="videoJudge.php"><input type="checkbox" name="checkbox[]" id="checkbox[]" value="'.$idNo.'" /></form>     
                </div></div>';
            }
             if ($_SERVER['REQUEST_METHOD'] == "POST" && $_POST['btnPost']) {
                $toPost = $_POST['textPost'];
                $date = date("y-m-d");
                $postTime = $_POST['display'];
                $postTime = floor($postTime);
                $insertPostQuery = "INSERT INTO tblTimePosts VALUES('','Mimagazine Asia','Chelsea','$postTime','$toPost','$date')";
                $query3 = "SELECT PostID FROM tblTimePosts";

                $result = mysqli_query($cxn, $insertPostQuery) or die(mysqli_error());
                $result3 = mysqli_query($cxn, $query3) or die(mysqli_error());
                if ($result > 0) {
                    while($row = mysqli_fetch_assoc($result3)) {
                        $idNo2 = $row['PostID'];
                    }
                    echo '<div id="post1"><p class="postParagraph">Post ID No.'.$idNo2.'<br />'.$toPost.' at '.$postTime.' seconds mark</p><div class="close"><form method="POST" action="videoJudge.php"><input type="checkbox" name="checkbox[]" id="checkbox[]" value="'.$idNo2.'"
                    /></form></div></div>';
                }
                else
                    echo "Add Failed";
            }
            if($_POST['btnDelete'] == "Delete")
             {
                 $tbl_name = 'tblTimePosts';
                    foreach ($_POST['checkbox'] as $id => $value) 
                    {
                        $sql = 'DELETE FROM `'.$tbl_name.'` WHERE `PostID`='.(int)$id;
                        mysqli_query($cxn,$sql);
                    }
                    header('Location: videoJudge.php'); 
             }
        ?>
        </div>
        <div id="videoContainer" class="page">
            <video id="video" poster="images/cover.png" controls>
                <source src="video/animationTest.m4v" />
            </video>
        </div>
        <div id="etc" class="page">
            <form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
                    <textarea name="textPost" id="textPost"> 
                    </textarea>
                        <input type="submit" name="btnPost" id="btnPost" value="Post" />
                        <div id="tae">
                            <input type="text" name="display" id="display" value="0" />
                        </div>

                <div id="delButtonDiv">
                <input type="submit" value="Delete" name="btnDelete" />
                </div>
            </form>
        </div>
share|improve this question

3 Answers

up vote 2 down vote accepted

Doing this should solve the problem, try:

<div id="etc" class="page">
    <form action="<?php $_SERVER['PHP_SELF']?>" method="POST">
        <textarea name="textPost" id="textPost"></textarea>
        <input type="submit" name="btnPost" id="btnPost" value="Post" />
        <div id="tae">
           <input type="text" name="display" id="display" value="0" />
        </div>

        <!-- END FORM REMOVED HERE -->

        <!-- FORM REMOVED HERE -->

     <div id="delButtonDiv"> <input type="submit" value="Delete" name="btnDelete" /> </div>
   </form>
</div>

[EDITED]

You have two submit button in your form, then you need to know what exactly action you are taking, POST or DELETE, right? then you can create an basic controller to your post in your own php script, something like this:

Here is the form -> <form action="<?php $_SERVER['PHP_SELF']?>" method="POST">

and here are the submit buttons

<input type="submit" name="btnPost" id="btnPost" value="Post" />
<input type="submit" value="Delete" name="btnDelete" />

$_SERVER['PHP_SELF'] tells you that it will send the form data to itself, if so, then do something like this in the begin of your script:

if ($_POST[btnPost] == "Post") 
// do some action to post the data

if ($_POST[btnDelete] == "Delete") 
// do some action to delete the data

clear now?

share|improve this answer
 
nothing happened sir :( –  AJ Naidas Dec 15 '11 at 15:16
 
this is my code now. i followed all comments here but stil didnt work. –  AJ Naidas Dec 15 '11 at 15:35
 
i posted my new code above but still didnt work :( –  AJ Naidas Dec 15 '11 at 15:37
 
@AJ Naidas - You still doing wrong in the form, you do not need two <form> tags, just one and everything is wrapped in it.. your new code will never work like this. And you do not need this -> $_SERVER['REQUEST_METHOD'] == "POST" just use $_POST['btnPost'] == "Post" is more than enough.. –  B4NZ41 Dec 15 '11 at 15:54

You have using two form, where the submit button is belong to second form.
When you press the submit button, it only submit an empty form.
Move the submit button into first form should fix the problem.

You have two lines of

<form action="<?php $_SERVER['PHP_SELF']?>" method="POST">
..
...
<form action="<?php $_SERVER['PHP_SELF']?>" method="POST">

Remove the second one, and

<form action="<?php $_SERVER['PHP_SELF']?>" method="POST">
                   ^ missing an echo

If you want to submit back to same URL, is not required to set the form action.

share|improve this answer
 
what do you mean two forms? where should i put my delete block of codes? –  AJ Naidas Dec 15 '11 at 15:11
 
nothing happened sir. still nothing happens :( –  AJ Naidas Dec 15 '11 at 15:21

The delete button must be located with checkbox inside the same form where you went to submit to.

<form method="POST" action="videoJudge.php" >
                              ^______ url of the form you want to submit to. Possibly <?php echo $_SERVER['PHP_SELF']?>

<input type="checkbox" name="checkbox[]" id="checkbox[]" value="'.$idNo.'" />
<input type="submit" value="Delete" name="btnDelete" /> 
</form>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.