java - 我的表单中存在验证错误

标签 java php html

当我将文本框留空时,文本框旁边会显示错误。示例:需要输入类型。我有一个文件名交易表单 result.php,它定义了我的所有验证和插入到数据库中的price.php。它将进入第一个 if 语句,如果没有错误,该语句允许插入到数据库中。但事实上,我提交了一个空表格,它会提示我所有的错误。它只是没有发生。即使有错误,它也会将表单提交到price.php(插入)。感谢所有答案

<?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 **/

?>

最佳答案

您在 tradeformresult.php 中似乎有几个放错位置的右大括号。因此,如果没有给出出价,您只需设置 $noErrors。据我所知,最后两个大括号应该像这样向上移动:

if ($bidpriceNotGiven) {

    // we add new error into $errors
    $errors['bidprice'] = "Bid Price is required";
} // <===== Moved from below

$offerpriceNotGiven = empty($_POST['offerprice']);

// if name not given
if ($offerpriceNotGiven) {

    // we add new error into $errors
    $errors['offerprice'] = "Offer price is required";
} // <===== Moved from below

关于java - 我的表单中存在验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18051487/

相关文章:

javascript - 如何克服 CodeIgniter 的不良做法

javascript - 使用 GPS 监测移动设备的位移

javascript - jQuery UI Accordion - 上一节的内容在点击不同部分时加载

java - 与 @RequestMapping 一起使用的自定义注释在 Spring Boot 中不起作用

java - 无法更新纬度和经度

java - Log4j2不同jar的不同日志

java - 如何识别已在可扩展 ListView 中单击的组 View ?

php - 显示 MySQL 中特定行的数据

javascript - 卡在 Ajax 表单到 mysql 提交上

javascript - 从文本类型的输入动态创建列表