RonKevinT.Manuela 0 Junior Poster in Training

So basically, I want to require the user to select from the checkbox
Here is addpost.php

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

        <p><label>Title</label><br />
        <input class= "form-control" type='text' name='postTitle' value='<?php if(isset($error)){ echo $_POST['postTitle'];}?>'></p>

        <p><label>Description</label><br />
        <textarea name='postDesc' cols='60' rows='10'><?php if(isset($error)){ echo $_POST['postDesc'];}?></textarea></p>

        <p><label>Content</label><br />
        <textarea name='postCont' cols='60' rows='10'><?php if(isset($error)){ echo $_POST['postCont'];}?></textarea></p>
        <!--For textarea's rather then making the admins enter the html for the text themselves its better to use an editor-->
                CKEDITOR.replace( 'postDesc',{customConfig:'/sics/ckeditor/ckeditor_config.js'});
                CKEDITOR.replace( 'postCont',{customConfig:'/sics/ckeditor/ckeditor_config.js'} );

            <label><h5>Select a category</h5></label><br>


            $stmt2 = $db->query('SELECT catID, catTitle FROM category ORDER BY catTitle');
            $checked = '';
            while($row2 = $stmt2->fetch()){


                    if(in_array($row2['catID'], $_POST['catID'])){

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


        <p><input class="btn btn-primary btn" type='submit' name='submit' value='Submit'></p>
        <?php include('../includes/submitpost.php');?>


Here is when user submits


    //if form has been submitted process it

        //collect form data

        //very basic validation
        if($postTitle ==''){
            $error[] = 'Please enter the title.';

        if($postDesc ==''){
            $error[] = 'Please enter the description.';

        if($postCont ==''){
            $error[] = 'Please enter the content.';


            try {

                $postURL = slug($postTitle);

                //insert into database
                $stmt = $db->prepare('INSERT INTO posts (postTitle,postURL,postDesc,postCont,postDate) VALUES (:postTitle, :postURL, :postDesc, :postCont, :postDate)') ;
                    ':postTitle' => $postTitle,
                    ':postURL' => $postURL,
                    ':postDesc' => $postDesc,
                    ':postCont' => $postCont,
                    ':postDate' => date('Y-m-d H:i:s')
                $postID = $db->lastInsertId();

                //add categories
                    foreach($_POST['catID'] as $catID){
                        $stmt = $db->prepare('INSERT INTO post_cats (postID,catID)VALUES(:postID,:catID)');
                            ':postID' => $postID,
                            ':catID' => $catID

                //redirect to MainPanel page
                header('Location: ../users/MainPanel.php');

            } catch(PDOException $e) {
                echo $e->getMessage();



    //check for any errors
        foreach($error as $error){
            echo '<p class="error">'.$error.'</p>';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.