Link to home
Start Free TrialLog in
Avatar of egolddude
egolddude

asked on

(wanted) PHP Codes for changing INPUT VALUEs

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 ... :))
Avatar of Cornelia Yoder
Cornelia Yoder
Flag of United States of America image

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.


Avatar of Raynard7
Raynard7

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.
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.
Avatar of egolddude

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Raynard7
Raynard7

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
Raynard7 ... You're rocks man ... @'~'@ ...

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

!! TWO THUMBS UP !!
Excellent - glad I could help