0

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:

            <form action='' method='post'>

    <input type='hidden' name='postID' value='<?php echo $row['postID'];?>'>

    <p><label>Title</label><br />
    <input type='text' name='postTitle' value='<?php echo $row['postTitle'];?>'></p>

    <p><label>Description</label><br />
    <textarea name='postDesc' cols='60' rows='10'><?php echo $row['postDesc'];?></textarea></p>

    <p><label>Content</label><br />
    <textarea name='postCont' cols='60' rows='10'><?php echo $row['postCont'];?></textarea></p>

    <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 = '';

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

    <p><input type='submit' name='submit' value='Update'></p>
</form>

The POST part to retrieve the value from the checkbox

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

    //collect form data
    extract($_POST);
//Check the valid input from the form
    if($postID ==''){
        $error[] = 'This post is missing a valid id.';
    }
    if($postTitle ==''){
        $error[] = 'Please enter the title.';
    }
    if($postDesc ==''){
        $error[] = 'Please enter the description.';
    }
    if($postCont ==''){
        $error[] = 'Please enter the content.';
    }

    if(!isset($error)){

        try {
            $stmt = $db->prepare('UPDATE blog_posts SET postTitle = :postTitle, postDesc = :postDesc, postCont = :postCont WHERE postID =:postID');
            $stmt->execute(array(
                ':postTitle' => $postTitle, 
                ':postDesc' => $postDesc, 
                ':postCont' => $postCont, 
                ':postID' => $postID
                ));

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

            $catlist = array();

            $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
                    ));
                 }
             }
} catch (PDOException $e){
            echo $e->getMessage();
        }
    }

}
} ?>

2 Answers 2

0
<?php
$catIDlist = $_POST['catIDlist'];
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
        ));
    }
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

I have already put that thingy. Maybe I should clarify much clear on my question.
0

Have you var_dumped $_POST variables after submitting? Does it contain values in "catIDlist"?

It seems your coding part is ok and it should work.

Probably you have some input in below of this code with the same name "catIDlist" that will override values.

UPDATED

You have printing issue here. You are printing the name of checkbox as:

<input type='checkbox' name='catlist-<?= $row2['catID']; ?>' value='catlist-<?php echo $row2['catID']; ?>' <?php echo $checked;?>> <?php echo $row2['catTitle']; ?><br/>

So the result will be something like this:

<input type="checkbox" name="catlist-1" value="catlist-1">Cat Title<br />

But in the handler script you are checking $catIDlist variable.

Try to change the printing to this:

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

It should work.

4 Comments

I tried echo var_dump($_POST['catIDlist']); and it returns NULL...I didnt create variables named catIDlist other that the part of checkbox..Why would it happen?
Not sure why it happens. Can you post a full code of both scripts (form and handler script)?
haha, sorry, I copied the wrong code. Actually <?php echo $row2['catID']; ?>' <?php echo $checked;?>> <?php echo $row2['catTitle']; ?><br/> is my temporary sulotion as catIDlist[] doesn't work.. I can't pass the array to $_POST for some reason.. I mend my code now...
Sorry that I copied the wrong one..but I can see you're trying to help..is it possible that my server doesn't support passing array to $_POST?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.