Re: form validation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 12 August 2011 16:42, Chris Stinemetz <chrisstinemetz@xxxxxxxxx> wrote:
> I have a select menu created by a foreach loop. I am trying to
> validate that there was a selection made before it is submitted to the
> database. But I am not doing something correctly.
>
> <select name="market" class="ajax"
> onchange="javascript:get(this.parentNode);">
> <option value="">Choose...</option>
> <?php
>    foreach($market_prefix as $key => $value)
>        {
>        $selected = '';
>        if($value == $market)
>        {
>        $selected = 'selected';
>        }
>        echo '<option value="', htmlspecialchars($value), '" ', $selected,
> '>', htmlspecialchars($market_name[$key]), '</option>';
>        }
>        ?>
> </select>
>
> I am using the folling on the posted page.
>
> if (! array_key_exists($_POST['market'], $market_name))
> {
>    echo "You did not select a market.";
> }
>
> Thank you,
>
> Chris

$_POST['market'] won't exist if you haven't chosen one.

Turn on your error reporting and you should see something appropriate.

At a bare minimum, adding ...

isset($_POST['market'])

as the first thing to test (before seeing if the value is in
$market_name (though I would have thought $market_names would have
been a better name for the variable - implies more than 1 market).


-- 
Richard Quadling
Twitter : EE : Zend : PHPDoc
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux