简体   繁体   中英

How to prevent from resubmission when submit button in pressed without entering data.?

I have a simple form that submit data into database that I have created with the name of stu_data. The problem is when I enter the add button without entering data in student ID, student name and degree, it automatically submit 0 in ID and -select- in Degree column. How I can prevent this problem, I need a simple solution. Thanks

 <html> <head> <center> <table border="5" bgcolor="lightblue"><br> <a href="student.php">Student</a> &nbsp;&nbsp;&nbsp;&nbsp; <a href="degree.php">Degree</a> &nbsp;&nbsp;&nbsp;&nbsp; <a href="courses.php">Courses</a> &nbsp;&nbsp;&nbsp;&nbsp;<br><br> </center> </head> <body> <form method="POST" action="" name="reg" onSubmit="return validate()"> <center> <table border="5" bgcolor="lightblue"> <h1 ><font color="blue"><b>STUDENT REGISTRATION SYSTEM</b></font></h1><br><a href="student.php">HOME</a>&nbsp&nbsp&nbsp&nbsp<a href="update.php">UPDATE</a>&nbsp&nbsp&nbsp&nbsp<a href="delete.php">DELETE</a>&nbsp&nbsp&nbsp&nbsp<br><br> </center> <tr> <td>Student ID:</td> <td><input type="int" name="id" /></td> </tr> <tr> <td>Student Name:</td> <td><input type="text" name="name" /></td> </tr> <tr> <td>Degree:</td> <td> <select name="degree" input type="text" > <option> -Select- </option> <option>Bs Software Engineering</option> <option>Bs Telecom Engineering</option> <option>Bs Electrical Engineering</option> <option>B Business Administration</option> <option>B Economics</option> </select> </td> </tr> <tr> <td>&nbsp;</td> <td><input type="submit" name="submit" value="ADD" /></td> </tr> </table> </form> </body> </html> <?php require'db.php'; $id = (isset($_POST['id']) ? $_POST['id'] : ''); $name = (isset($_POST['name']) ? $_POST['name'] : ''); $degree = (isset($_POST['degree']) ? $_POST['degree'] : ''); if(isset($_POST['submit'])){ $sql ="INSERT INTO student values('$id','$name','$degree')"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); } ?> 

Returning false from the function will stop the form submitting.

function validate(){

    /*
    ... Test your fields ...
    Allow submitting only if fields not empty
    For Example:
    */

    var allow=1;

    // If ID a number
    if(!(document.getElementsByName("id").value>0)){
         allow=0;
    }

    // Test Name Characters
    var regex = /^[a-zA-Z ]{2,30}$/;
    if (!regex.test(document.getElementsByName("name").value)) {
         allow=0;
    }


    if (allow){
       return true; // Allow the form to submit
    }else{
       return false; // Stop the form submitting
    }
}

You could deactivate the submit buttton and then use validation checks to ensure ita not empy.

You can do this using PHP or javascript, and when data is filled then reactivate the submit button to allow them to send or alternatively just dont submit data until all forms are filled

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