Forum Moderators: coopster
<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
$fields = $_POST['fields'];
if (count($fields) > 0) {
$content = $content . count($fields);
for ($i=0;$i<count($fields);$i++) {
$content = $content . "<li>$fields[$i]\n";
}
}
1
- o
<?
$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>
echo "<pre>";
print_r($_POST);
print_r($_GET);
echo "</pre>";
exit;
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
)
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.
baze
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.
<?
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! :)