简体   繁体   English

使用先决条件的PHP表单验证

[英]php form validation using prerequisite

i am trying to validate some fields in my form. 我正在尝试验证表单中的某些字段。 I am working with two fields, "address" and "state", initially the "address" and "state" field are not compulsory but if any value is entered into the "address" field the "state" field (which is a selection list) automatically becomes compulsory. 我正在使用“地址”和“状态”两个字段,最初“地址”和“状态”字段不是强制性的,但是如果在“地址”字段中输入了任何值,则“状态”字段(这是一个选择)列表)自动成为必修课。 I am just unsure on how to code the right IF condition. 我只是不确定如何编写正确的IF条件。

Here is what i have started on: 这是我开始的事情:

<?php

if (isset($_POST["submit"])) {

$address = $_POST["address"];
$address = trim($address);
$lengtha = strlen($address);
$post = $_POST["post"];
$state = $_POST["state"];

if ($lengtha > 1) {

?>

<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="custinfo" >
<table>
<tr>
    <td><label for="custid">Customer ID (integer value): </label></td>
    <td><input type="text" id="custid" name="custid" value="<?php echo $temp ?>" size=11 /><?php echo $msg; ?></td>
</tr>

<tr>
    <td><label for="customerfname">Customer First Name: </label></td>
    <td><input type="text" id="fname" name="fname" size=50/><?php echo $strmsg; ?></td>
</tr>
<tr>
    <td><label for="customerlname">Customer Last Name: </label></td>
    <td><input type="text" id="lname" name="lname" size=50/><?php echo $strmsgl; ?></td>
</tr>
   <tr>
    <td><label for="customeraddress">Customer Address: </label></td>
    <td><input type="text" id="address" name="address" size=65/></td>

    <td><label for="suburb"> Suburb: </label></td>
<td><input type="text" id="suburb" name="suburb"/></td>
</tr>
<tr>
<td>
State:<select name="state" id="state">
    <option value="select">--</option>
    <option value="ACT">ACT</option>
    <option value="NSW">NSW</option>
    <option value="NT">NT</option>
    <option value="QLD">QLD</option>
    <option value="SA">SA</option>
    <option value="TAS">TAS</option>
    <option value="VIC">VIC</option>
     <option value="WA">WA</option>
   </select>
</td>

Any help with figuring this out would be great! 弄清楚这一点的任何帮助将是巨大的!

Essentially, you just need to validate your state field if and only if the address field is not empty. 本质上,仅当地址字段不为空时,您才需要验证状态字段。 This can be achieved with the following code: 这可以通过以下代码实现:

if ( isset( $_POST[ 'address' ] ) && ! empty( $_POST[ 'address' ] ) ) {
    // An address has been provided, so validate the state.
    if ( ! isset( $_POST[ 'state' ] ) || ! in_array( $_POST[ 'state' ], $valid_states ) ) {
        // There was an error: the address is set but the state is not.
    }
}

Keep in mind that $valid_states in the above code represents an array of all the state values from the select list that should be accepted by the form. 请记住,以上代码中的$valid_states表示应由表单接受的选择列表中所有状态值的数组。 For example: 例如:

$valid_states = array(
    'KY', 'IL', 'FL', 'WY', /* ... */
);

You could also add some JavaScript to your form to completely hide the state field if the address field is not populated. 如果未填充地址字段,还可以向表单中添加一些JavaScript以完全隐藏状态字段。 Since state will only be validated if address is populated, it's existence on the form will not matter. 由于只有在填充地址后才会验证状态,因此表单上的状态将无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM