我使用以下代码添加值
$debdes = $_POST['debdes'];
$debamt = $_POST['debamt'];
$crdes = $_POST['crdes'];
$cramt = $_POST['cramt'];
$date = $_POST['date'];
include_once ("db.php");
$ucbook = "INSERT INTO cbook(debdes,debamt,crdes,cramt,date) VALUES ('$debdes','$debamt','$crdes','$cramt','$date');";
现在我希望如果“debamt”被填充“debdes”不应该是空的,如果“cramt”被填充,那么“crdes”不应该是空的,同时按下提交按钮,所以如果有人按下提交按钮它显示一条消息说首先填写“debdes”。
答案 0 :(得分:3)
您正在寻找的是一些基本的表单验证:
$debdes = $_POST['debdes'];
$debamt = $_POST['debamt'];
$crdes = $_POST['crdes'];
$cramt = $_POST['cramt'];
$date = $_POST['date'];
include_once ("db.php");
$errors = array();
// Check for error conditions
if(!empty($debamt) && empty($debdes)) {
$errors[] = 'Fill also debdes';
}
if(!empty($cramt) && empty($crdes)) {
$errors[] = 'Fill also crdes';
}
// If no errors were found, execute the query
if(count($errors) == 0) {
$ucbook = "INSERT INTO cbook(debdes,debamt,crdes,cramt,date) VALUES ('$debdes','$debamt','$crdes','$cramt','$date');";
} else {
// If there are errors, display them:
echo implode('<br />', $errors);
}
答案 1 :(得分:2)
在上一个表单本身上,您可以使用“提交”按钮触发javascript函数,如下所示: -
function validate(){
var debamt=document.*form_name*.debamt.value;
var debdes=document.*form_name*.debdes.value;
var error_message;
if(debamt!=null && debdes==null){
error_message = 'Please fill debdes value first!!!';
}
var cramt=document.*form_name*.cramt.value;
var crdes=document.*form_name*.crdes.value;
if(cramt!=null && crdes==null){
error_message = '\nPlease fill crdes value first!!!';
}
alert(error_message);
}
答案 2 :(得分:1)
if( ! empty($_POST['debdes']) && empty($_POST['debdes']))
die('fill "debdes" first');
对于cramt和crdes来说同样如此。
答案 3 :(得分:1)
$debdes = $_POST['debdes'];
$debamt = $_POST['debamt'];
$crdes = $_POST['crdes'];
$cramt = $_POST['cramt'];
$date = $_POST['date'];
if ( !empty($debamt) && empty($debdes) ) {
show_error('debdes');
}
if ( !empty($cramt) && empty($crdes) ) {
show_error('drdes');
}
function show_error($string) {
echo "Value '$string' must be filled.";
exit;
}
include_once ("db.php");
$ucbook = "INSERT INTO cbook(debdes,debamt,crdes,cramt,date)
VALUES ('$debdes','$debamt','$crdes','$cramt','$date');";