我还是一个新手,在php中仍在学习。我需要的是一旦用户在Age字段中输入了除整数以外的其他内容,而不提交给mysql,就停止该表单。这是我的基本形式和代码:
add.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。如果这是唯一的解决方案,那么您可以教我-大声笑-如何实现几行脚本。谢谢
答案 0 :(得分:0)
您可以使用退出功能(http://php.net/manual/en/function.exit.php)
if (!ctype_digit($age)){
echo "<font color='red'>ERREUR! age n'est PAS entier</font><br/>";
exit();
...
然后脚本执行将在此时停止。