简体   繁体   中英

Re-enter only certain values in html form

I am making an html form where I am taking certain inputs from user which include name, photos, and other details. I am also validating the fields (eg checking that a selected file entered is an image).

The problem is whenever the user select a file with the wrong type, it is checked only after the submit button is clicked!

I do not want the user to enter all the details of the form again -- only the field(s) he or she entered incorrectly should be required.


This is what I have tried:

<form action="abc.php" method = "POST" enctype="multipart/form-data">
<input type="text" name="detail" value=<?php echo $_POST['detail']; ?> >

But it produces this error:

Undefined variable : detail

I even tried:

<input type="text" name ="detail" value="<?php if($_POST['detail'])
                                                 $test=$_POST['detail'];
                                               else $test='';
                                               echo $test; ?>">

使用php isset以便在三元比较中检查post变量是否存在:

<input type="text" name="detail" value="<?php echo isset($_POST['detail']) ? $_POST['detail'] : ''; ?>" />

use like following

<form action="abc.php" method = "POST" enctype="multipart/form-data">
<input type="text" name="detail" value=<?php if(isset($_POST['detail'])){ echo $_POST['detail']; } ?> >

so every time when your form will load. then in value we need to check first if $_POST["detail"] is set ? then only we should print it. so by using isset() you can avoid such errors.

Javascript is used for validation of forms on the client side. So that you can stop the submission before posting the data(Before pressing Submit button)

It is the best practice because with that you avoid unnecessary server calls.

Please follow tutorial on the link given below to achieve your task: http://jqueryvalidation.org/extension-method/

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