We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

(wanted) PHP Codes for changing INPUT VALUEs

egolddude
egolddude asked
on
Medium Priority
357 Views
Last Modified: 2010-07-27
Straight to the point ... I need an answer soon ... Please help me ...

Pay attention to the options' input value (color0, color1, color2, color3, animal0, animal1, animal2, animal3)

I have this form:
-----------------
<FORM METHOD="POST" ACTION="form.php">
Please choose your favorite color:&nbsp;
<SELECT NAME="color">
<OPTION VALUE="color0" selected>Choose One</OPTION>
<OPTION VALUE="color1">Red</OPTION>
<OPTION VALUE="color2">Yellow</OPTION>
<OPTION VALUE="color3">Green</OPTION>
</SELECT>
<BR><BR>
Please choose your favorite animal:&nbsp;
<SELECT NAME="animal">
<OPTION VALUE="animal0" selected>Choose One</OPTION>
<OPTION VALUE="animal1">Dog</OPTION>
<OPTION VALUE="animal2">Cat</OPTION>
<OPTION VALUE="animal3">Monkey</OPTION>
</SELECT>
<BR><BR>
<INPUT TYPE="submit" NAME="submit" VALUE="Continue">
</FORM>


and the "form.php"
------------------
<?
$color = $_POST['color'];
$animal = $_POST['animal'];
?>
<FORM METHOD="POST" ACTION="nextform.php">
<INPUT TYPE="hidden" NAME="color" VALUE="<?php echo $color; ?>">
<INPUT TYPE="hidden" NAME="animal" VALUE="<?php echo $animal; ?>">
PLEASE REVIEW YOUR SUBMITTED INFORMATION!<BR>
Click NEXT to go to next survey or BACK to make changes
<BR><BR>
Your favorite color is <?php echo $color; ?>
<BR><BR>
Your favorite animal is <?php echo $animal; ?>
<BR><BR>
<INPUT TYPE="button" NAME="back" VALUE="BACK" onclick="javascript:history.back()">
&nbsp;&nbsp;
<INPUT TYPE="submit" NAME="submit" VALUE="NEXT">
</FORM>


The VIEW Results on "form.php" will looks like this:
----------------------------------------------------
Your favorite color is color3
Your favorite animal is animal3


What I need is the RESULTS should shows like:
---------------------------------------------
Your favorite color is Green
Your favorite animal is Monkey

So, please help me for the PHP codes in changing the values
- color0 to NOT SELECTED
- color1 to RED
- color2 to YELLOW
- and so all VALUES ...

Thanks ... :) ... I hope the explanation I'm about to receive is clear enough for a PHP newbie ... :))
Comment
Watch Question

Commented:
On all your OPTION statements, make the VALUE= the name you want to display:

<OPTION VALUE="RED">Red</OPTION>
<OPTION VALUE="YELLOW">Yellow</OPTION>
<OPTION VALUE="GREEN">Green</OPTION>
</SELECT>
<BR><BR>
Please choose your favorite animal:&nbsp;
<SELECT NAME="animal">
<OPTION VALUE="animal0" selected>Choose One</OPTION>
<OPTION VALUE="DOG">Dog</OPTION>
<OPTION VALUE="CAT">Cat</OPTION>
<OPTION VALUE="MONKEY">Monkey</OPTION>

etc.


Top Expert 2006

Commented:
Hi,

you could do this in a few ways - firstly you could just change the option value to be the same as the text displayed

ie
<OPTION VALUE="RED">RED</OPTION>

or alternativley you could set up an array to check these against,

ie

$colorArray['color0'] = "Choose One";
$colorArray['color1'] = "Red";
$colorArray['color2'] = "Yellow";
$colorArray['color3'] = "Green";

Then you could do

if (isset($_POST['color']0 && isset($colorArray[$_POST['color']])){
    $color = $colorArray[$_POST['color']];
} else {
    $color = "Not In List Of Acceptable Values";
}

which adds the colours to an array - checks that the color was submitted and what was submitted was in the array and if so then returns the correct value else it will say that it was not set correctly.
Top Expert 2006

Commented:
You would normally go the array option if you had loaded the data from a database then dynamically created the options - then saved that array that you pulled the values from the database into a $_SESSION array - then retreive this on submit and check against what you orginally gave th euser the option to select from.

Author

Commented:
To 'yodercm' ... No I cannot simply change the values as <OPTION VALUE="RED">Red</OPTION> because of some reasons ...

To 'Raynard7' ... Yes this ARRAY thingy is exactly what I've been looking for.
And I just try this and not working .. :( .. Please give me a complete source for the PHP Section.
Here what I was compile (somehow I know this aint a CORRECT codes):

<?
$colorArray['color0'] = "NOT SELECTED";
$colorArray['color1'] = "RED";
$colorArray['color2'] = "YELLOW";
$colorArray['color3'] = "GREEN";

if (isset($_POST['color']0 && isset($colorArray[$_POST['color']])){
    $color = $colorArray[$_POST['color']];
} else {
    $color = "Not In List Of Acceptable Values";
}

$animalArray['animal0'] = "NOT SELECTED";
$animalArray['animal1'] = "DOG";
$animalArray['animal2'] = "CAT";
$animalArray['animal3'] = "MONKEY";

if (isset($_POST['animal']0 && isset($animalArray[$_POST['animal']])){
    $animal = $animalArray[$_POST['animal']];
} else {
    $animal = "Not In List Of Acceptable Values";
}

?>

The DATAs come from the 'submitted form' NOT from a database .. I believe this will easier the codes written.
Top Expert 2006
Commented:
had a typeo with 0 instead of ) (must have forgot to press shift)

<?
$colorArray['color0'] = "NOT SELECTED";
$colorArray['color1'] = "RED";
$colorArray['color2'] = "YELLOW";
$colorArray['color3'] = "GREEN";

if (isset($_POST['color']) && isset($colorArray[$_POST['color']])){
    $color = $colorArray[$_POST['color']];
} else {
    $color = "Not In List Of Acceptable Values";
}

$animalArray['animal0'] = "NOT SELECTED";
$animalArray['animal1'] = "DOG";
$animalArray['animal2'] = "CAT";
$animalArray['animal3'] = "MONKEY";

if (isset($_POST['animal']) && isset($animalArray[$_POST['animal']])){
    $animal = $animalArray[$_POST['animal']];
} else {
    $animal = "Not In List Of Acceptable Values";
}

?>

should work

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Raynard7 ... You're rocks man ... @'~'@ ...

YES the problems are all FIXED ... @'~'@ ...

!! TWO THUMBS UP !!
Top Expert 2006

Commented:
Excellent - glad I could help
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.