You are here: Re: php form code best practise « All PHP « IT news, forums, messages
Re: php form code best practise

Posted by Barry Morris on 03/12/06 11:28

Thank you for the tips and example code I especially like the if ('fred' ==
$STRING) { that would have caused a lot less grief.

Barry

"J.O. Aho" <user@example.net> wrote in message
news:47dvkpFe88lsU1@individual.net...
> Barry Morris wrote:
>> Hi
>>
>> I am a php newbie although I have been a programmer for years, this can
>> be dangerous because all the languages I know use = as equal comparison
>> so it took me a long time to debug if ($STRING = 'fred') { not realising
>> I was assigning fred to $STRING and the evaluation is always true! It
>> took ages to discover if ($STRING = = 'fred') {.
>
> There is a movement that when comparing and have a static string as in
> your example, then always put it first, as this will create an arror if
> you do by mistake assign the value instead of compare and you easier have
> a check when you are assigning and not.
>
> if ('fred' == $STRING) {
>
>
>> Combobox selection: How do you set a default or remember what the user
>> selected so the form is re-displayed with their selection. I did this:
>> Item1 = '< -- Select a Country --> as "selected", I then changed the text
>> to the actual selection using echo if the form is re-displayed, Question:
>> How do you dynamically set a default or dynamically move the SELECTED
>> tag?
>
> You need to store the value somewhere, in a database or cookie are two
> qutie good options. For a drop down list, I think the best is to loop
> through an array, this way you don't have to write so much code as you
> otherwise would
>
> function write_preselected_dropdown($select_name,$list,$selectedvalue) {
> echo "<select name=\"$select_name\">\n";
>
> for($i=0;$i<count($list);$i++) {
> echo "<option";
> if($list[$i]==$selectedvalue) {
> echo " selected";
> }
> echo ">".$list[$i]."</option>\n";
> }
> echo "</select>\n";
> }
>
> $list_of_countries=array("Afghanistan","Albania","Algeria");
> write_preselected_dropdown("country",$list_of_countries,$_COOKIE['country']);
>
> This assumes we have stored the country in a cookie variable called
> "country", we don't make any checks that it's really set, if not set then
> the if-statement will anyway get false and the selected option won't be
> used.
>
>
>> <option selected><?echo $COUNTRY?></option>
>> <option>Afghanistan</option>
>> <option>Albania</option>
>> <option>Algeria</option>
>>
>>
>> Radio Buttons: I assigned each with a <? echoDEFAULT1 ?>, <? echo
>> DEFAULT2 ?> etc and set each to "" except for the default I really want,
>> this is set to "checked", surely I am not doing this the right way?
>
> You need to set an if-statement kind of like the one used for the dropdown
> list, if you can manage to make a loop of what you type out, then you can
> just add an easy check in that loop to type out the "checked" to the right
> value, otherwise you have to rewrite the check code for each radio button.
>
>
> //Aho

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация