如何在不输入数据的情况下按下提交按钮时防止重新提交。

时间:2018-01-28 18:05:50

标签: php sql

我有一个简单的表单,将数据提交到我用stu_data名称创建的数据库中。问题是当我输入添加按钮而没有在学生ID,学生姓名和学位中输入数据时,它会自动在ID和-select- in Degree列中提交0。如何防止这个问题,我需要一个简单的解决方案。感谢



<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();
    }
?>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

从函数返回false将停止表单提交。

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
    }
}

答案 1 :(得分:0)

您可以停用提交按钮,然后使用验证检查以确保它不会显示。

您可以使用PHP或javascript执行此操作,并在填写数据时重新激活提交按钮以允许他们发送,或者只是在所有表单填写完毕之前不提交数据