Solved

(wanted) PHP Codes for changing INPUT VALUEs

Posted on 2006-11-04
7
325 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 ... :))
0
Comment
Question by:egolddude
  • 4
  • 2
7 Comments
 
LVL 27

Expert Comment

by:yodercm
ID: 17874964
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.


0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17874968
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.
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17874971
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.
0
Industry Leaders: 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!

 

Author Comment

by:egolddude
ID: 17875047
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.
0
 
LVL 35

Accepted Solution

by:
Raynard7 earned 500 total points
ID: 17875053
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
0
 

Author Comment

by:egolddude
ID: 17875103
Raynard7 ... You're rocks man ... @'~'@ ...

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

!! TWO THUMBS UP !!
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17875105
Excellent - glad I could help
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question