当我将文本框留空时,文本框旁边会显示错误。示例:需要输入。我有一个文件名交易表单 result.php,它定义了我所有的验证和 price.php,它插入到数据库中。它进入第一个 if 语句,如果没有错误,它允许插入到数据库中。但事实上,我提交了一个空表单,它会提示我所有的错误。它只是没有发生。即使有错误,它也会将表单提交给 price.php(insert)。所有答案表示赞赏
<?php
$selection = '';
$type = '';
$size = '';
$bidprice = '';
$offerprice = '';
$stoploss = '';
$takeprofit = '';
////////////////////////////////
$Error = '';
$selectionError = '';
$typeError = '';
$sizeError = '';
$bidpriceError = '';
$offerpriceError = '';
$stoplossError = '';
$takeprofitError = '';
$message = '';
$errors = array();
$noErrors = true;
$haveErrors = !$noErrors;
require_once('validations/tradeformresult.php');
if ($noErrors && $userArriveBySubmittingAForm) {
require_once('price.php');// INSERTION
echo "<script type='text/javascript'>\n";
echo "alert('Trade is successfully executed!');\n";
echo "</script>";
///////////MESSAGE/////////////////
}
else if ($haveErrors && $userArriveBySubmittingAForm) {
echo "<script type='text/javascript'>\n";
echo "alert('Please re-enter your parameters.);\n";
echo "</script>";
$message = "\t\t" . '<font color="red">Fail!</font><br />' . "\n";
$message = $message . "\t\t" . 'Validation errors : <br />' . "\n";
$message = $message . "\t\t" . '<ol>' . "\n";
foreach ($errors as $key=>$errorMessage) {
$message = $message . "\t\t\t" . '<li>' . $errorMessage . '</li>' . "\n";
if ($key == 'selection') {
$selectionError = $errorMessage;
}
if ($key == 'type') {
$typeError = $errorMessage;
}
if ($key == 'size') {
$sizeError = $errorMessage;
}
if ($key == 'bidprice') {
$bidpriceError = $errorMessage;
}
if ($key == 'offerprice') {
$offerpriceError = $errorMessage;
}
if ($key == 'stoploss') {
$stoplossError = $errorMessage;
}
if ($key == 'takeprofit') {
$takeprofitError = $errorMessage;
}
}
$message = $message . "\t\t" . '</ol>' . "\n";
}
else if ($userArriveByClickingOrDirectlyTypeURL) { // we put the original form inside the $message variable
$newTitle = 'The link is broken';
$h1Title = '';
$message = '';
}
?>
交易表格结果
$userArriveBySubmittingAForm = !empty($_POST);
// user arrives by GET
$userArriveByClickingOrDirectlyTypeURL = !$userArriveBySubmittingAForm;
// check if user arrives here via a POSTBACK
if ($userArriveBySubmittingAForm) {
$selectionNotGiven = empty($_POST['selection']);
// if name not given
if ($selectionNotGiven) {
// we add new error into $errors
$errors['selection'] = "Symbol is required";
}
$typeNotGiven = empty($_POST['type']);
// if name not given
if ($typeNotGiven) {
// we add new error into $errors
$errors['type'] = "Type is required";
}
$sizeNotGiven = empty($_POST['size']);
// if name not given
if ($sizeNotGiven) {
// we add new error into $errors
$errors['size'] = "Size is required";
}
$bidpriceNotGiven = empty($_POST['bidprice']);
// if name not given
if ($bidpriceNotGiven) {
// we add new error into $errors
$errors['bidprice'] = "Bid Price is required";
$offerpriceNotGiven = empty($_POST['offerprice']);
// if name not given
if ($offerpriceNotGiven) {
// we add new error into $errors
$errors['offerprice'] = "Offer price is required";
$stoplossInvalid = ($_POST['stoploss'])>($_POST['offerprice'])||($offernodecimal-$stoplossnodecimal)< 200
||($_POST['stoploss'])<($_POST['bidprice'])||($bidnodecimal-$stoplossnodecimal)< 200;
if ($stoplossInvalid) {
// we add new error into $errors
$errors['stoploss'] = "Stop Loss is invalid";
}
$takeprofitInvalid = ($_POST['takeprofit'])<($_POST['offerprice'])||($takeprofitnodecimal-$bidnodecimal)< 200
||($_POST['takeprofit'])>($_POST['bidprice'])||($offernodecimal-$takeprofitnodecimal)< 200;
if ($takeprofitInvalid) {
// we add new error into $errors
$errors['takeprofit'] = "Take Profit is invalid";
}
}
$noErrors = (count($errors) == 0);
// haveErrors is the opposite of noErrors
$haveErrors = !$noErrors;
if (!empty($_POST['selection'])) {
$selection = $_POST['selection'];
} // end if name NOT empty
if (!empty($_POST['type'])) {
$type = $_POST['type'];
} // end if name NOT empty
if (!empty($_POST['size'])) {
$size = $_POST['size'];
} // end if password NOT empty
if (!empty($_POST['bidprice'])) {
$bidprice = $_POST['bidprice'];
} // end if password NOT empty
if (!empty($_POST['offerprice'])) {
$offerprice = $_POST['offerprice'];
} // end if sex NOT empty
if (!empty($_POST['stoploss'])) {
$stoploss = $_POST['stoploss'];
} // end if sex NOT empty
if (!empty($_POST['takeprofit'])) {
$takeprofit = $_POST['takeprofit'];
} // end if diploma NOT empty
}
}
// end if no errors
/** end of proceed as normal **/
// @TODO code expected here to assign the variables in Ex1-5/registerform.php lines 10-17
// remember that $interests is an array, so we need to check $_POST['interests'] for empty and also ????
/** end of proceed as normal **/
?>