Re: radio form submission
| From: | Tamara Temple | Date: | Fri, 24 Jun 2011 00:41:12 +0000 |
| Subject: | Re: radio form submission | ||
| References: | 1 2 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-47875@lists.php.net to get a copy of this message | ||
On Jun 23, 2011, at 2:32 PM, Karl DeSaulniers wrote:
Try this... function getSpeed($val) { if($val != 'undefined') { switch ($val){ case "1": $post_tptest = "0-250kbps"; break; case "2": $post_tptest = "250-300kbps"; break; case "3": $post_tptest = "300-400kbps"; break; case "4": $post_tptest = "400-600kbps"; break; case "5": $post_tptest = "600kbps-3.8mbps"; break; default: $post_tptest = "Speed Undetected"; // or "0-250kbps" break; } } else { return("Error, no speed value set");Just to point out, this is the only return from this function. The $post_tptest is never returned. You could actually just say: return "0-250kbps"; above instead of setting the $post_tptest *local* variable. (You wouldn't need the break statements, then, either, but it's still probably a good idea to code them in.)
} } } $post_speed = getSpeed($post_tptest);Or, you know, an array: speed = array( '1'=>'0-250kbps', '2'=>'250-300kbps'. '3'=>'300-400kbps', '4'=>'400-600kbps', '5'=>'600kbps-3.8mbps', ); //$post_speed = isset($speed[$post_tptest])?$speed[$post_tptest]:"Speed undetected"; (See below for inline usage.) Of course, you could also avoid all this translation by putting the string values you want in the radio button on the form itself. This may have implications for your database and how you store it, however. You can also avoid the problem with an empty return if no radio button is checked by making sure one is checked when your form loads, by using the 'checked' attribute on one of the items. If you want them to have a "not tested" option, you can include that as one of your buttons and add it to the array above as well. (That might be a good place to use the zero value.)
$posts_sql = "SELECT posts.post_store, posts.post_content, posts.post_speed, posts.post_date, posts.post_by, users.user_id, users.user_name, users.first_name, users.last_name FROM posts LEFT JOIN users ON posts.post_by = users.user_id WHERE posts.post_store = " . mysql_real_escape_string($_GET['id']) . " ORDER BY posts.post_date DESC "; ... Best, Karl On Jun 23, 2011, at 1:18 PM, Chris Stinemetz wrote:Here is where you'd use the speed array we defined above: <td class="post-content">' . (isset($speed[($posts_row['post_tptest'])])? $speed[($posts_row['post_tptest'])]:"Speed undetected") .So far I am good on creating the form and submitting it to mysql database and calling the submitting value from a different script. My question is: How can I make it so when a user selects radio option value "1" it will then be displayed as 0-250kbps when I call it the value in the bottom script? forgive my indention. Gmail messes it up. #### My Form #### echo '<form method="post" action=""> Store name: <input type="text" name="store_subject" /><br /<br />Market:'; echo '<select name="store_mar">'; while($row = mysql_fetch_assoc($result)) { echo '<option value="' . $row['mar_id'] . '">' . $row['mar_name'] . '</option>'; } echo '</select><br /><br />'; echo 'Broadband speed test results: <br /><br /> 0-250kbps<input type="radio" name="post_tptest" value="1" />| 250-300kbps<input type="radio" name="post_tptest" value="2" />| 300-400kbps<input type="radio" name="post_tptest" value="3" />| 400-600kbps<input type="radio" name="post_tptest" value="4" />| 600kbps-3.8mbps<input type="radio" name="post_tptest" value="5" /><br /><br /> Store visit details: <br /><br /> <textarea name="post_content" /></textarea><br /><br /> <input type="submit" value="Add a store visit" /> </form>'; #### My display script #### $posts_sql = "SELECT posts.post_store, posts.post_content, posts.post_tptest, posts.post_date, posts.post_by, users.user_id, users.user_name, users.first_name, users.last_name FROM posts LEFT JOIN users ON posts.post_by = users.user_id WHERE posts.post_store = " . mysql_real_escape_string($_GET['id']) . " ORDER BY posts.post_date DESC "; $posts_result = mysql_query($posts_sql); if(!$posts_result) { echo '<tr><td>The posts could not be displayed, please try again later.</tr></td></table>'; } else { while($posts_row = mysql_fetch_assoc($posts_result)) { echo '<tr class="topic-post"> <td class="user-post">' . $posts_row['first_name'] . ' ' . $posts_row['last_name'] . '<br/>' . date('m-d-Y h:iA', strtotime($posts_row['post_date'])) . '</td> <td class="post-content">' . $posts_row['post_tptest'] .
'<br/>' . htmlentities(stripslashes($posts_row['post_content'])) . '</td> </tr>'; } } --PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.phpKarl DeSaulniers Design Drumm http://designdrumm.com