要求用户选择Checkbox

时间:2014-10-16 12:42:03

标签: php checkbox

所以基本上,我想要求用户从复选框中选择...

这是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>

以下是用户提交

的时间
<?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>';
    }
}?>

有可能吗?因为我无法让它工作

5 个答案:

答案 0 :(得分:1)

这有效:

<input type= "checkbox" name="field" required="required" />

答案 1 :(得分:0)

试试这个:

<input type='checkbox' name='catID[]' value='".$row2['catID']."' $checked required>

JSFiddle演示: http://jsfiddle.net/5f7663b0/

答案 2 :(得分:0)

如果您有多个具有相同名称(数组)的复选框,则无法使用必需属性&#34;至少有一个复选框&#34; (http://jsfiddle.net/frantisekmasa/kvtbqLe7/2/)。

您应该检查服务器端:

if(isset($_POST['catID']) && ! empty($_POST['catID']) {
    // User has selected at least one checkbox
} else {
   // User hasn't selected checkbox
}

答案 3 :(得分:0)

试试这个

在标题之间添加java脚本

                     <script type="text/javascript"> 
                     function checkForm(form) { ... if(!form.catID[].checked) { alert("your customised message here"); form.catID[].focus(); return false; } return true; }
                      </script>

                      edit the form part as
                      <form action='' method='post' onsubmit="return checkForm(this);">

                        <input type="checkbox" name="catID[]"  id="catID[]" value=".$row2['catID']." />

答案 4 :(得分:-1)

只需添加“#34; required&#34;到输入字段