Solved

(wanted) PHP Codes for changing INPUT VALUEs

Posted on 2006-11-04
7
338 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 27

Expert Comment

by:Cornelia Yoder
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
Independent Software Vendors: 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 dynamically set the form action using jQuery.

628 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