stangen Posted January 10, 2014 Share Posted January 10, 2014 Ok I am needing to create a simple function to change text submitted by a form. I have a form with a drop down select. This form is posting to another page and I am getting the data using the <?php echo $_POST["frame"]; ?> function This echo's the value that is set in the form. I am wanting to change the value that is being displayed only on this one page. I would like to data to stay the same, but show something different Example: I know this is not the correct code, using as example: <form action="nextpage.php" method="post"> <select name="frame" id="frame"> <option value="#000000">Black Frame</option> <option value="#0000FF">Red Frame</option> <option value="#FFFFFF">White Frame</option> </select> <input type="submit"> </form> The form has a drop down to select a frame color. I used a hex code as a value, so on the next page it is pulling the data using the above code and it is displaying the hex code. What I would like for it to do is enter in the name of the color that is shown in the drop down menu.Such as "Black Frame" instead of "#000000" I've been thinking about creating an external php file that will sort of "translate" the text. Like, if $frame #000000 then echo Black Frame , but as a newbie I am unsure how to do this correctly Quote Link to comment https://forums.phpfreaks.com/topic/285265-probably-simple-but-im-new-at-php/ Share on other sites More sharing options...
adam_bray Posted January 10, 2014 Share Posted January 10, 2014 You can do that using arrays - <?php $color_to_hex = array( 'black' => '#000000', 'blue' => '#0000ff', 'green' => '#00ff00', 'red' => '#ff0000', 'white' => '#ffffff', ); print $color_to_hex[$_POST['color']]; ?> $_POST['color'] would be one of the left values (a key) and it would print the right value (array value). Quote Link to comment https://forums.phpfreaks.com/topic/285265-probably-simple-but-im-new-at-php/#findComment-1464732 Share on other sites More sharing options...
ginerjm Posted January 10, 2014 Share Posted January 10, 2014 Despite the overall goal of this exercise, the easy way to accomplish what I think you are asking is (and BTW - try to post a REAL question in your topic header next time!) to make your value attribute contain both things. <option value="#000000|Black">Black Frame</option> Then when you retrieve it do this: $sel_ar = explode("|",$_POST['frame']); This will give you an array with: $sel_ar[0] = "#000000"; $sel_ar[1] = "Black"; Quote Link to comment https://forums.phpfreaks.com/topic/285265-probably-simple-but-im-new-at-php/#findComment-1464733 Share on other sites More sharing options...
stangen Posted January 10, 2014 Author Share Posted January 10, 2014 Despite the overall goal of this exercise, the easy way to accomplish what I think you are asking is (and BTW - try to post a REAL question in your topic header next time!) to make your value attribute contain both things. <option value="#000000|Black">Black Frame</option> Then when you retrieve it do this: $sel_ar = explode("|",$_POST['frame']); This will give you an array with: $sel_ar[0] = "#000000"; $sel_ar[1] = "Black"; This seems to have fixed my issue. Thank you for this! Quote Link to comment https://forums.phpfreaks.com/topic/285265-probably-simple-but-im-new-at-php/#findComment-1464737 Share on other sites More sharing options...
stangen Posted January 10, 2014 Author Share Posted January 10, 2014 Actually, I was wrong, this did not work. I am unable to get the data to show. I am using <?php $sel_ar[0] = explode("|",$_POST['frame']); ?> to show the color hex code and it's not working. Am I typing it incorrectly? Quote Link to comment https://forums.phpfreaks.com/topic/285265-probably-simple-but-im-new-at-php/#findComment-1464741 Share on other sites More sharing options...
ginerjm Posted January 10, 2014 Share Posted January 10, 2014 No you are not. Quote Link to comment https://forums.phpfreaks.com/topic/285265-probably-simple-but-im-new-at-php/#findComment-1464742 Share on other sites More sharing options...
stangen Posted January 10, 2014 Author Share Posted January 10, 2014 (edited) Is this supposed work in the _GET method as well? I've changed the _POST to _GET Edited January 10, 2014 by stangen Quote Link to comment https://forums.phpfreaks.com/topic/285265-probably-simple-but-im-new-at-php/#findComment-1464743 Share on other sites More sharing options...
ginerjm Posted January 10, 2014 Share Posted January 10, 2014 Sorry - I read your first response incorrectly. YES - you are typing it incorrectly and you are attempting to extract the retrieved values incorrectly. To retrieve your values you use the explode function on the input value in the POST array which I showed you. Then wherever you want to display one of those two values you simply output one element of the array - the '0' element for the numeric value or the '1' element for the name value. So to display both values you could have this in your script: echo "You have selected a color value of " . $ar[0] . " which is the color " . $ar[1]; Quote Link to comment https://forums.phpfreaks.com/topic/285265-probably-simple-but-im-new-at-php/#findComment-1464744 Share on other sites More sharing options...
stangen Posted January 10, 2014 Author Share Posted January 10, 2014 (edited) Ok I am totally confused now. Forgive me for my lack of PHP knowledge. First form <form action="firstpage.php" method="get"> Select font color: <br> <select name="color" id="color"> <option value="#FF0000|Red">Red</option> <option value="#FFFFFF|White">White</option> <option value="#000000|Black">Black</option> </select> </form> On firstpage.php trying to call the hex code into inline css: <span class="previewtext" style="color:<?php $ar[0]?>;">Black Text</span> What am I doing incorrectly? Edited January 10, 2014 by stangen Quote Link to comment https://forums.phpfreaks.com/topic/285265-probably-simple-but-im-new-at-php/#findComment-1464754 Share on other sites More sharing options...
ginerjm Posted January 10, 2014 Share Posted January 10, 2014 First of all - I would never mix my html and my php the way you (and many many others are doing). I would do this in my php section before I began my html output: $color_opt = "'" . $ar[0] . "'"; $color_nm = $ar[1]; which is the color value obtained already from my previous code snippet bracketed inside two single quotes. then in my html section where I am outputting everything : <span class="previewtext" style="color:$color_opt;">$color_nm Text</span So much easier to read and type <span class="previewtext" style="color:<?php .$ar[0];?>;">Black Text</span> Quote Link to comment https://forums.phpfreaks.com/topic/285265-probably-simple-but-im-new-at-php/#findComment-1464757 Share on other sites More sharing options...
adam_bray Posted January 10, 2014 Share Posted January 10, 2014 This does what you need and stores the color information in 1 place. <?php // Store available colors $colors = array( 'black' => '#000000', 'blue' => '#0000ff', 'green' => '#00ff00', 'red' => '#ff0000', 'white' => '#ffffff', ); // If the form has been submit if( $_POST['color'] ) { die( 'You selected a ' . array_search( $_POST['color'], $colors ) . ' frame, that\'s ' . $_POST['color'] . ' in HEX!' ); } // Set up the form options $select_options = ''; foreach( $colors as $color => $hex ) { $select_options .= '<option value="'.$hex.'">'.$color.' Frame</option>' . "\n"; } ?> <form method="post" action="<?=$_SERVER['PHP_SELF'];?>"> <select name="color"> <option selected="true" disabled="true">Please select frame color</option> <?=$select_options;?> </select> </form> Quote Link to comment https://forums.phpfreaks.com/topic/285265-probably-simple-but-im-new-at-php/#findComment-1464769 Share on other sites More sharing options...
jcbones Posted January 10, 2014 Share Posted January 10, 2014 I am with Adam Bray on this one.Here is an exercise: Run the following code in a stand alone script: <?php $color_to_hex = array( 'black' => '#000000', 'blue' => '#0000ff', 'green' => '#00ff00', 'red' => '#ff0000', 'white' => '#ffffff', ); if($_SERVER['REQUEST_METHOD'] == 'POST') { echo 'The color you selected is ' . $_POST['color'] . ' and has a hex value of ' . $color_to_hex[$_POST['color']] . '<br />'; } echo '<form method="post"> Select font color: <br /> <select name="color" id="color">'; foreach($color_to_hex as $key => $value) { echo '<option value="' . $key . '">' . ucfirst($key) . ' Frame</option>'; } echo '</select> <input type="submit" value="Submit" /> </form>'; Quote Link to comment https://forums.phpfreaks.com/topic/285265-probably-simple-but-im-new-at-php/#findComment-1464770 Share on other sites More sharing options...
Solution stangen Posted January 10, 2014 Author Solution Share Posted January 10, 2014 You can do that using arrays - <?php $color_to_hex = array( 'black' => '#000000', 'blue' => '#0000ff', 'green' => '#00ff00', 'red' => '#ff0000', 'white' => '#ffffff', ); print $color_to_hex[$_POST['color']]; ?> $_POST['color'] would be one of the left values (a key) and it would print the right value (array value). I've used this method and it works for what I need. Thanks! <?php $hex = array ('black' => '#000000','white' =>'#ffffff','red' => '#ff0000'); ?> Changed my form options to: <select name="color" id="color"> <option value="red">Red</option> <option value="white">White</option> <option value="black">Black</option> </select> Then I called the array in my inline css <span class="previewtext" style="color:<?php echo''.$hex[$_GET['color']];?>;">Text</span> And then finished up calling on the second page as normal Font Color:<?php echo $_POST['color'];?> All works great. Thanks for the help guys! Quote Link to comment https://forums.phpfreaks.com/topic/285265-probably-simple-but-im-new-at-php/#findComment-1464772 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.