0

Hi,
When i leave my text boxes empty , there will be errors that will be displayed beside my text boxes.Example: type is required. I have a file name trade form result.php which defines all my validations and price.php which inserts in to database.It is going into the first if statement which allows inserting into database if there are no errors. But in fact, i submitted an empty form which will prompt all my errors. It is just not happening. It submits the form to price.php( insert) even if there are errors. All answers appreciated

Tradeform.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 = '';
                    }
                ?>
                <!DOCTYPE html>
                <html>
                <head>
                <style type="text/css">

                div#overlay {
                    display: none;
                    z-index: 2;
                    background: #000;
                    position: fixed;
                    width: 100%;
                    height: 100%;
                    top: 0px;
                    left: 0px;
                    text-align: center;
                }
                div#specialBox {
                    display: none;
                    position: relative;
                    z-index: 3;
                    p.padding;

                    padding-top:25px;
                    padding-bottom:25px;
                    padding-right:50px;
                    padding-left:50px;

                    margin: 150px auto 0px auto;
                    width: 500px; 
                    height: 500px;
                    background: #FFF;
                    color: #000;
                }
                div#wrapper {
                    position:absolute;
                    top: 0px;
                    left: 0px;
                    padding-left:24px;
                }
                </style>
                <script type="text/javascript">
                function toggleOverlay(){
                    var overlay = document.getElementById('overlay');
                    var specialBox = document.getElementById('specialBox');
                    overlay.style.opacity = .8;
                    if(overlay.style.display == "block"){
                        overlay.style.display = "none";
                        specialBox.style.display = "none";
                    } else {
                        overlay.style.display = "block";
                        specialBox.style.display = "block";
                    }
                }
                </script>
                </head>
                <body>
                <!-- Start Overlay -->
                <div id="overlay"></div>
                <!-- End Overlay -->
                <!-- Start Special Centered Box -->
                <div id="specialBox">
                <p>Create Order
                  <p><?php
                $timestamp=time(); require_once 'start.php';
                ?>

                <!DOCTYPE html>

                <head><meta charset="UTF-8"></head>
                <body>

                <form action="tradeform.php" method="post" > 
                <input type="hidden" name="formSubmitted" value="true">
                <?php echo $message; ?>
                <?php ?>
                <?php if ($haveErrors || $userArriveByClickingOrDirectlyTypeURL) : ?>
                    <fieldset>
                <p>Symbol : <select name = "selection" id="selection">
                <option disabled = "disabled" selected = "selected"> Choose one </option>
                <option value="eur/usd">EUR/USD</option>
                <option value="usd/jpy">USD/JPY</option>
                <option value="usd/cad">USD/CAD</option>
                <option value="eur/jpy">EUR/JPY</option>
                <option value="eur/chf">EUR/CHF</option>
                <option value="gbp/usd">GBP/USD</option>
                <option value="aud/usd">AUD/USD</option>
                <option value="usd/chf">USD/CHF</option>

                </select><font color="red"><?php echo $selectionError?></font></p>
                        </fieldset>  <fieldset>
                <p> Date : <input type="datetime" value="<?php echo date("Y-m-d ",$timestamp); ?>"READONLY name="date"/></p>
                <p> Type : <input type="radio" name="type" value="buy">Buy <input type="radio" name="type" value="sell">Sell<font color="red"><?php echo $typeError;         // @TODO code expected here echo what error message? ?></font></p>
                <p> Size : <input type="number"pattern="[0-9]+([\.|,][0-9]+)?" step="0.01"name="size"/><font color="red"><?php echo $sizeError?></font></p>
                <p> Bid Price : <input id="bidprice" READONLY name="bidprice" type="text" value=" "> 
                Offer Price<input id="offerprice" READONLY name="offerprice" type="text" value=" "><font color="red"><?php echo $bidpriceError?><?php echo $offerpriceError?></font> </p>
                <p> Stop Loss : <input type="number"step="any" name="stoploss"/><font color="red"><?php echo $stoplossError?></font></p>
                <p> Take Profit : <input type="number"step="any"name="takeprofit"/><font color="red"><?php echo $takeprofitError?></font></p>
                </fieldset>
                <div align="center">
                <input type="submit" value="Submit"/>
                </div>
                <?php endif; ?>
                </form>

                 <script type="text/javascript"> 

                 window.onload=function() {  
                  if (document.getElementById) { 
                 document.getElementById("selection").onchange=function() { switchme(this); }  
                  }  
                 } 

                  function switchme(SNewSel) { 

                 var ind = SNewSel.selectedIndex; 
                 var txt = document.getElementById('bidprice'); 
                 var txt2 = document.getElementById('offerprice'); 
                  switch (ind) { 
                  case 1: // car purchase 
                  txt.value = <?php echo json_encode($bid); ?>; 
                   txt2.value = <?php echo json_encode($bid1); ?>; 
                  break; 
                  case 2: // other purchase 
                  txt.value = <?php echo json_encode($bid2); ?>;
                  txt2.value = <?php echo json_encode($bid3); ?>;

                  break; 
                 case 3: // other purchase 
                 txt.value = <?php echo json_encode($bid4); ?> ;
                  txt2.value = <?php echo json_encode($bid5); ?>;
                 break; 
                 case 4: // other purchase 
                     txt.value = <?php echo json_encode($bid6); ?> ;
                      txt2.value = <?php echo json_encode($bid7); ?>;
                     break; 
                 case 5: // other purchase 
                     txt.value = <?php echo json_encode($bid8); ?> ;
                      txt2.value = <?php echo json_encode($bid9); ?>;
                     break; 
                 case 6: // other purchase 
                     txt.value = <?php echo json_encode($bid10); ?> ;
                      txt2.value = <?php echo json_encode($bid11); ?>;
                     break; 
                 case 7: // other purchase 
                     txt.value = <?php echo json_encode($bid12); ?> ;
                      txt2.value = <?php echo json_encode($bid13); ?>;
                     break; 
                 case 8: // other purchase 
                     txt.value = <?php echo json_encode($bid14); ?> ;
                      txt2.value = <?php echo json_encode($bid15); ?>;
                     break; 

                 default: 
                 txt.value=''; 
                 break; 
                 } 
                 } 
                 </script> 



                 <button onmousedown="toggleOverlay()">Close </button>
                </body>
                </html></p>

                </div>
                <!-- Start Special Centered Box -->
                <!-- Start Normal Page Content -->
                <div id="wrapper">
                  <h2>Trade</h2>

                  <button onmousedown="toggleOverlay()">Create Order</button>

                </div>

                <!-- End Normal Page Content -->
                </body>
                </html>







