在我的表单中验证错误

时间:2013-08-05 06:01:10

标签: java php html

当我将文本框留空时,我的文本框旁会显示错误。例如:类型是必需的。我有一个文件名trade form.php,它定义了我的所有验证和price.php,它插入到database.It进入第一个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 **/

?>

1 个答案:

答案 0 :(得分:0)

tradeformresult.php似乎有一些错位的结束括号。因此,如果没有给出出价,您只需设置$noErrors。从我所看到的,最后2个括号应该像这样向上移动:

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