Re: HTTP_POST_VARS not picking up checkboxes that are unchecked

From: Date: Wed, 20 Jun 2001 14:17:02 +0000
Subject: Re: HTTP_POST_VARS not picking up checkboxes that are unchecked
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-54536@lists.php.net to get a copy of this message
"Tom Beidler" <tom@orbittechservices.com> wrote in message news:B753E0F5.238B%tom@orbittechservices.com... > I have a form with approximately 40 input fields. When a form is posted it > sends an email and then builds a tab delimited $txt entry for a text file on > the server. > > The problem is that if a checkbox is not checked, when it's not required, I > don't get a blank entry in my tab file for the checkbox input. I'm imploding (snip)> > Is there a way to send a blank value if the checkbox is not set? Should I > use a completely different strategy? Have I said Thank you enough to Rasmus > Lerdorf and whomever else was involved in resuscitating the list? > I've had a similar project kicking about in my head, and this post finally got me to try a few things. The idea I had was for a "multi-questionnaire" system, where surveys are created from a database query, scored by a common marking script, and the results stored back to the database. Obviously, the marking script can't know what inputs to expect... My first idea was to create a parseable "variable-names-to-look-for" variable, but that seemed unwieldy. Then I recalled someone complaining about not being able to retrieve multiple input fields with the same name - they only get the last ie most recent field set. ;-) and there's the answer. If you have a hidden input with the same name and a default value _before_ your checkbox input, then your checkbox will over-write it if it's checked; otherwise you receive the default value! ie <input type="hidden" name="myvariable" value="No"> <input type="checkbox" name="myvariable" value="Yes"> if the checkbox is checked, $myvariable = "Yes", otherwise $myvariable = "No" - exactly what you were looking for. Better yet, because $myvariable only occurs once among the POSTed variables, you can simply iterate through them, ie foreach($HTTP_POST_VARS as $name => $val) $outstr .= "<br>$name: $val"; See the attached file, 'test.php', for a simply demo. begin 666 test.php M/#]P:' *"21S='(@/2 B(CL*"69O<F5A8V@H)$A45%!?4$]35%]605)3(&%S M("1N86UE(#T^("1V86PI('L*"0DD<W1R("X]("(\8G(^)&YA;64@+29G=#L@ M(CL*"0D*"0EI9B H)'9A;" ]/2 B(BD*"0D))'-T<B N/2 B*&5M<'1Y*2([ M"@D)96QS90H)"0DD<W1R("X]("1V86P["@E]"@H_/@H\(41/0U194$4@2%1- M3"!054),24,@(BTO+U<S0R\O1%1$($A434P@-"XP(%1R86YS:71I;VYA;"\O M14XB/@H\:'1M;#X*/&AE860^"@D\=&ET;&4^56YT:71L960\+W1I=&QE/@H\ M+VAE860^"CQB;V1Y/@H)/&@Q/E1E<W0@;7D@8VAE8VMB;W@M=F%L=64M<V]L M=F5R(&ED96$\+V@Q/@H)/#\]("1S='(@/SX*"3QF;W)M(&UE=&AO9#TB<&]S M="(@86-T:6]N/2(C(CX*"0D\=&%B;&4^"@D)"3QT<CX*"0D)"3QT9#Y;($9I M;&QE9"!\($5M<'1Y(%T\+W1D/@H)"0D)/&EN<'5T('1Y<&4](FAI9&1E;B(@ M;F%M93TB9FEL;&5D96UP='DB('9A;'5E/2(B/@H)"0D)/'1D/CQI;G!U="!T M>7!E/2)C:&5C:V)O>"(@;F%M93TB9FEL;&5D96UP='DB('9A;'5E/2)F:6QL M960B/CPO=&0^"@D)"3PO='(^"@D)"3QT<CX*"0D)"3QT9#Y;($)L=64@?"!' M<F5E;B!=/"]T9#X*"0D)"3QI;G!U="!T>7!E/2)H:61D96XB(&YA;64](F)L M=65G<F5E;B(@=F%L=64](D=R965N(CX*"0D)"3QT9#X\:6YP=70@='EP93TB M8VAE8VMB;W@B(&YA;64](F)L=65G<F5E;B(@=F%L=64](D)L=64B/CPO=&0^ M"@D)"3PO='(^"@D)"3QT<CX*"0D)"3QT9#Y;(%EE<R!\($YO(%T\+W1D/@H) M"0D)/&EN<'5T('1Y<&4](FAI9&1E;B(@;F%M93TB>65S;F\B('9A;'5E/2). M;R(^"@D)"0D\=&0^/&EN<'5T('1Y<&4](F-H96-K8F]X(B!N86UE/2)Y97-N M;R(@=F%L=64](EEE<R(^/"]T9#X*"0D)/"]T<CX*"0D\+W1A8FQE/@H)"3QI M;G!U="!T>7!E/2)S=6)M:70B/@H)/"]F;W)M/@H*/"]B;V1Y/@H\+VAT;6P^ !"@`` ` end

« previous php.general (#54536) next »