Problem with nested if-else statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pradeep.thekkottil@gmail.com

    #1

    Problem with nested if-else statement

    I'm setting up an auction website using PHP and MySQL. There in the
    section where logged in members can put up new auction in a form, I
    want to run a form validation where I used if else statements to check
    the fileds filled. In the form page there are two radio buttons -
    fixed and auction - (only one can be chosen) and depend upon which one
    is chosen some text should be inserted in the text fields. For that
    I'm using a validation where this nested if else is not working
    properly. It checks until some if statements then won't check the rest
    of the if statements. The codes below I have reduced to relevant
    parts.

    names of
    radio buttons: groupname - 'rdoAuctionType ', with 'fixed',
    'auction'
    text fields:
    when 'fixed' button is selected, text field 'txtFixedPrice'
    must be filled
    when 'auction' button is selected, text fields
    'txtStartPrice' and 'txtIncrement' must be filled
    ---------------------------------------------------------------------------­------------------------------------------------------------


    Here is the code:
    auction_formval idation.php


    if(isset($_POST['btnConfirm'])) {
    .............
    .............
    $auctiontype = $_POST['rdoAuctionType '];
    $fixedprice = $_POST['txtFixedPrice'];
    $startprice = $_POST['txtStartPrice'];
    $bidincrement = $_POST['txtIncrement'];
    $duration = $_POST['txtDuration'];
    ...........
    if {
    .....
    }
    else if {
    ......
    }
    else if(trim($auctio ntype) != '') {
    if(trim($auctio ntype) == 'fixed') {
    if(trim($fixedp rice) == '') {
    $errmsg .= '<li>Please enter the fixed price.</li>';
    }
    else if(!isNumber($f ixedprice)){
    $errmsg .= '<li>Fixed price should only contain numbers.</
    li>';
    }
    }
    else if(trim($auctio ntype) == 'auction'){
    if(trim($startp rice) == '') {
    $errmsg .= '<li>Please enter the start price.</li>';
    }
    else if(!isNumber($s tartprice)) {
    $errmsg .= '<li>Start price should only contain numbers.</
    li>';
    }
    else if(trim($bidinc rement) == '') {
    $errmsg .= '<li>Please enter your bid increment.</li>';
    }
    else if(!isNumber($b idincrement)) {
    $errmsg .= '<li>Bid increment should only contain numbers.</
    li>';
    }
    }
    }
    else if(trim($durati on) == '') { <----------------- it doent check
    from here ownwards
    $errmsg .= '<li>Please enter the duration for the auction.</li>';
    }
    else if {
    .......
    }

    }


    Code of form page:
    add_auction.php

    <html>
    ..............
    ..............
    <form name="formRegis ter" action="add_auc tion.php" method="post">
    <fieldset>
    ...............
    ...............
    ...............
    <label>Auctio n Type:</label>
    <table border="0">
    <tr>
    <td valign="top"><i nput type="radio" name="rdoAuctio nType"
    value="fixed" <?if ($auctiontype == 'fixed') echo "checked";?
    >Fixed<br>Fix ed Price:<input type="textbox" name="txtFixedP rice"
    size="8" value="<?echo $fixedprice?>"> </td <td><input
    type="radio" name="rdoAuctio nType" value="auction" <?if ($auctiontype
    == 'auction') echo "checked";?>>;" >Auction<br>Sta rting Price:<input
    type="textbox" name="txtStartP rice" size="8" value="<? echo
    $startprice?>"> <br>Bid Increment:<inpu t type="textbox"
    name="txtIncrem ent" size="5" value="<?echo $bidincrement?> "></td>
    </tr>
    </table>

    <label>Duration :</label>
    <input type="textbox" size="4" name="txtDurati on" value="<?echo
    $duration?>"
    <select>
    <option value="day">Day (s)</option>
    <option value="week">We ek(s)</option>
    <option value="month">M onth(s)</option></select><br>
    ............... .
    ...............
    <table>
    <tr>
    <td>&nbsp;</td><td><input type="submit" name="btnConfir m"
    value="Confirm" ><input type="reset" name="btnReset" value="Reset"></
    td>
    </tr>
    </table>
    </fieldset>
    </form>
    ............
    .............
    </html>
    ---------------------------------------------------------------------------­------------------------------------------------------------

    I have posted this in some other group, but until now didn't get any
    reply. So I thought to put it here too. Am I in the right place? :s

    Could somebody help me? I would be thankful :)

  • Geoff Berrow

    #2
    Re: Problem with nested if-else statement

    Message-ID: <1179927455.108 253.312170@k79g 2000hse.googleg roups.comfrom
    Ravi contained the following:
    >use empty($duration ).and check weather that variable is existed or not
    >first.check it with isset($duration ).
    empty() does both. Not clear in the documentation but example 2503
    indicates this is the case.

    --
    Geoff Berrow (put thecat out to email)
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

    Comment

    • pradeep.thekkottil@gmail.com

      #3
      Re: Problem with nested if-else statement

      Sorry for late reply. My laptop got defect, and I couldnt do further
      on that work. Now borrowed my friends and continuing on my work.

      Thanks for the help. But I tried empty($duration ) but still no change.
      Then I don't understand why it works with other trim() statements and
      this one not. Are you sure the if-else statement is written correctly?

      Comment

      Working...