Link to home
Start Free TrialLog in
Avatar of planetemails
planetemails

asked on

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

.. continued from https://www.experts-exchange.com/questions/22756015/Form-Submit-problem.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 ...
ASKER CERTIFIED SOLUTION
Avatar of etully
etully

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of planetemails
planetemails

ASKER

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 ...
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.
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 ...
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>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ...