Tuesday, 15 May 2012

forms - Radio buttons, changing the background color PHP -



forms - Radio buttons, changing the background color PHP -

alright guys, have form. time user clicks submit, form should output users selections, or text, background color of user selected.

currently working text...and checkboxes. cannot drop downwards selection output... , when comes background color.... have no clue on start.. kinda lengthy... sorry that, need direction. illustration of it's supposed on output here: http://omega.uta.edu/~cyjang/ctec4309/labex/php2/post_form_3a.php 1 time fill out, see..

here html :

<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="mycity"> <option value="arlington" name"city">arlington</option> <option value="dallas" name"city">dallas</option> <option value="ftw" name"city">fort worth</option> </select> <br/> background color *: <input type="radio" name="bgcolor" value"yellow"> yellowish <input type="radio" name="bgcolor" value"blue"> bluish <br/> comment * : <br/><textarea name="comment" rows="5" cols="40"></textarea><br> <input type="submit" name="submitthis" value="preview"> </form>

and here php: //========================== // info validation //========================== // check see if there form submission or not if (array_key_exists("submitthis", $_post)) { // info validation // - check required fields //== modify required , expected arrays below fit form ======== $required = array('title', 'author','comment','bgcolor', 'city'); $expected = array('title', 'author','comment','tag', 'email', 'city'); $missing = array(); // utilize foreach loop run through each item in expected array foreach($expected $thisfield) { // setup variable store user input field name $thisuserinput = $_post[$thisfield]; // check if field required field if (in_array($thisfield, $required)) { // check if user input of field empty, if yes, add together field missing array if (empty($thisuserinput)) { array_push($missing, $thisfield); } else { ${$thisfield} = $thisuserinput; } } else { ${$thisfield} = $thisuserinput; } } // after running through expected fields, check $missing array. if there no required field missing, $missing array empty. if (empty($missing)){ // empty($missing) true --> no missing field, proceed business processes (in example, display user input.) // deal array input, ex. $tag $tagstr = implode(", ", $tag); // print_r ($tag); // enable line print $tag array, can see what's been stored in $tag array. may help debug. // process author name , email if (!empty($email)) { $author = "<a href='mailto:$email'>$author</a>"; } $output = "<p> <table border=2 cellpadding=5> <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) false --> $missing array not empty -- prepare message user $missingfieldlist = implode(", ",$missing); $output = "the next fields missing post, please go , fill them in. give thanks you. <br> <b>missing fields: $missingfieldlist </b> "; } } else { $output = "please post message utilize <a href='post_form_3.php'>this form</a>."; } ?>

on drop downwards <option> can have value="" attribute, not name="". name can in <select> portion:

<select name="city"> <option value="arlington">arlington</option> <option value="dallas">dallas</option> <option value="ftw">fort worth</option> </select>

for color, utilize simple style sheet (css):

$output = " <style> th,td { background-color: $bgcolor; } </style>";

or, if create css class called "yellow" , "blue", can assign class:

$output .= " <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>";

css file:

table.yellow td, table.yellow th { background-color: #ffff00; } table.blue td, table.blue th { background-color: #0000ff; }

php forms validation radio-button

No comments:

Post a Comment