2013-01-14 20 views
-5

这是通过电子邮件发送的表单的一部分。当你选择选项状态时,文本框状态是必需的,如果你不这样做的话,对于县和邮政编码来说是相同的。 我想要的是验证这个PHP,所以我可以发送电子邮件,但到目前为止我还没有能够完成它。PHP - 如果选择了选项,Multiselect需要字段

下面是HTML:

<select multiple="multiple" name="geographic" id="geographic"> 
    <option value="state">State</option> 
    <option value="county">County</option> 
    <option value="zip">Zip Code</option> 
</select> 
<label for="state">What state?</label> 
<input id="state" name="state" type="text"> 
<label for="county">What County?</label> 
<input id="county" name="county" type="text"> 
<label for="zipcode">What is the Zip Code? *</label> 
<input id="zipcode" name="zipcode" type="text"> 
+1

请输入PHP代码? –

+4

http://whathaveyoutried.com/ – Stu

+1

您不需要在服务器上进行此验证。为什么不用JavaScript代替? – 2013-01-14 19:35:29

回答

-1

首先你需要用你的代码的形式,但你可能确实已经?如果不是,你应该看看http://www.w3schools.com/php/php_forms.asp

表单发布的php文件接收它与$ _POST,在地理条件是$ _POST ['地理']的情况下,同样的状态。现在您知道如何访问该值,您可以验证它。

所以

if($_POST['geographic'] == 'state'){ 

    if(empty($_POST['state'])){ 
      echo 'state is required'; 
    }else{ 
      echo 'state is given, proceed'; 
    } 

} 
+1

[w3fools.com](http://w3fools.com/) – 2013-01-14 19:36:01

+0

那是怎么回事? – Frankey

+0

点击我的第一个评论中的链接。阅读它指向的页面。 – 2013-01-20 02:01:54

1

你可以使用Javascript允许在前端验证和不需要页面加载,类似下面应该为你工作:

<head> 
<script type="text/javascript"> 
function SelectValidator() { 
    // Get the select object 
    var index = document.getElementById("geographic").selectedIndex; 
    var select = document.getElementById("geographic").options; 

    // Get our input elements 
    var county = document.getElementById('county'); 
    var state = document.getElementById('state'); 
    var zipcode = document.getElementById('zipcode'); 

    if (select[index].value === "county") { 
    if (county.value === null || county.value === "") { 
     alert("Please complete the County field"); 
    } else { 
     alert("You could simply put a return statement here."); 
    } 
    } else if (select[index].value === "state") { 
    if (state.value === null || state.value === "") { 
     alert("Please complete the state field"); 
    } else { 
     alert("You could simply put a return statement here."); 
    } 
    } else if (select[index].value === "zip") { 
    if (zipcode.value === null || zipcode.value === "") { 
     alert("Please complete the Zip Code field"); 
    } else { 
     alert("You could simply put a return statement here."); 
    } 
    } 
} 
</script> 
</head> 

而且您的表格:

<form> 
<select multiple="multiple" name="geographic" id="geographic"> 
    <option value="state">State</option> 
    <option value="county">County</option> 
    <option value="zip">Zip Code</option> 
</select> 

<label for="state">What state?</label> 
<input id="state" name="state" type="text"> 

<label for="county">What County?</label> 
<input id="county" name="county" type="text"> 

<label for="zipcode">What is the Zip Code? *</label> 
<input id="zipcode" name="zipcode" type="text"> 


<button type="button" onclick="SelectValidator()">Display index</button> 
</form> 
相关问题