Php 要求用户选中复选框
所以基本上,我想要求用户选择从复选框 下面是addpost.php:Php 要求用户选中复选框,php,checkbox,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['postTit
<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>
标题
内容
replace('postDesc',{customConfig:'/sics/CKEDITOR/CKEDITOR_config.js'});
replace('postCont',{customConfig:'/sics/CKEDITOR/CKEDITOR_config.js'});
选择一个类别
这里是用户提交的时间
<?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>';
}
}?>
可能吗?因为我不能让它工作这个工作:
<input type= "checkbox" name="field" required="required" />
只需在输入字段中添加单词“required”试试这个:
jsiddle演示:当您有多个同名(数组)的复选框时,您不能对“至少一个复选框”()使用必需的属性 您应该在服务器端检查它:
if(isset($_POST['catID']) && ! empty($_POST['catID']) {
// User has selected at least one checkbox
} else {
// User hasn't selected checkbox
}
试试这个
在标题之间添加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']." />
函数checkForm(form){…if(!form.catID[].checked){alert(“此处的自定义消息”);form.catID[].focus();返回false;}返回true;}
将表单零件编辑为
要在客户端/服务器端执行检查吗?很抱歉,对于一项琐碎的任务,您可能希望减少代码并专注于问题领域,而不是向我们提供您的全部代码。显然,这在Safari上不起作用。但这并不重要,因为我通常不使用它。我不会对你投反对票,因为这是一个正确的回答,但请谨慎使用这一点,因为它只出现在较新的浏览器中(我在看你的IE)@Duniyadnd我认为“旧IE”不重要,因为在XP停止更新后,没有人敢将XP与IE6或更旧的浏览器一起使用:),你应该考虑jQuery验证插件()。如果我有选择远离Safari或旧浏览器,我也会远离jQuery和其他框架。我更喜欢一个“轻”的网站,而不是一个更花哨的“全兼容”网站。但我想这取决于应用程序/站点的目标。我认为这没有意义。只有复选框被发送到服务器,因此PHP无法检查它是否为空。@itsols如果不发送复选框,第一个条件将为false。在大多数Linux/Apache服务器上,如果在未设置字段的情况下尝试查看字段是否为空,则会出现错误。如果您只是一个在开发计算机上工作的Windows用户,则不太可能看到此错误。@itsols当第一个条件为false时,第二个条件将不被处理。顺便说一句,恐怕您对最后一条评论是错的。我认为当条件为假时,评估不会停止。据我所知,它必须完成评估。如果您所说的是正确的,那么简单地以不同的顺序重写条件将改变代码行为,我不认为这是如何发生的。你有证明你是正确的人吗?我只是好奇,拜托?你为什么不看好我?我刚刚回答了别人在我3分钟后做的事情?老实说,我没有给你打分。。。我只作了评论。如果我不看好你,我会给出一个很好的理由。对于一些用户来说,这是一种疾病,他们只是降低了速率,然后走开了。你可能因为回答不完整而被降级。我不知道为什么实际的降级被做。@mbo-我错误地给你降级了,当我意识到我做了什么时,我试图改变它,但已经六分钟了。很抱歉。我认为最初的原因是因为你的回答不足以解释——如果你考虑一下这个人和他问的问题类型,这是非常基本的,可以很容易地用谷歌搜索到,理想的情况是多握一点手可能会有所帮助。