...

View Full Version : removing web form fields with no value



steviecee
06-28-2005, 02:56 PM
I want to search a database using multiple user inputs from a web form. if the user decides to search the db using only, say, 3 out of 4 fields is there anyway php can work through the submitted data and pick the fields with inputs, or delete the ones without input (ie "") any ideas or links to tutorils will be great
cheers
steve

SeeIT Solutions
06-28-2005, 03:33 PM
name the form elements as an array like this


<input name="search[]" type="text">
<input name="search[]" type="text">
<select name="search[]"></select>

..and so on

then in your php page use this


<?php
if (isset($_POST['search'])) {
$count = 0;
for ($i = 0;$i < count($_POST['search']);$i++) {
if ($i != "") {
if ($count > 0) {
$searchstr .= " AND";
} else {
$searchstr = " WHERE";
$count++;
}
searchstr .= " `column` = ".$_POST['search'][$i];
}
}
$query = "SELECT * FROM `table`";
if (isset($searchstr)) { $query .= "$searchstr"; }
//
//run query etc
//
}

devinemke
06-28-2005, 03:36 PM
<?php
if (!isset($_POST['submit']))
{
echo '
<form action="" method="POST">
field 1: <input type="text" name="fields[1]"><br>
field 2: <input type="text" name="fields[2]"><br>
field 3: <input type="text" name="fields[3]"><br>
field 4: <input type="text" name="fields[4]"><br>
field 5: <input type="text" name="fields[5]"><br>
<input type="submit" name="submit" value="submit">
</form>
';
}
else
{
echo 'you filled in the following fields:<br><br>';
foreach ($_POST['fields'] as $key => $value)
{
if ($value != '') {echo 'field #' . $key . ' = ' . $value . '<br>';}
}
}
?>

steviecee
06-28-2005, 03:59 PM
i'm getting a parse error for this line:

searchstr .= " type = ".$_POST['search'][$i];

i've changed the'column' for the column name i want to begin the search with, is this right?
cheers
steve

Fou-Lu
06-28-2005, 04:31 PM
Its not a variable:


$searchstr .= " `column` = " . $_POST['search'][$i];

Also, depending on your field types, I believe that post will send empty fields as null. Since null is technically an input, instead of using an isset($_POST ) method, I'd recommend using an !empty($_POST ) method instead.

steviecee
06-28-2005, 04:49 PM
i've changed that, but still the same error ie:
Parse error: parse error in C:\Server\Apache2\local-html\search_york.php on line 240

it still refers to the line below:
searchstr .= " 'column' = ".$_POST['search'][$i];

so its got me totally baffled!!
thanks for all the help, any other ideas would be very much appreciated
steve

SeeIT Solutions
06-28-2005, 04:54 PM
use this instead, make sure the quotes around the column name are ` and not '


$searchstr .= " `type` = '" . $_POST['search'][$i]."'";//i assume type is the name of your colum

Also, remember to change the table name in this line if you haven't


$query = "SELECT * FROM `table`";

steviecee
06-28-2005, 05:09 PM
thanks for that, that is working to a point now! however it brings up all entries in the db no matter what i input and whether the entry corrosponds to the info inputted or not, the full php code is:



if (!empty($_POST['search']))
{
$count = 0;
for ($i = 0;$i < count($_POST['search']);$i++)
{
if ($i != "")
{
if ($count > 0)
{
$searchstr .= " AND";
}
else
{
$searchstr = " WHERE";
$count++;
}
$searchstr .= " `type` = '" . $_POST['search'][$i]."'";

}
}
$query = "SELECT * FROM 'location'";
if (isset($searchstr)) { $query .= "$searchstr"; }

$query_result=mysql_query( "select location_id, owner_id, loc_name, type, address_1, address_2, city, county, post_code from location");
}

while($row=mysql_fetch_assoc($query_result))
{
echo "<tr>";
echo "<td>".$row['location_id']."</td>";
echo "<td>".$row['owner_id']."</td>";
echo "<td>".$row['loc_name']."</td>";
echo "<td>".$row['type']."</td>";
echo "<td>".$row['address_1']."</td>";
echo "<td>".$row['address_2']."</td>";
echo "<td>".$row['city']."</td>";
echo "<td>".$row['county']."</td>";
echo "<td>".$row['post_code']."</td>";
echo "</tr>";
}

mysql_free_result ($query_result);
mysql_close();


Once again thanks for everyones tiime on this, it is appreciated!!
steve

SeeIT Solutions
06-28-2005, 05:45 PM
if (!empty($_POST['search']))
{
$count = 0;
for ($i = 0;$i < count($_POST['search']);$i++)
{
if ($i != "")
{
if ($count > 0)
{
$searchstr .= " AND";
}
else
{
$searchstr = " WHERE";
$count++;
}
$searchstr .= " `type` = '" . $_POST['search'][$i]."'";

}
}
$query = "SELECT location_id, owner_id, loc_name, type, address_1, address_2, city, county, post_code FROM location";
if (isset($searchstr)) { $query .= "$searchstr"; }

$query_result=mysql_query($query);
}

while($row=mysql_fetch_assoc($query_result))
{
echo "<tr>";
echo "<td>".$row['location_id']."</td>";
echo "<td>".$row['owner_id']."</td>";
echo "<td>".$row['loc_name']."</td>";
echo "<td>".$row['type']."</td>";
echo "<td>".$row['address_1']."</td>";
echo "<td>".$row['address_2']."</td>";
echo "<td>".$row['city']."</td>";
echo "<td>".$row['county']."</td>";
echo "<td>".$row['post_code']."</td>";
echo "</tr>";
}

mysql_free_result ($query_result);
mysql_close();

steviecee
06-29-2005, 11:14 AM
thanks for that. that has finally got rid of all the errors (hurray!!) it seems to be working fine
cheers again
steve



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum