nblax27 Posted October 23, 2007 Share Posted October 23, 2007 I have a very simple form <form class="main" name="search" method="get" action="http://rapidsearch.gptwebsites.com/search.php"> <input maxlength="1000" size="50" name="query" value="" title="Search" /> <input type="submit" value="Search!" style="width:75px;" /> <table class="main"><tr><td><input type="checkbox" name="siteid[]" value="1" checked="checked" />rapidshare.com</td> <td><input type="checkbox" name="siteid[]" value="2" />megaupload.com</td> <td><input type="checkbox" name="siteid[]" value="3" />sendspace.com</td></tr></table> The search.php page is... <?php $query = $_GET['query']; $siteid = implode(', ', $_GET['siteid']); echo $query; echo $siteid; ?> How can I make the value of the siteid[] a variable or another value. For example... I want 1 to equal rapidshare, 2 to equal megadownload, and 3 to equal sharespace. Instead of listing the number "1" when i echo $siteid, I want it to say rapidshare, NOT 1. I want to keep the values of the form as 1, 2, and 3 because I need to keep the url short. Do I need to do something before or after $_GET or can I somehow put it in the implode () funtion? Quote Link to comment https://forums.phpfreaks.com/topic/74414-solved-how-to-make-form-value-from-a-table-a-variable/ Share on other sites More sharing options...
PHP_PhREEEk Posted October 23, 2007 Share Posted October 23, 2007 When using small values like that, you can get away with a quick n' dirty Switch/Case flow. Quick example: switch ($siteid) { case 1: echo "Rapidshare"; break; case 2: echo "Megaupload"; break; case 3: echo "Sendspace"; break; default: // $siteid was not 1, 2, or 3. Generate error here, or re-direct back to form... echo "Unknown Site submitted!"; } You should definitely sanitize those variables too... Switch/Case help here: http://us2.php.net/manual/en/control-structures.switch.php PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/74414-solved-how-to-make-form-value-from-a-table-a-variable/#findComment-376082 Share on other sites More sharing options...
nblax27 Posted October 23, 2007 Author Share Posted October 23, 2007 This will not work because it will only list 1 checked item. If all three boxes are checked I need all three names to appear. This will list only the first one that it come across. Taking the break out makes it list all three even when they are not checked. Quote Link to comment https://forums.phpfreaks.com/topic/74414-solved-how-to-make-form-value-from-a-table-a-variable/#findComment-376462 Share on other sites More sharing options...
PHP_PhREEEk Posted October 23, 2007 Share Posted October 23, 2007 Alrighty then... let's try in_array... // test to see that we have an array if (is_array($_GET[siteid])) { // assign all selected searches into a text array if ( (in_array ('1', $_GET[siteid], true) ) ) { $query_arr[] .= 'Rapidshare'; } if ( (in_array ('2', $_GET[siteid], true) ) ) { $query_arr[] .= 'Megaupload'; } if ( (in_array ('3', $_GET[siteid], true) ) ) { $query_arr[] .= 'Sendspace'; } // now we concantenate all text and separate by a comma and whitespace if ( count($query_arr) > 1 ) { foreach ($query_arr as $text) { $query_text .= $text . ', '; } $query_text = substr($query_text, 0, strlen($query_text) - 2); // strips last comma + whitespace } else { // if there was only one search type, assign it here $query_text = $query_arr[0]; } echo 'Query = ' . $query_text; } else { echo 'No Query Type submitted!'; exit; } This code has been thoroughly tested. PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/74414-solved-how-to-make-form-value-from-a-table-a-variable/#findComment-376520 Share on other sites More sharing options...
nblax27 Posted October 24, 2007 Author Share Posted October 24, 2007 This worked. Thank you for your help!! Quote Link to comment https://forums.phpfreaks.com/topic/74414-solved-how-to-make-form-value-from-a-table-a-variable/#findComment-376666 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.