Welcome to WebmasterWorld Guest from 3.229.122.166

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Processing multiple checkbox options?

     
2:29 am on Dec 7, 2004 (gmt 0)

Full Member

10+ Year Member

joined:Feb 28, 2001
posts:208
votes: 0


For seem reason this seems to be more difficult then it should be. I am trying to process a form that has a series of checkboxes. Obviously, more then one can be selected. Basic code for this is below:

<input type="checkbox" name="fields[]" value="name">Machine Name<br />
<input type="checkbox" name="fields[]" value="domain">Workgroup/Domain<br />
<input type="checkbox" name="fields[]" value="make">Make<br />
<input type="checkbox" name="fields[]" value="model">Model<br />
<input type="checkbox" name="fields[]" value="os">Operating System

Now, I thought that assigned the values to the variable field. I try and check it with this in the processing page:

$fields = $_POST['fields'];
if (count($fields) > 0) {
$content = $content . count($fields);
for ($i=0;$i<count($fields);$i++) {
$content = $content . "<li>$fields[$i]\n";
}
}

Unfortunately, this does not get me what I expect. When I echo out $content when Machine Name and Operating system are checked, for instance, I get the following output:

1
  • o


I'm sure I've made a simple mistake somewhere, but I can't spot it. Perhaps fresh eyes will help. :) Thanks.
3:57 am on Dec 7, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 26, 2004
posts:153
votes: 0


Guess I'd need to see the rest of your script. But here's a working example of what you posted (printed the array several ways just playing around):

<?
$fields = $_POST['fields'];
if (is_array($fields)) {
echo "<pre>";
print_r($fields);
echo "</pre>";
foreach ($fields as $key=>$val) {
echo "$key -> $val <br />";
}
$content = '<hr><br>';
$content = $content . count($fields);
for ($i=0;$i<count($fields);$i++) {
$content = $content . "<li>$fields[$i]\n";
}
echo $content;
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<form action="<? $_SERVER["PHP_SELF"];?>" method="post">
<input type="checkbox" name="fields[]" value="name">Machine Name<br />
<input type="checkbox" name="fields[]" value="domain">Workgroup/Domain<br />
<input type="checkbox" name="fields[]" value="make">Make<br />
<input type="checkbox" name="fields[]" value="model">Model<br />
<input type="checkbox" name="fields[]" value="os">Operating System
<input name="submit" type="submit" value="submit">
</form>
</body>
</html>

4:00 am on Dec 7, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8637
votes: 283


That's odd. I just plugged your code into a form and it worked fine for me. I think the problem is perhaps elsewhere in the script. Try the old standby. Add this to your script right at the top of the page that receives the data from the form.

echo "<pre>";
print_r($_POST);
print_r($_GET);
echo "</pre>";
exit;

11:28 pm on Dec 7, 2004 (gmt 0)

Full Member

10+ Year Member

joined:Feb 28, 2001
posts:208
votes: 0


Thanks for the suggestions. I have given them a shot and am still stumped.

I tried the is_array, and the if statement never executes, making me think for some reason, $fields is not coming back as an array.

using print_r($_POST); Generates the following when Make and Operating System are selected:

Array
(
[cst] =>
[fields] => os
[searchtype] => sc
)

(cst is for search terms. I didn't put any in, so it's blank) The first checkbox, Make, does not show up in fields.
2:50 am on Dec 8, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8637
votes: 283


Are you using the exact code listed above? I ask because that is not what you should be getting from print_r($_POST). You should get


Array
(
[cst] =>
[fields] => Array
(
[0] => make
[1] => model
)
)

You didn't do print_r($_POST['fields']) did you?

5:32 pm on Dec 8, 2004 (gmt 0)

Full Member

10+ Year Member

joined:Feb 28, 2001
posts:208
votes: 0


Yep, I just copied and pasted from your code above. The lack of Array status for $fields is what has me stumped. Why would this not be coming across as an array, and only storing the last value checked?
9:55 pm on Dec 8, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8637
votes: 283


Sorry man, I'm stumped. I use your exact code and get exactly the proper result.

Can you simplify the page back to the absolute bar bones and see if you can get that to work?

Tom

11:02 pm on Dec 9, 2004 (gmt 0)

Full Member

10+ Year Member

joined:Feb 28, 2001
posts:208
votes: 0


Ok, got it working. In the form, I had to add:
id="whatever"
to the checkbox options where "whatever" equals the contents of the "value" attribute. Seems kinda goofy, but it worked.
5:11 am on Dec 11, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8637
votes: 283


I'd like to see someone explain that!
5:50 pm on Dec 11, 2004 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


so would I. That really doesn't make any sense.

The only other thing that came to mind was using value="name" could be confusing something but I don't see that being the problem either.

The code works perfectly as it appears in the first post for me as well.

6:05 pm on Dec 11, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 26, 2004
posts:153
votes: 0


could it be client related? If $_POST is showing what is listed above, wouldn't that suggest that that might be all that was sent back from the browser?

baze

1:20 am on Dec 12, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8637
votes: 283


Well yes, it would, but have you ever come across a browser that could be set up to not send post data? I wouldn't even know how to stop FF, Moz, IE, Opera or any browser I've ever used from sending back form data without maybe some tricky javascript onsubmit functions that would clear the post array and that's not something I would do by accident.

In any case, the part that JK and I were thinking was so unexplainable above all is the fact that assigning an id somehow made it all work.

That makes me wonder if there was possibly some unmatched quote mark or something like that got sorted out.

evinrude, have you tried removing the id="something" attributes. Does that break it? I would be really interested to know.

1:35 am on Dec 12, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 26, 2004
posts:153
votes: 0


The id= thing is what got me to thinking along the lines of a buggy browser. I'm with you, can't think of what else would cause this situation. I'd be interested to see the raw paramlist as returned from the browser. I wonder if changing to GET would affect it at all... at least it would be easy to see what the browser is sending back.

baze

10:12 pm on Dec 14, 2004 (gmt 0)

Full Member

10+ Year Member

joined:Feb 28, 2001
posts:208
votes: 0


Hiya, sorry for not getting back to this for so long.

Removing the id attribute returns the page to its broken state. Frustrating to hear it's working for others without the editing. :P I, too, would assume it's a broken browser, except I can replicate it here with IE and FireFox. Perhaps it's something in my (unposted) code?

If there's interest, I can post both the form page code, and the processing page code.

1:10 am on Dec 15, 2004 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


Go ahead. I have been watching from the sidelines here and have come to a point now (along with others following this thread) that we need to see the code ;)
7:24 pm on Dec 15, 2004 (gmt 0)

Full Member

10+ Year Member

joined:Feb 28, 2001
posts:208
votes: 0


Ok, here goes. The two files are copied below. searchnew.php is the input page. Please excuse the crappy table based layout. The center search (search computers) is the one giving the problem.
----

<?
require_once "includes/dbconnection.php";

