That chubby swede (sleepy) wrote,
That chubby swede
sleepy

  • Mood:
  • Music:
<PHP-Rant>I've searched and googled about how to fetch an array of form data from a multiple select box... with no success at all ... I finally found it, it was obvious and pretty simple... but it seems hard to find such bit and pieces of information... so here's something for other n00bs out there...

<?PHP

( ... snip ... )

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$id = $_POST["id"];

if(isset($_POST["subscribers"]) ){
$subscribers = $_POST["subscribers"];
$items = count($subscribers);

/* Remove all subscriptions... */
$query = "DELETE FROM (tablename) WHERE Product = $id";
// echo($query . "<br>");
mysql_query($query);


for($i = 0; $i < $items; $i++){
$query = "INSERT INTO (tablename) (Subscriber, Product) VALUES('$subscribers[$i]','$id')";
// echo($query . "<br>");
mysql_query($query);
}
}
} else {
$id = $_GET["id"];
}

$query = "SELECT Subscriber_ID, PhoneNumber, LastName, FirstName FROM (tablename) WHERE Owner = $owner AND Credits >= $cost";
$rowset = @mysql_query($query) or die(mysql_error());

echo("<table border=\"0\" cellpadding=\"0\" cellspacing=\"2\">\n");
echo("<form action=\"" . $PHP_SELF . "\" method=\"post\">\n");
echo("<input type=\"hidden\" name=\"id\" value=\"" . $id . "\">");
echo("<tr><td>");
echo("<select name=\"subscribers[]\" id=\"subscribers[]\" multiple multi STYLE=\"width: 250px;scroll:auto;\" SIZE=\"15\">\n");
while ($thisrow = mysql_fetch_object($rowset)) {

$subscriber_id = $thisrow->Subscriber_ID;
$phonenumber = $thisrow->PhoneNumber;
$lastname = $thisrow->LastName;
$firstname = $thisrow->FirstName;

( ... snip ... )

echo("<option value=\"" . $subscriber_id . "\"");
if($sub_flag == 1) {
echo(" SELECTED");
}
echo(">");
echo($phonenumber . " (" . $lastname . ", " . $firstname . ")");
echo("</option>\n");
$i++;
}
echo("</td></tr>");
echo("<tr><td align=\"right\">");
echo("<input type=\"submit\" value=\"Save »\">");
echo("</td></tr>");
echo("</form>");
echo("</table>\n");
?>

Notice the [] in the name of the selectbox... that's what does all the magic.


</PHP-Rant>
Subscribe
  • Post a new comment

    Error

    default userpic

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments