Radio buttons, changing background color on selection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
krnbrasha
Forum Newbie
Posts: 5
Joined: Thu Nov 06, 2014 7:54 pm

Radio buttons, changing background color on selection

Post by krnbrasha »

Hey guys, I am a newbie to this forum and to PHP. So I have an assignment that requires the user to fill out the form and select a background color. Upon pressing the submit button the results the user has filled out will output, and the color of the background will output. I am confusing the crap out of myself trying to apply the user selection of the color. I have two colors they can choose from: yellow or blue. I've tried different things with no results, can anyone send me in the right direction? I had issues outputting the text, checkboxes, and dropdown but those have been taken care of.... ahh the background color. Here is an example.... fill out the form choose a color and it will output the background color that you have chosen. http://omega.uta.edu/~cyjang/ctec4309/l ... orm_3a.php

Code: Select all

<style>
table.yellow td,
table.yellow th {background-color: #ffff00;}

table.blue td,
table.blue th {background-color: #0000ff;}
</style>

<?php
//--------------------------
// Form Processing Script
//		This script is written to work with the form in post_form_3.php.
//--------------------------


//==========================
// Data validation
//==========================

// check to see if there is a form submission or not
if (array_key_exists("SubmitThis", $_POST)) {

	// data validation
	//  - check required fields

	//== Modify the required and expected arrays below to fit your form ========
	$required = array('title', 'author','comment','bgcolor');
	$expected = array('title', 'author','comment','tag', 'email', 'city');
	$missing = array();

	// use foreach loop to run through each item in the expected array
	foreach($expected as $thisField) {
		// setup a variable to store user input for this field name
		$thisUserInput = $_POST[$thisField];

		// check if this field is a required field
		if (in_array($thisField, $required)) {
			// check if user input of this field is empty, if yes, add this field to the missing array
			if (empty($thisUserInput)) {
				array_push($missing, $thisField);
			} else {
				
				${$thisField} = $thisUserInput;
			}
		} else {
			${$thisField} = $thisUserInput;
		}
	}


	// after running through all expected fields, check the $missing array. if there is no required field missing, the $missing array will be empty.
	if (empty($missing)){
		// empty($missing) is true --> no missing field, proceed with business processes (in this example, display all user input.)

		// deal with array input, ex. $tag
		$tagStr = implode(", ", $tag);
		
		// print_r ($tag); // enable this line will print the $tag array, so you can see what's been stored in the $tag array.  It may help you to debug.

		// process author name and email
		if (!empty($email)) {
			$author = "<a href='mailto:$email'>$author</a>";
		}
		
		$output = "
			<style>
				th,td { background-color: $bgcolor;}
			</style>";
			
		$output .= "<p> <table border=2 cellpadding=5 class=\"$bgcolor\">
				<tr><th> Author:</th><td> $author </td></tr>
				<tr><th> Title:</th><td> $title </td></tr>
				<tr><th> Tag:</th><td> $tagStr </td></tr>
				<tr><th> City:</th><td> $city </td></tr>
				<tr><th> Comment:</th><td> <br>$comment </td></tr>
				</table></p>";
	
	} else {
		// empty($missing) is false --> $missing array is not empty -- prepare a message for the user

		$missingFieldList = implode(", ",$missing);
		$output = "The following fields are missing from your post, please go back and fill them in.  Thank you. <br>
						<b>Missing fields: $missingFieldList </b>
					";

	}


} else {
	$output = "Please post your message use <a href='post_form_3.php'>this form</a>.";
}



?>

<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE> CTEC 4309 Class Working File: Message Form Processing </TITLE>
</HEAD>

<BODY>
CTEC 4309 Class Working File 
<hr>

<h2>Preview Your Message</h2>

<hr size="1">
<p>
	<?php echo $output ?>
</p>


</BODY>
</HTML>


This is my HTML:

Code: Select all

<!DOCTYPE HTML>
<HTML>
<HEAD>
<style>
table.yellow td,
table.yellow th {background-color: #ffff00;}

table.blue td,
table.blue th {background-color: #0000ff;}
</style>
<TITLE> CTEC 4309 Class Working File: Posting a Message </TITLE>
</HEAD>

<BODY>
CTEC 4309 Class Working File 
<hr>

<h2>Post a Message</h2>

<hr size="1">

<h3>Form</h3>
* required fields
 <form action= "post3.php" method="post">
	Author * : <input type="text" name="author"><br/>
	Email : <input type="text" name="email"><br/>
	Title * : <input type="text" name="title"><br/>
	
	Tag: 
	<input type="checkbox" name="tag[]" value="General Interests"> General Interests
	<input type="checkbox" name="tag[]" value="Local Schools"> Local Schools
	<input type="checkbox" name="tag[]" value="Safety"> Safety
	<br/>
    
     City *:
    <select name= "city">
    <option value="Arlington" >Arlington</option>
    <option value="Dallas" >Dallas</option>
    <option value="FTW" >Fort Worth</option>
    </select> 
    <br/>
     
     Background color *:
    <input type="radio" name="bgcolor" value"yellow">  Yellow 
    <input type="radio" name="bgcolor" value"blue">  Blue  
    <br/>
    
   
   

	Comment * : <br/><textarea name="comment" rows="5" cols="40"></textarea><br>
	<input type="Submit" name="SubmitThis" value="Preview">
    
    
  </form>
  



</BODY>
</HTML>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Radio buttons, changing background color on selection

Post by Celauran »

Code: Select all

$expected = array('title', 'author','comment','tag', 'email', 'city');
...
foreach($expected as $thisField) {
bgcolor isn't in the $expected array, so $bgcolor never gets set. Referencing it later will therefore do nothing, though it should throw a notice if you have error reporting turned on (and you should!)

Also, you're missing the = in your radio buttons. value="yellow", not value"yellow"
krnbrasha
Forum Newbie
Posts: 5
Joined: Thu Nov 06, 2014 7:54 pm

Re: Radio buttons, changing background color on selection

Post by krnbrasha »

wow, thank you so much.... IT WORKS!!!!!!!!!!!!!
Post Reply