mercredi 28 décembre 2016

The input value of checkbox array can't pass to POST on my php site

I have a problem on passing the input value of checkbox array to POST but it doesn't work at all. The following codes are about how I pass the input value of checkbox array to POST. The database and the server work fine as I tried only the value instead of passing variables and it works in this case and it must be the problem of the coding part. I tried several approaches from others but it didn't work at all. Does anyone have any idea? Thanks!

The checkbox part in input form:

        <fieldset>
    <legend>Categories</legend>
    <?php
    $stmt2 = $db->query('SELECT catID, catTitle FROM blog_cats ORDER BY catTitle');
    while($row2 = $stmt2->fetch()){

        $stmt3 = $db->prepare('SELECT catID FROM blog_cat_cats WHERE catID=:catID AND postID=:postID');
        $stmt3->execute(array(':catID'=> $row2['catID'],':postID'=> $row['postID']));
        $row3 = $stmt3->fetch();

        if($row3['catID'] == $row2['catID']){
            $checked = 'checked="checked"';
        } else {
            $checked = '';

        }
        echo "<input type='checkbox' name='catIDlist[]' value='".$row2['catID']."' $checked> ".$row2['catTitle']."<br/>";
    }
    ?>
    </fieldset>

The POST part to retrieve the value from the checkbox

    if(isset($_POST['submit'])){
    $_POST = array_map( 'stripslashes', $_POST);

    //collect form data
    extract($_POST);

            $stmt = $db->prepare('DELETE FROM blog_cat_cats WHERE postID = :postID');
            $stmt->execute(array(':postID' => $postID));

            if(is_array($catIDlist)){
                foreach($_POST['catIDlist'] as $catvalue){
                    $stmt = $db->prepare('INSERT INTO blog_cat_cats (postID, catID) VALUES (:postID, :catID)');
                    $stmt->execute(array(
                        ':postID' => $postID, 
                        ':catID' => $catvalue
                    ));
                 }
             }




Aucun commentaire:

Enregistrer un commentaire