Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

PHP: How to how values on CheckBox & SelectBox after form redirected back

.. continued from http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_22756015.html

actually the form is having an image verification (captcha), and if the image verification is wrong then user is redirected back to the same form showing the information that they were filled previously and only asked them to enter the correct captcha
 ... I put this on that page:

if (isset($_REQUEST['name']))
  $name = $_REQUEST['name'];

if (isset($_REQUEST['email']))
  $email = $_REQUEST['email'];

if (isset($_REQUEST['os']))
  $os = $_REQUEST['os'];

if (isset($_REQUEST['other2']))
  $other2 = $_REQUEST['other2'];

And the value of $name is written on the
<input type="text" name="name" value="$name" size="40">

Also for the $email on:
<input type="text" name="email" value="$email" size="40">

... BUT not on the selectbox which is $os OR $other2:
<select name="os" value="$os">
<option value="">-- Choose One --</option>
<option value="windows">Windows</option>
<option value="linux">Linux</option>
<option value="other1">Other</option>
</select>
<br />
<label>Please specify: <input type="text" name="other2" value="other2" class="DEPENDS ON os BEING other1"></label>

Also for this CheckBoxes:
Your favorite color (more than one is accepted):
<input type="checkbox" name="color" value="Red" />Red<br/>
<input type="checkbox" name="color" value="Black" />Black<br/>
<input type="checkbox" name="color" value="White" />White<br/>
<input type="checkbox" name="color" value="Green" />Green<br/>

Really need an a.s.a.p reply ...
0
planetemails
Asked:
planetemails
  • 3
  • 2
  • 2
2 Solutions
 
etullyCommented:
<select name="os" value="$os">
<option value="">-- Choose One --</option>
<option value="windows"
<?php if ($os=="windows") { print " selected"; }?>>Windows</option>
<option value="linux"
<?php if ($os=="linux") { print " selected"; }?>>Linux</option>
<option value="other1"
<?php if ($os=="other1") { print " selected"; }?>>Other</option>
</select>


<input type="checkbox" name="color" value="Red"
<?php if (in_array("Red",$color)) { print " checked"; } ?> />Red<br/>
<input type="checkbox" name="color" value="Black"
<?php if (in_array("Black",$color)) { print " checked"; } ?> />Black<br/>
<input type="checkbox" name="color" value="White"
<?php if (in_array("White",$color)) { print " checked"; } ?> />White<br/>
<input type="checkbox" name="color" value="Green"
<?php if (in_array("Green",$color)) { print " checked"; } ?> />Green<br/>

The code for the checkboxes assumes that the redirect is passing zero or more values of color like this:  color[]=Red
0
 
planetemailsAuthor Commented:
Oh .. gee ... my mistake ..  :((
Problems fixed for the SelectBoxes BUT not yet on the CheckBoxes ... actually it's written like this:

<input type="checkbox" name="color1" value="Red" />Red<br/>
<input type="checkbox" name="color2" value="Black" />Black<br/>
<input type="checkbox" name="color3" value="White" />White<br/>
<input type="checkbox" name="color4" value="Green" />Green<br/>

Please give me solution for this ...
0
 
hernst42Commented:
Change
<input type="checkbox" name="color1" value="Red" />Red<br/>
<input type="checkbox" name="color2" value="Black" />Black<br/>
<input type="checkbox" name="color3" value="White" />White<br/>
<input type="checkbox" name="color4" value="Green" />Green<br/>

to have multiple colors, add the [] to the name.
<input type="checkbox" name="color[]" value="Red"
<?php if (in_array("Red",$color)) { print " checked"; } ?> />Red<br/>
<input type="checkbox" name="color[]" value="Black"
<?php if (in_array("Black",$color)) { print " checked"; } ?> />Black<br/>
<input type="checkbox" name="color[]" value="White"
<?php if (in_array("White",$color)) { print " checked"; } ?> />White<br/>
<input type="checkbox" name="color[]" value="Green"
<?php if (in_array("Green",$color)) { print " checked"; } ?> />Green<br/>

In your code you will no longer have $_REQUEST['color1'], ..., instead you have $_REQUEST['color'] which is an array or not set, in non checkbox ismarked.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
planetemailsAuthor Commented:
Well, if I change all 'name' on checkboxes to name="color[]" ... it will then affect another codes in the page which is:

$color = "";
if (isset($_REQUEST['color1'])) {
  $color .= $_REQUEST['color1'];
}
if (isset($_REQUEST['color2'])) {
  if ($color !== "") {
    $color .= ", ";
  }
  $color .= $_REQUEST['color2'];
}
if (isset($_REQUEST['color3'])) {
  if ($color !== "") {
    $color .= ", ";
  }
  $color .= $_REQUEST['color3'];
}
if (isset($_REQUEST['color4'])) {
  if ($color !== "") {
    $color .= ", ";
  }
  $color .= $_REQUEST['color4'];
}

Is there other way so I can still use the original:
<input type="checkbox" name="color1" value="Red" />Red<br/>
<input type="checkbox" name="color2" value="Black" />Black<br/>
<input type="checkbox" name="color3" value="White" />White<br/>
<input type="checkbox" name="color4" value="Green" />Green<br/>

Let me know ...
0
 
etullyCommented:
This will do it for the checkboxes

<input type="checkbox" name="color" value="Red"
<?php if ($color1=="Red") { print " checked"; } ?> />Red<br/>
<input type="checkbox" name="color" value="Black"
<?php if ($color2=="Black") { print " checked"; } ?> />Black<br/>
<input type="checkbox" name="color" value="White"
<?php if ($color3=="White") { print " checked"; } ?> />White<br/>
<input type="checkbox" name="color" value="Green"
<?php if ($color4=="Green") { print " checked"; } ?> />Green<br/>

And still use this for the select boxes:

<select name="os" value="$os">
<option value="">-- Choose One --</option>
<option value="windows"
<?php if ($os=="windows") { print " selected"; }?>>Windows</option>
<option value="linux"
<?php if ($os=="linux") { print " selected"; }?>>Linux</option>
<option value="other1"
<?php if ($os=="other1") { print " selected"; }?>>Other</option>
</select>
0
 
hernst42Commented:
If you stil lwant to keep the names use:

<input type="checkbox" name="color1" value="Red"
<?php if (isset($_REQUEST['color1'])) { print " checked"; } ?> />Red<br/>
<input type="checkbox" name="color2" value="Black"
<?php if (isset($_REQUEST['color2'])) { print " checked"; } ?> />Black<br/>
<input type="checkbox" name="color3" value="White"
<?php if (isset($_REQUEST['color3'])) { print " checked"; } ?> />White<br/>
<input type="checkbox" name="color4" value="Green"
<?php if (isset($_REQUEST['color4'])) { print " checked"; } ?> />Green<br/>
0
 
planetemailsAuthor Commented:
Wow ... many good people here helping me ... Thanks y'all ... :)

BTW I really have to split the points .. half to etully (for the Select Box) and half to hernst42 (for the CheckBoxes) ... your both response are really the solutions I'm looking for.

Thanks guys ...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now