dsb53973 2014-10-16 12:42
浏览 25
已采纳

要求用户选择Checkbox

So basically, I want to require the user to select from 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-->
     <script>
            CKEDITOR.replace( 'postDesc',{customConfig:'/sics/ckeditor/ckeditor_config.js'});
            CKEDITOR.replace( 'postCont',{customConfig:'/sics/ckeditor/ckeditor_config.js'} );
     </script>
    <fieldset>
        <label><h5>Select a category</h5></label><br>
        <?php   
        $stmt2 = $db->query('SELECT catID, catTitle FROM category ORDER BY catTitle');
        $checked = '';
        while($row2 = $stmt2->fetch()){
            if(isset($_POST['catID'])){
                if(in_array($row2['catID'], $_POST['catID'])){
                   $checked="checked='checked'";
                }
            }
            echo "<input type='checkbox' name='catID[]' value='".$row2['catID']."' $checked> ".$row2['catTitle']."<br />";
        }
        ?>
    </fieldset>
    <br>
    <p><input class="btn btn-primary btn" type='submit' name='submit' value='Submit'></p>
    <?php include('../includes/submitpost.php');?>
    </form>

Here is when user submits

<?php
//if form has been submitted process it
if(isset($_POST['submit'])){
    //collect form data
    extract($_POST);
    //very basic validation
    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 {
            $postURL = slug($postTitle);
            //insert into database
            $stmt = $db->prepare('INSERT INTO posts (postTitle,postURL,postDesc,postCont,postDate) VALUES (:postTitle, :postURL, :postDesc, :postCont, :postDate)') ;
            $stmt->execute(array(
                ':postTitle' => $postTitle,
                ':postURL' => $postURL,
                ':postDesc' => $postDesc,
                ':postCont' => $postCont,
                ':postDate' => date('Y-m-d H:i:s')
            ));
            $postID = $db->lastInsertId();
            //add categories
            if(is_array($catID)){
                foreach($_POST['catID'] as $catID){
                    $stmt = $db->prepare('INSERT INTO post_cats (postID,catID)VALUES(:postID,:catID)');
                    $stmt->execute(array(
                        ':postID' => $postID,
                        ':catID' => $catID
                    ));
                }
            }
            //redirect to MainPanel page
            header('Location: ../users/MainPanel.php');
            exit;
        } catch(PDOException $e) {
            echo $e->getMessage();
        }
    }
}
//check for any errors
if(isset($error)){
    foreach($error as $error){
        echo '<p class="error">'.$error.'</p>';
    }
}?>

Is it possible? cause I cant get it to work

  • 写回答

5条回答 默认 最新

  • dpw70180 2014-10-16 12:51
    关注

    When you have multiple checkboxes with same name (array), you can't use required attribute for "at least one checkbox" (http://jsfiddle.net/frantisekmasa/kvtbqLe7/2/).

    You should check it server-side:

    if(isset($_POST['catID']) && ! empty($_POST['catID']) {
        // User has selected at least one checkbox
    } else {
       // User hasn't selected checkbox
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