简体   繁体   中英

After Validate (Same page) the value stay on the page

Input(value) stay on the same page when click on "submit"(validation)

http://lab.iamrohit.in/cvalid/ The Code

<form action="" method="post">
<select name="type">
<option value="American">American Express</option>
<option value="Dinners">Diner's Club</option>
<option value="Discover">Discover</option>
<option value="Master">Master Card</option>
<option value="Visa">Visa</option>
</select>
<input type="text" name="cNum">
<button type="submit">Submit</button>
</form>

After click on submit , the input stay on the text box

You have not specified the action in your form that's why the form is by default is submitting to the same page. You can use autocomplete .

Autocomplete allows the browser to predict the value. When a user starts to type in a field, the browser should display options to fill in the field, based on earlier typed values.

<input type="text" name="cNum" autocomplete="off">

If you want form selected values be as selected after form subimmision, try this

$type = '';
$cNum  = '';
if(isset($_POST['submit'])){
  $type = $_POST['type'];
  $cNum = $_POST['cNum'];
 }
?>
<form action="" method="post">
 <select name="type">
    <option value="American" <?php if($type == 'American'):?>selected="selected"<?php endif;?>>American Express</option>
    <option value="Dinners"  <?php if($type == 'Dinners'):?>selected="selected"<?php endif;?>>Diner's Club</option>
    <option value="Discover" <?php if($type == 'Discover'):?>selected="selected"<?php endif;?>>Discover</option>
    <option value="Master"   <?php if($type == 'Master'):?>selected="selected"<?php endif;?>>Master Card</option>
    <option value="Visa"     <?php if($type == 'Visa'):?>selected="selected"<?php endif;?>>Visa</option>
</select>
  <input type="text" name="cNum" value="<?php echo $cNum;?>"/>
  <input type="submit" name="submit" value="submit"/>
</form> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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