php form validation using prerequisite -


i trying validate fields in form. working 2 fields, "address" , "state", "address" , "state" field not compulsory if value entered "address" field "state" field (which selection list) automatically becomes compulsory. unsure on how code right if condition.

here 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 figuring out great!

essentially, need validate state field if , if address field not empty. can achieved following code:

if ( isset( $_post[ 'address' ] ) && ! empty( $_post[ 'address' ] ) ) {     // address has been provided, validate state.     if ( ! isset( $_post[ 'state' ] ) || ! in_array( $_post[ 'state' ], $valid_states ) ) {         // there error: address set state not.     } } 

keep in mind $valid_states in above code represents array of state values select list should accepted form. example:

$valid_states = array(     'ky', 'il', 'fl', 'wy', /* ... */ ); 

you add javascript form hide state field if address field not populated. since state validated if address populated, it's existence on form not matter.


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -