我是php的新手,还在学习。im需要的是,一旦用户在age字段中输入了非整数的内容而不提交给mysql,就停止表单。以下是我的基本表格和代码:
添加.html->
<form action="add.php" method="post" name="form1">
<table width="25%" border="0">
<tr>
<td>Name</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Age</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Add"></td>
</tr>
</table>
</form>
下面是进程文件add.php->
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
//including the database connection file
include_once("config.php");
if(isset($_POST['Submit'])) {
$name = mysqli_real_escape_string($mysqli, $_POST['name']);
$age = mysqli_real_escape_string($mysqli, $_POST['age']);
$email = mysqli_real_escape_string($mysqli, $_POST['email']);
// checking integer fields
if (!ctype_digit($age)){
echo "<font color='red'>ERREUR! age n'est PAS entier</font><br/>";
}
// checking empty fields
if(empty($name) || empty($age) || empty($email)) {
if(empty($name)) {
echo "<font color='red'>Name field is empty.</font><br/>";
}
if(empty($age)) {
echo "<font color='red'>Age field is empty.</font><br/>";
}
if(empty($email)) {
echo "<font color='red'>Email field is empty.</font><br/>";
}
//link to the previous page
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
} else {
// if all the fields are filled (not empty)
//insert data to database
$result = mysqli_query($mysqli, "INSERT INTO users(name,age,email) VALUES('$name','$age','$email')") or die(mysql_error());
//display success message
echo "<font color='green'>Data added successfully.";
echo "<br/><a href='index.php'>View Result</a>";
}
}
?>
我读过很多帖子,但作为一个初学者,现在要深入研究javascript似乎太难了。如果这是唯一的解决方案,你可以教我-哈哈-如何实现脚本的几行。谢谢
1条答案
按热度按时间cpjpxq1n1#
您可以使用退出功能(http://php.net/manual/en/function.exit.php)
此时脚本执行将停止。