如何在下拉选择菜单中添加验证?

时间:2013-04-06 08:53:47

标签: php validation drop-down-menu option

如何在下拉选择菜单中添加验证我有代码,我正在努力,但问题是,什么都不工作显示错误消息。如果在点击添加到购物车按钮之前不应选择下拉选择值,我想显示错误消息吗?

if(isset($_REQUEST['check'])){
if (isset($_POST['size']) && $_POST['size'] == '' ) {
echo 'Error, select category';
exit;
}}

<form action="" id="size1" name="size1" method="post"> 
<select style="background-color:#CCC" 
name="size" id="size"  onchange="this.form.submit();" >
<option value="">Select</option>
<option <?php if ($_POST['size'] == 'Small') 
print 'selected '; ?>  value="Small">Small</option>
<option <?php if ($_POST['size'] == 'Medium') 
print 'selected '; ?> value="Medium">Medium</option>
<option  <?php if ($_POST['size'] == 'Large') 
print 'selected '; ?> value="Large">Large</option>
<option   <?php if ($_POST['size'] == 'XL')
print 'selected '; ?> value="XL">XL</option>
</select>

<input type="button" class="button1" name="check" 
value="Add To Cart" onclick="addtocart(<?php echo $row3['id'];?>);" />
</form

3 个答案:

答案 0 :(得分:0)

请尝试它的工作

应该是这样的

<?php
    if($_POST['country']=="0")
    {
        echo 'Error, select category';
        //exit;
    }
?>
<form action="" id="size1" name="size1" method="post">
<select style="background-color:#CCC" 
name="size" id="size"  onchange="this.form.submit();" >
  <option value="0">Select</option>
  <option <?php if ($_POST['size'] == 'Small') 
print 'selected '; ?>  value="Small">Small</option>
  <option <?php if ($_POST['size'] == 'Medium') 
print 'selected '; ?> value="Medium">Medium</option>
  <option  <?php if ($_POST['size'] == 'Large') 
print 'selected '; ?> value="Large">Large</option>
  <option   <?php if ($_POST['size'] == 'XL')
print 'selected '; ?> value="XL">XL</option>
</select>
<input type="button" class="button1" name="check" 
value="Add To Cart" onclick="addtocart(<?php echo $row3['id'];?>);" />
</form>

答案 1 :(得分:0)

如果我的问题是正确的,您希望在表单提交之前进行验证吗?如果是这样,那么您需要使用客户端验证:

http://docs.jquery.com/Plugins/validation#Example

答案 2 :(得分:0)

在这里使用html进行小改动

<option value="0">Select</option>

和这样的功能来检查

function check_selected_or_not(){
var check_selected = jQuery('#size').val();
if (check_selected == 0){
    alert('Please select');
}else{
    alert('sucess');
}

}