PHP表单 - 需要文本框或下拉列表

时间:2015-09-11 18:20:00

标签: php html drop-down-menu

我想知道是否有人可以帮助我。我有一个带有两个必填字段的php联系表单。用户需要填写文本字段,或从下拉列表中选择一个选项。需要填写其中一个或两个。这就是我所拥有的:

    // previous requisition number
if(trim($_REQUEST['prevReqNum']) === '')  {
    $prevReqNumError = '<span class="error">Please enter your previous requisition number.</span>';
    $hasError = true;
} else if (!preg_match("/^([1]-)?[a-z0-9]{6}$/i", trim($_REQUEST['prevReqNum']))) {
    $prevReqNumError = '<span class="error">You entered an invalid requisition number.</span>';
    $hasError = true;

// assigned engineer
} else if(empty($_POST['engineer'])) {
$engineerError = '<span class="error">Please select the engineer that you were working with.</span>';
$hasError = true; }
else{
$engineer = $_POST['engineer'];
}

这是HTML:

         <p><label for="prevReqNum">*Previous Requisition Number:&nbsp;</label><input type="text" size="40" name="prevReqNum" id="prevReqNum" value="<?php if(isset($_POST['prevReqNum'])) echo $_POST['prevReqNum'];?>" placeholder=" eg. 000000" /> <?php if($prevReqNumError != '') { ?><span class="error"><?=$prevReqNumError;?></span><?php } ?>
             or <label for="engineer">Assigned Engineer: </label><select name='engineer' id="engineer" size="1">
              <option value="" selected="selected">Select</option>
              <option <?php echo $engineer=='Dexter'?'selected':''; ?> >Dexter</option>
              <option <?php echo $engineer=='Jake'?'selected':''; ?> >Jake</option>
              <option <?php echo $engineer=='Mo'?'selected':''; ?> >Mo</option>
              <option <?php echo $engineer=='Richard'?'selected':''; ?> >Richard</option>
            </select> <?php if($engineerError != '') { ?><span class="error"><?=$engineerError;?></span><?php } ?></p>  

因此,用户应填写申请号,或选择他们正在使用的工程师。我得到了申请号文本字段。因此,当我留下那个空白时,我收到一条错误消息。但是当我未选择工程师下拉菜单时,我没有收到任何错误消息。请原谅我,我不是程序员,所以我不确定这里有什么问题。谢谢!

4 个答案:

答案 0 :(得分:0)

您应该可以将其添加到PHP中。看起来您只需要检查工程师和prevReqNum,如果它们都不存在,则返回错误消息。这只是遵循其他验证。

if (empty($_POST['engineer']) && trim($_REQUEST['prevReqNum']) === '') {
    $prevReqNumError = '<span class="error">Please choose an engineer or enter a requisition number.</span>';
    $hasError = true;
}

答案 1 :(得分:0)

下面将列出潜在的错误。两者都是空的。

<?php
$reqNumber=trim($_POST['prevReqNum']);
$engineer=$_POST['engineer'];
if(empty($reqNumber) && empty($engineer)){
$prevReqNumError = '<span class="error">Please enter your previous requisition number or select an engineer.</span>';
$hasError = true;
}
else{
if(!empty($reqNumber))  {
if (!preg_match("/^([1]-)?[a-z0-9]{6}$/i", $reqNumber)) {
$prevReqNumError = '<span class="error">You entered an invalid requisition number.</span>';
$hasError = true;
}
else{
  if(empty($_POST['engineer'])){
    $engineerError = '<span class="error">Please select the engineer that you were working with.</span>';
    $hasError = true;
  }  
  }
}
}
?>

答案 2 :(得分:0)

主要问题是逻辑的布局。仔细看,你有:

if
else if
else if
else

一旦PHP匹配“if”,就会跳过其他测试。

你想要if / else用于以前的请求编号,而另一个if / else用于指定的工程师,因为如果有任何错误,你想抛出一个错误。

答案 3 :(得分:0)

我试图帮忙。它会有所帮助。

Add Form "action" and "onsubmit" in <form></form> tag.

<form method="POST" action="Give Here" onsubmit="return CheckValidation()">
<p>
    <label for="prevReqNum">*Previous Requisition Number:&nbsp;</label>
    <input type="text" size="40" name="prevReqNum" id="prevReqNum" value="<?php if(isset($_POST['prevReqNum'])) echo $_POST['prevReqNum'];?>" placeholder=" eg. 000000" />
    <span class="error InvalidRegdNo" style="display:none;">You entered an invalid requisition number.</span>
    <span class="error EmptyRegdNo" style="display:none;">Please enter your previous requisition number.</span>             
    OR <label for="engineer">Assigned Engineer: </label>
    <select name='engineer' id="engineer" size="1">
        <option value="" selected="selected">Select</option>
        <option <?php echo $engineer=='Dexter'?'selected':''; ?> >Dexter</option>
        <option <?php echo $engineer=='Jake'?'selected':''; ?> >Jake</option>
        <option <?php echo $engineer=='Mo'?'selected':''; ?> >Mo</option>
        <option <?php echo $engineer=='Richard'?'selected':''; ?> >Richard</option>
    </select>
    <span class="error SelectEngineer" style="display:none;">Please select the engineer that you were working with.</span>
    <span class="error SelectAtleastOne" style="display:none;">Please select atleast engineer or type registration no.</span>
</p>

</form>

<script>
    function CheckValidation()
    {
        var RegNo=$('#prevReqNum').val();
        var Engineer=$('#engineer').val();

        /*First Check Valid Registration Number*/
        if (!preg_match("/^([1]-)?[a-z0-9]{6}$/i", trim(RegNo)))
        {
            $('.InvalidRegdNo').show();
            return false;
        }
        if(RegNo=="" && Engineer=="")
        {
            $('.SelectAtleastOne').show();
            return false;
        }
    }
</script>