Jump to content

[SOLVED] How to make form value from a table a variable?


nblax27

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.