`

Tradeformresult.php

     <?php

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




                    ?>
2
Contributors
2
Replies
33
Views
4 Years
Discussion Span
Last Post by nigelhow
0

This looks pretty verbose to me for a simple validator. Loads of error variables, where perhaps you just need the one (array) and then check to see if it's empty or not?

Aren't there missing braces here:

                        if ($bidpriceNotGiven) {
                            // we add new error into $errors
                            $errors['bidprice'] = "Bid Price is required";
                            $offerpriceNotGiven = empty($_POST['offerprice']);
                        // if name not given
                        //HERE MAYBE?
                        if ($offerpriceNotGiven) {
                            // we add new error into $errors
                            $errors['offerprice'] = "Offer price is required";
                            //HERE MAYBE?
                        $stoplossInvalid = ($_POST...

Also there are many initialisations of variables, e.g. $bidnodecimal, that aren't shown - which makes it difficult to follow the code.

Tip - try to declutter the code as much as possible - strip out any CSS and any js not directly related to the issue. You've got over 400 lines of code/markup here which could be cut considerably. Also the indenting (or initial margin) is a bit extreme!

Edited by diafol

0

Hi diafol , that actually worked for me . the text managed to display. sorry im not too good at css java script and stuff. But okay , lets say i need the div ( special box ) to stay after submission. which means after submission , the form stays and the red error text appears beside the text box. is that possible?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.