Solved

(wanted) PHP Codes for changing INPUT VALUEs

Posted on 2006-11-04
7
286 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now