Jump to content

This function should work but it isn't doing it...


kenwvs

Recommended Posts

I have a function that should work for validation for a form field with a drop down box.  I am thinking it is set up properly, but it isn't working, but all the other functions on the form do work.

This function is suppose to find a problem with the validation if the field drop down box kept the original entry which says "Please select category.  This entry has a value of 000

This is what is called in the form (only listed a few of the categories as there are lots of them) and the function is below this.

require_once ('functions.php');
$categories = array("000" =>'Please Select Category',
                              "150" =>'Antiques',
                              113 =>"Arts and Crafts",
                                20 =>"Automobiles - Antique Vehicles",
                                18 =>"Automobiles - Cars",

This is the function for the above.

function ItemCategory($element)    {    // Verify Valid Category
  if($element == "000")              //assumes you gave it the value 000
    return FALSE; "Please select a valid category";
}

Can you see anything jumping out that would cause it not to work?

thanks,

Ken
Link to comment
Share on other sites

i would suggest trying:

[code]$element == '000';[/code]

if you use single quotes, it will be considered a literal and will not be ambiguous in any way.  i assume because you're using double quotes, it considers it equivalent to 0.  this is the only problem i can think of.

as for the === operator, it's silly to use this on anything returned by POST.  everything returned by POST is a string, regardless of its format pre-submission.

[b]EDIT:  joehaley caught what we all should have.  i'll leave this here as a little lesson on literals.[/b]
Link to comment
Share on other sites

As I didn't write this myself, but had someone write it for me (it is well beyond my capabilities), he has since given me some more code to add, but now it will catch that there is an error, but finds an error with it, regardless of what category is selected.... need to find out why it won't accept any codes, should only cause a problem with "000".  I appreciate any ideas as the guy who wrote it is busy at work, and I would like to get this working.

This is the function that goes with the code, for this area, not including all functions....

[code]
function ItemCategory($element)    {    // Verify Valid Category
  if($element == "000")              //assumes you gave it the value 000
    return FALSE; "Please select a valid category";
}
[/code]

[code]
<?php
/* sample.php */

require_once ('functions.php');
$categories = array("000" =>'Please Select Category',
                              "150" =>'Antiques',
                              113 =>"Arts and Crafts",
                                20 =>"Automobiles - Antique Vehicles",
                                18 =>"Automobiles - Cars",
                                21 =>"Automobiles - Hot Rod Vehicles",
                                19 =>"Automobiles - Trucks",
                                52 =>"Automotive Parts and Accessories - New Parts",
                                51 =>"Automotive Parts and Accessories - Used Parts",
                              134 =>"Automotive Stereos and Radar Detectors",
                              140 =>"Bicycles - Single Speed",
                              141 =>"Bicycles - 10 Speed and Under",
                              142 =>"Bicycles - 18 Speed",
                              143 =>"Bicycles - Over 18 Speed",
                              144 =>"Bicycles - Unicycles",
                              145 =>"Bicycles - Scooters",
                              146 =>"Bicycles - Childrens Trikes",
                              147 =>"Bicycles - Children Bikes with Training Wheels",
                                84 =>"Cameras - Digital Camera",
                                85 =>"Cameras - Camcorders",
                                86 =>"Cameras - Others",
                              167=>"Collector Coins",
                              149 =>"Community Meetings",
                                87 =>"Computers and Accessories - Monitors",
                                88 =>"Computers and Accessories - Printers",
                                89 =>"Computers and Accessories - CPU's",
                                90 =>"Computers and Accessories - Complete Systems",
                                91 =>"Computers and Accessories - Accessories",
                              103 =>"Computers and Accessories - Services",
                              105 =>"Construction - Services",
                              106 =>"Construction - Equipment",
                              107 =>"Construction - Tools",
                              109 =>"Construction - Products - New",
                              110 =>"Construction - Products - Used",
                                74 =>"Employment Ads - General",
                              120 =>"Employment Ads - General Labourer",
                                72 =>"Employment Ads - Professional",
                              119 =>"Employment Ads - Restaurant",
                              118 =>"Employment Ads - Retail",
                                73 =>"Employment Ads - Trades",
                                66 =>"Farm / Livestock / Equipment - Feed",
                                65 =>"Farm / Livestock / Equipment - Livestock",
                                64 =>"Farm / Livestock / Equipment - Mowers",
                                63 =>"Farm / Livestock / Equipment - Tillers",
                                62 =>"Farm / Livestock / Equipment - Tractors",
                              168 =>"Health and Wellness",
                                96 =>"Home Electronics - DVD and VCR",
                                92 =>"Home Electronics - Gaming Machines",
                                97 =>"Home Electronics - MP3, Discman, etc.",
                              135 =>"Home Electronics - Telephone",
                                77 =>"Home and Garden - Gardening Tools and small equipment",
                                75 =>"Home and Garden - Lawnmowers",
                                76 =>"Home and Garden - Rototillers",
                                78 =>"Home and Garden - Services",
                                79 =>"Home and Garden - Sheds",
                              154 =>"Household Goods - Appliances",
                              155 =>"Household Goods - Bar - B - Q",
                              156 =>"Household Goods - Bedroom Furniture",
                              152 =>"Household Goods - Furniture",
                              151 =>"Household Goods - Kitchen",
                              153 =>"Household Goods - Small Appliances",
                              154 =>"Household Goods - Appliances",
                                61 =>"Houses for Sale - Acreages",
                                35 =>"Homes for Sale - Condo",
                                33 =>"Homes for Sale - Duplex",
                                32 =>"Homes for Sale - Houses",
                                36 =>"Homes for Sale - Mobile Homes",
                                34 =>"Homes for Sale - Townhouse",
                                71 =>"Housing Rentals - Acreages",
                                39 =>"Housing Rentals - Apartments",
                                42 =>"Housing Rentals - Condo",
                                44 =>"Housing Rentals - Duplex",
                                40 =>"Housing Rentals - Houses",
                                43 =>"Housing Rentals - Mobile Home",
                                41 =>"Housing Rentals - Townhouse",
                              132 =>"Jewellery and Watches - Beaded Jewellery",
                              131 =>"Jewellery and Watches - Rings",
                              133 =>"Jewellery and Watches - Engagement and Wedding",
                              130 =>"Jewellery and Watches - Watches",
                                53 =>"Motorcycles - ATV",
                                22 =>"Motorcycle - OFF ROAD",
                                23 =>"Motorcycle - Street Legal",
                                54 =>"Motorcycle Parts and Accessories - Apparel",
                                56 =>"Motorcycle Parts and Accessories - Boots",
                                58 =>"Motorcycle Parts and Accessories - Equipment and Tools",
                                55 =>"Motorcycle Parts and Accessories - Helmuts",
                                59 =>"Motorcycle and ATV Parts - New",
                                60 =>"Motorcycle Parts and Accessories - Used",
                              137 =>"Personal Care Products",
                              121 =>"Pets",
                                27 =>"Recreational Vehicle - 5th Wheels",
                                26 =>"Recreational Vehicle - Motor Homes",
                                24 =>"Recreational Vehicle - Tent Trailer",
                                25 =>"Recreational Vehicle - Trailers",
                              159 =>"Renovations - Bathroom",
                              162 =>"Renovations - Bedroom",
                              164 =>"Renovations - Electrical",
                              160 =>"Renovations - Kitchen",
                              161 =>"Renovations - Living Areas",
                              163 =>"Renovations - Plumbing",
                              165 =>"Renovations - Wholesalers",
                              165 =>"Services - Child Care",
                              127 =>"Services - Moving and Storage",
                              122 =>"Services - Professional",
                              123 =>"Services - Tradesman",
                              126 =>"Services - Yard Care and Snow Removal",
                              166 =>"Services - Renovation Contractors",
                                95 =>"Stereos - All in One",
                                94 =>"Stereos - Components",
                                93 =>"Stereos - Speakers",                                                                            98 =>"Televisions - Flat Screen",
                                98 =>"Televisions - Flat Screen",
                                99 =>'Televisions - Under 20"',
                              100 =>"Televisions - 20 inch and over",
                              101 =>"Televisions - Plasma",
                              102 =>"Televisions - Projection",
                              148 =>"Tools",
                              138 =>"Toys",
                                46 =>"Upcoming Events - Coaldale",
                                47 =>"Upcoming Events - Coalhurst",
                                49 =>"Upcoming Events - Fort McLeod",
                                45 =>"Upcoming Events - Lethbridge",
                                48 =>"Upcoming Events - Taber",
                                50 =>"Upcoming Events - Surrounding Areas",
                              128 =>"WANT ADS");
$valid = TRUE;
$err = array();
$err_msg = array();
$funcs = array('id'=>'LettersAndDigits',
                                        'item_title' => 'Variable',
                                        'item_category'=>'ItemCategory',
                                        'item_type' => 'Variable',
                                        'quantity_available' => 'isDigits',
                                        'starting_bid' => 'Dollars',
                                        'bid_increment' => 'BidIncrement',
                                        'reserve_price' => 'ReservePrice',
                                        'duration' => 'isDigits',
                                        'end_time' => 'isDigits',
                                        'auto_relist' => 'isLetters',
                                        'city' => 'Variable',
                                        'state' => 'Variable',
                                        'country' => 'Variable',
                                        'item_description' => 'Variable',
                                        'paypal_id' => 'EmailorEmpty',
                                        'hit_counter' => 'Variable');
$hc = array(0=> 'No Counter',
            1=> 'Style 1',
            2=> 'Style 2',
            3=> 'Style 3');
$itemType = array(1=> 'Auction',
                  2=> 'Dutch Auction',
                  3=> 'Fixed Price',
                  4=> 'Classified Ad');
$countries = array(32=>'Canada',210=>'USA');
$yesno = array(1=>'Yes',0=>'No');
$hours = array(24 =>'12:00 a.m.',
                                        1 =>'1:00 a.m.',
                                        2 =>'2:00 a.m.',
                                        3 =>'3:00 a.m.',
                                        4 =>'4:00 a.m.',
                                        5 =>'5:00 a.m.',
                                        6 =>'6:00 a.m.',
                                        7 =>'7:00 a.m.',
                                        8 =>'8:00 a.m.',
                                        9 =>'9:00 a.m.',
                                        10 =>'10:00 a.m.',
                                        11 =>'11:00 a.m.',
                                        12 =>'12:00 p.m.',
                                        13 =>'1:00 p.m.',
                                        14 =>'2:00 p.m.',
                                        15 =>'3:00 p.m.',
                                        16 =>'4:00 p.m.',
                                        17 =>'5:00 p.m.',
                                        18 =>'6:00 p.m.',
                                        19 =>'7:00 p.m.',
                                        20 =>'8:00 p.m.',
                                        21 =>'9:00 p.m.',
                                        22 =>'10:00 p.m.',
                                        23 =>'11:00 p.m.');

if (isset ($_POST['Submit'])) {
    foreach($_POST as $key=>$val) {
                if ($key != 'Submit')
                        switch ($funcs[$key]) {
                                case 'LettersAndDigits':
                                        if (!LettersAndDigits($val)) $err[] = $key;
                                        break;
                                case 'Variable':
                                        if (!Variable($val)) $err[] = $key;
                                        break;
                                case 'Dollars':
                                        if (!Dollars($val)) $err[] = $key;
                                        break;
                                case 'ItemCategory':
                                        $ic = ItemCategory($val);
                                        if (!$ic[0]) {
                                        $err[] = $key;
                                        $err_msg[$key] = $ic[1];
                                        }
                                        break;
                                case 'isDigits':
                                        if (!isDigits($val)) $err[] = $key;
                                        break;
                                case 'EmailorEmpty':
                                        if (!EmailorEmpty($val)) $err[] = stripslashes($key);
                                        break;
                                case 'BidIncrement':
                                        $bi = BidIncrement($val,$itemType[$_POST['item_type']]);
                                        if (!$bi[0]) {
                                                $err[] = $key;
                                                $err_msg[$key] = $bi[1];
                                        }
                                        break;
                                case 'ReservePrice':
                                        $rp = ReservePrice($val,$itemType[$_POST['item_type']]);
                                        if (!$rp[0]) {
                                                $err[] = $key;
                                                $err_msg[$key] = $rp[1];
                                        }
                                        break;
                                }
    }
        if (!empty($err)) $valid = false;
}

function disp_val($idx) {
        $retv = '';
        if (isset($_POST[$idx])) $retv = stripslashes($_POST[$idx]);
        return($retv);
}

function is_selected($it,$val,$def='') {
        if (isset($_POST[$it]))
                $ret =($_POST[$it] == $val)?' selected ':'';
        else
                $ret = ($val == $def)?' selected':'';
        return($ret);
}

function disp_options($opt_array,$fld,$def,$for_type) {
        $tmp = array();
        if ($for_type)
                foreach ($opt_array as $key=>$val)
                        $tmp[] = '<option value="' . $key . '"' . is_selected($fld,$key,$def) . '>' . $val . '</option>';
        else
                for ($i=0;$i<count($opt_array);$i++)
                        $tmp[] = '<option value="' . $i . '"' . is_selected($fld,$i,$def) . '>' . $opt_array[$i] . '</option>';
        echo "\t\t" . implode("\n\t\t",$tmp) . "\n";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title>For Sale 4 U QUICK LISTER</title>
<style type="text/css">
body {
        background-color: Gainsboro;
        margin: 0;
        padding: 0;
        font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
        font-size: 72%;
        color: black;
}
table {
        width: 96%;
        display: block;
        margin-left: auto;
        margin-right: auto;
}

form {
        width: 98%;
        display: block;
        margin-left: auto;
        margin-right: auto;
}
.red {
        color: Red;
}
.error {color:#C03; font-weight:bold; }

h3 {
        text-align: center;
        color: Red;
        font-weight: bold;
}
h4 {
        text-align: center;
        color: Blue;
        font-weight: bold;
}
.center {
        text-align: center;
}

.bold {
        font-weight: bold;
}
</style>
</head>
<body>
<h3>Quick Lister</h3>
<p class="center"><span class="red">For Sale 4 U - Canada's Online Auction</span></p>
<p class="center"><span class="bold">Please note:&nbsp; </span>You may
not use characters such as $, #, &quot;, &amp;, *,/, etc.&nbsp; It is best to use normal
text and numerical characters only.&nbsp;<BR><BR><BR>
<?php
$trace = array();
if (!$valid) {
?>
<h4>Please correct the items in <span class="error">red</span> and resubmit.<br /><br /> </h4>
<?php
}
?>
<HR>
<form method="POST" action="sample.php">
<table>
<tr>
    <td valign="top" <?php if(in_array('id',$err)) echo 'class="error"'; ?>> User ID:</td>
    <td><input type="text" name="id" size="12" value="<?php echo  disp_val('id') ?>"><br>
    You must use the <span class="bold">same User ID</span> for all items you are uploading during this session.(It does not have to be your regular username.)</td>
</tr>
<tr>
    <td <?php if(in_array('item_title',$err)) echo 'class="error"'; ?>>  Item Title:</td>
    <td><input type="text" name="item_title" size="60" value="<?php echo  disp_val('item_title'); ?>"</td>
</tr>
<tr>
    <td <?php if(in_array('item_category',$err)) echo 'class="error"'; ?>>Item Category:</td>
    <td> <select size="1" name="item_category">
        <?php disp_options($categories,'item_category','',true) ?>
          </select>
          <? if (isset($err_msg['item_category'])) echo '&nbsp;<span
          class="error">' . $err_msg['item_category'] . '</span>'; ?>
          </td></p>
</tr>
<tr>
    <td <?php if(in_array('item_type',$err)) echo 'class="error"'; ?>>Item Type:</td>
    <td><select size="1" name="item_type">
        <?php
                        $tmp = array();
                        for ($i=1;$i<count($itemType)+1;$i++)
                                $tmp[] = '<option value="' . $i . '"' . is_selected('item_type',$i,0) . '>' . $itemType[$i] . '</option>';
                        echo "\t\t" . implode("\n\t\t",$tmp) . "\n";
        ?>
    </td></select>
</tr>
<tr><td <?php if(in_array('quantity_available',$err)) echo 'class="error"'; ?>>Quantity:</td>
    <td><input type="text" name="quantity_available" size="3" value="<?php echo  disp_val('quantity_available') ?>"</td></tr>
</tr>
<tr>
    <td <?php if(in_array('starting_bid',$err)) echo 'class="error"'; ?>>Starting Bid:</td>
    <td> <input type="text" name="starting_bid" size="7" value="<?php echo  disp_val('starting_bid') ?>"></td>
</tr>
<tr>
    <td <?php if(in_array('bid_increment',$err)) echo 'class="error"'; ?>>Bid Increment (auction only):</td>
    <td><input type="text" name="bid_increment" size="7" value="<?php echo  disp_val('bid_increment') ?>"><? if (isset($err_msg['bid_increment'])) echo '&nbsp;<span class="error">' . $err_msg['bid_increment'] . '</span>'; ?></td>
</tr>
<tr>
    <td <?php if(in_array('reserve_price',$err)) echo 'class="error"'; ?>>Reserve Bid&nbsp;(auction only ):</td>
    <td><input type="text" name="reserve_price" size="7" value="<?php echo  disp_val('reserve_price') ?>"><? if (isset($err_msg['reserve_price'])) echo '&nbsp;<span class="error">' . $err_msg['reserve_price'] . '</span>'; ?></td>
</tr>
<tr>
    <td <?php if(in_array('duration',$err)) echo 'class="error"'; ?>>Ad Duration:</td>
    <td><select size="1" name="duration">
        <?php $tmp = array();
                for ($d=1;$d<31;$d++)
                        $tmp[] = '<option value="' . $d . '"' . is_selected('duration',$d,'14') .'>' . $d . '</option>';
                echo "\t\t" . implode("\n\t\t",$tmp) . "\n";
                ?>
        </select>
    </td>
</tr>
<tr>
    <td <?php if(in_array('end_hour',$err)) echo 'class="error"'; ?>>End Time:</td>
    <td> <select size="1" name="end_hour">
        <?php disp_options($hours,'end_hour',20,true); ?>
        </select>
    </td>
</tr>
    <tr><td <?php if(in_array('auto_relist',$err)) echo 'class="error"'; ?>>Auto Relist:</td>
    <td><select size="1" name="auto_relist">
        <?php disp_options($yesno,'auto_relist',1,true); ?>
        </select>
    </td>
</tr>
<tr>
    <td <?php if(in_array('city',$err)) echo 'class="error"'; ?>>City:</td>
    <td><input type="text" name="city" size="15" value="<?php echo  disp_val('city') ?>"> </td>
</tr>
<tr>
    <td <?php if(in_array('state',$err)) echo 'class="error"'; ?>>Province/State:</td>
    <td><input type="text" name="state" size="10" value="<?php echo  disp_val('state') ?>"></td>
</tr>
<tr>
    <td <?php if(in_array('country',$err)) echo 'class="error"'; ?>>Country:</td>
    <td><select size="1" name="country">
                <?php disp_options($countries,'country',32,true) ?>
                </select>
    </td>
</tr>
<tr>
    <td valign="top" <?php if(in_array('item_description',$err)) echo 'class="error"'; ?>>Item Description:</td>
    <td colspan=8><textarea rows="4" name="item_description" cols="116"><?php echo  disp_val('item_description') ?></textarea></td>
</tr>
<tr>
      <td <?php if(in_array('paypal_id',$err)) echo 'class="error"'; ?>>Paypal ID (Optional):</td>
                <td><input type="text" name="paypal_id" size="25" value="<?php echo  disp_val('paypal_id') ?>">
</tr>
<tr>
      <td <?php if(in_array('hit_counter',$err)) echo 'class="error"'; ?>>Hit Counter:</td>
                <td><select size="1" name="hit_counter">
                <?php disp_options($hc,'hit_counter',0,true); ?>
        </select>
    </td>
</tr>
</tr>
        <td colspan="2"><hr></td>
<tr>
<tr>
    <td colspan="5" class="center">
        <span class="bold"><br>Please verify all details shown for your item before pressing the SAVE button.</span>
    </td>
</tr>
<tr>
    <td colspan="5" class="center"><br><input type="submit" name="Submit" value="SAVE  ITEM"></td>
</tr>
<tr>
    <td colspan="2" class="center"><BR>You will be guided through this Multi Step Process.  Please be sure
        to use the <span class="bold">same User ID</span> when entering your items or they will not all
        be listed correctly.</font></p>
    </td>
</tr>
</table>
</form>
</body>
</html>
[/code]

Thanks,

Ken
Link to comment
Share on other sites

When you push the submit button, it should validate the fields.  this field, if it still has the "Please Select Category" would produce an error and would then require you to select a valid category.  It isn't that you get an actual "error", it just won't validate properly.  Even if you have a valid entry, it still produces the error, and is looking for a valid entry.

Ken
Link to comment
Share on other sites

try this instead:

[code]<?php
function ItemCategory($element)    {    // Verify Valid Category
  if($element == "000")              //assumes you gave it the value 000
    return array(FALSE, "Please select a valid category");
  else
    return array(TRUE);
}
?>[/code]

your switch statement is expecting an array returned from ItemCategory(), and it sure wasn't returning an array.  an unset return value will also be considered FALSE, so in the case that it's valid, you still need to return something.
Link to comment
Share on other sites

that appears to have solved the problem.  Thank You very much.  It is greatly appreciated.  I am actually going to have a form that will make sure the info is filled out..... it has been in progress for a week, as I really don't know what I am doing, and trying to learn.  Now to format the appearance of the form, and it will be ready to use.  That may be a challenge yet, but not a PHP challenge..... Thanks again, muchly appreciated.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.