Jump to content

Probably simple, but I'm new at php


Go to solution Solved by stangen,

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/285265-probably-simple-but-im-new-at-php/
Share on other sites

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).

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";

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!

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];

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 by stangen

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>

 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>

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>';
  • Solution

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.