$sqlBuilding = "select name from buildings order by name asc";
$sqlRoom = "select distinct number from rooms order by number asc";
$Bresult = mysql_db_query("inventory", $sqlBuilding, $dbInventory) or die("Invalid Query!");
$Rresult = mysql_db_query("inventory", $sqlRoom, $dbInventory) or die("Invalid Query!");
while ($row = mysql_fetch_array($Bresult)) {
//build the Building Name select option
$bname = $bname . "<option value=\"$row[name]\">$row[name]</option>\n";
}
while ($rooms = mysql_fetch_array($Rresult)) {
//build the Room Number select option
$rnumber = $rnumber . "<option value=\"$rooms[number]\">$rooms[number]</option>\n";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional //EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>CAT Inventory - Search</title>
<link rel="stylesheet" type="text/css" href="cat.css" />
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="CAT Inventory - Add Building" />
<meta name="keywords" content="cat, inventory, center for academic technology, computers, equipment" />
</head>
<body>
<? require_once "includes/menu.html"?>
<div class="main">
<div align="center">
<h1>Search</h1>
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td colspan="3"><strong>Search By Room</strong></td>
</tr>
<tr>
<td>
<form name="searchroom" action="search_handler.php" method="post">
BUILDING:
<select name="bname">
<option value="">--Please Choose--</option>
<? echo $bname;?>
</select>
</td>
<td>
ROOM:
<select name="roomnumber">
<option value="">--Please Choose--</option>
<? echo $rnumber?>
</select>
</td>
<td>
<input type="hidden" name="searchtype" value="sr">
<input type="submit">
</td>
</form>
</tr>
</table>
<br />
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2">
<strong>Search for Computer</strong>
</td>
</tr>
<tr>
<td>
<form name="searchcomputer" action="test.php" method="post">
Search Term:
</td>
<td><input type="text" name="cst"></td>
</tr>
<tr>
<td colspan="2">Limit search to the following fields:</td>
</tr>
<tr>
<td align="left">
<input type="checkbox" name="fields[]" value="name">Machine Name<br />
<input type="checkbox" name="fields[]" value="workgroup">Workgroup/Domain<br />
<input type="checkbox" name="fields[]" value="make">Make<br />
<input type="checkbox" name="fields[]" value="model">Model<br />
<input type="checkbox" name="fields[]" value="os">Operating System
</td>
<td align="left">
<input type="checkbox" name="fields[]" value="serialnumber">Serial Number<br />
<input type="checkbox" name="fields[]" value="inventorynumber">Property Number<br />
<input type="checkbox" name="fields[]" value="ip">IP Address<br />
<input type="checkbox" name="fields[]" value="mac">MAC Address<br />
<input type="checkbox" name="fields[]" value="processor">Processor Type<br />
</td>
</tr>
<tr>
<td colspan="2">
<input type="hidden" name="searchtype" value="sc">
<input type="submit">
</td>
</form>
</tr>
</table>
<br />
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2">
<strong>Search for Equipment</strong>
</td>
</tr>
<tr>
<td>
<form name="searchequipment" action="search_handler.php" method="post">
Search Term:
</td>
<td>
<input type="text" name="est">
</td>
</tr>
<tr>
<td colspan="2">Limit search to the following fields:</td>
</tr>
<tr>
<td align="left">
<input type="checkbox" name="fields[]" value="make">Make<br />
<input type="checkbox" name="fields[]" value="model">Model<br />
<input type="checkbox" name="fields[]" value="func">function<br />
</td>
<td align="left" valign="top">
<input type="checkbox" name="fields[]" value="sn">Serial Number<br />
<input type="checkbox" name="fields[]" value="pn">Property Number<br />
</td>
</tr>
<tr>
<td colspan="2">
<input type="hidden" name="searchtype" value="se">
<input type="submit">
</td>
</form>
</tr>
</table>
</div>
</div>
</body>
</html>

This goes to test.php, which process and currently is just supposed to provide a list of checkbox elements.


<?
require_once "includes/dbconnection.php";

$type = $_POST['searchtype'];

// Determine the type
if ($type == "sr") {
//Ignore this if, it does not concern you! :)
} elseif ($type == "sc") {
$term = $_POST['cst'];
$fields = $_POST['fields'];
$content = "Your results: ";
if (count($fields) > 0) {
$content = $content . count($fields);
for ($i=0;$i<count($fields);$i++) {
$content = $content . "<li>$fields[$i]\n";
}
} else {
//more to come
}
} else {
//More to come!
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional //EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>CAT Inventory - Search Results</title>
<link rel="stylesheet" type="text/css" href="cat.css" />
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="CAT Inventory - Search Results" />
<meta name="keywords" content="cat, inventory, center for academic technology, computers, equipment" />
</head>
<body>
<? require_once "includes/menu.html"?>
<div class="main">
<div>
<? echo $content?>
</div>
</div>
</body>
</html>

Any comments are helpful. Like I said, I got this to work by placing id attributes in the checkbox elements. I'll also readilly admit, it's probably a stupid error on my part, or a screwy browser/computer.

//edit - Hmmm, neither the quote nor pre tag preserved formatting. I promise I don't chunk all my code together like that! :)

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members