Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting Empty $_POST

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
1
16
US
I have a page with several forms but one is causing me no end of grief! I can't seem to figure out how to make it not show when nothing has been submitted to it. If one of the other forms is submitted, it triggers this one but without any specific values having been sent, it shows all entries. The query is built dynamically from many form options so it's impractical to do a conditional for each one and I'm not totally sure how the query itself can be reworked to show no results without breaking it.

Here is what I have; any ideas?

Code:
$FullQuery = $BaseQuery . $Where . $OrderBy;
	
if ($_POST) {
	if ($result = $mysqli->query($FullQuery)) {
		$count = $result->num_rows;
		if ($count > 0) {
			$SelectFile = "<form class=\"SearchResults\" name=\"SearchForm\" method=\"POST\" action=\"dcs_searchresults.php\">\n";
			$SelectFile .= "Devices found:<br>\n";
			$Query = $FullQuery;
			$i = 0;
			while ($row = $result->fetch_row()) {
				if ($row[3] && !$row[4]) {$Updated = date("M d Y g:i:s A", $row[3]);};
				if ($row[4]) {$Updated = date("M d Y at H:i:s", $row[4]);}
				$Shortname = basename($row[0], ".csv");
				$SelectFile .= "<input name=\"ID[]\" type=\"checkbox\" value=\"$row[1]\">$Shortname ($Updated)<br>\n";
				$i++;
			}
			$result->close();
		echo "<input type=\"submit\" name=\"Process Selected Files\" value=\"Process\">\n";
		echo "</form>\n\n";
		} elseif ($count == 0) {
			$SelectFile = "<div class=\"Message\">\n";
			$SelectFile .= "Sorry, no files were found. Please select or enter other parameters.\n";
			$SelectFile .= "</div>\n\n";
		}
		echo $SelectFile;	
	}
}
 
Code:
if (count($_POST) > 0):

endif;
 
Thank you. I had actually already tried that and, in fact, the query itself as wrapped within a conditional with exactly that which "elses" to one that gives no results.

I just noticed that when I add as as a test [bold]echo print_r($_POST)[/bold] that there is an odd numeric value coming from somewhere and it shows like this:

Code:
Array
(
    [Value1] => 
    [Value2] => 
    [Value3] => 
    [Value4] => 
    [Value5] => 
    [Value6] => 
)
1

I see nothing to account for the "1" that is after the array! There are no hidden fields of any sort so I guess I'll have to go through every line to be sure there is no typo anywhere.
 
boolean true will display as one.
the POST is being submitted because you have a form on the incoming page. text fields and the like (but not checkboxes) will be submitted whether or not there is anything in the form.
it sounds like you have your html structure a bit bizarre (why have an action on a page that submits an empty form if you don't want that form submitted) but if you are confident that it is how you want it, and that for example Value1 must have a value to constitute a submission, then test for that value

Code:
if (!empty($_POST['Value1'])):
//do something with incoming post values
endif;
 
Thank you. The form has about a dozen fields but unfortunately there is no single one that is required as long as at least one is used. It sounds like I'll need to write a conditional to account for each one which is troublesome because the form itself is dynamic and changes depending upon another user choice on a different form so the dozen fields will get multiplied several times!

I thought perhaps that the answer was in the query since it was written to pull up everything when there were no choices so I wrote another that pulls nothing by default. Still the same problem, though, since I can't seem to some up with any condition to determine if choices were made or not.

(Having said that, an ideal situation would be for the results to "stack up" with multiple submissions rather than being replaced with the new choices but I have no clue how to do that.)
 
You mentioned:
If one of the other forms is submitted,...

This leads me to believe that there are several forms there, but only a specific one should trigger that particular query?
If so can you not add a specific hidden input to that particular form, and look for it to run the query?

For instance:

HTML:
<form action="process.php" method="post">
<input type="text" name="somename">
<input type="hidden" name="form1Identfier" value="true">
<input type="submit" name="process" value="Process">
</form>


<form action="process.php" method="post">
<input type="text" name="someothername">
<input type="hidden" name="form2Identfier" value="true">
<input type="submit" name="process" value="Process">
</form>

PHP:
if(isset($_POST['form1Identifier']))
{
  echo "The First form was submitted";
}

if(isset($_POST['form2Identifier']))
{
  echo "The Second form was submitted";
}



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I thought of that and even started to do it but then realized that it would submit the value even if no choices were made so it would leave the same problem. The other forms on the page do not cause the problem.

By the way, this form submits to the same page because it creates a secondary form that does the actual work. The secondary form submits to the separate search results page and these is a third form for uploading and parsing CSV files. Right now there are only a few sample files but once there are hundreds, having them all show up will be a pain!
 
the question has become too vague. it would help significantly if you provided code and appropriate logic
 
I thought of that and even started to do it but then realized that it would submit the value even if no choices were made so it would leave the same problem. The other forms on the page do not cause the problem.

By the way, this form submits to the same page because it creates a secondary form that does the actual work. The secondary form submits to the separate search results page and these is a third form for uploading and parsing CSV files. Right now there are only a few sample files but once there are hundreds, having them all show up will be a pain!
[bugeyed][shocked]

You totally lost me now. Like jpadie says we need code and a logic path or flow chart.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Since [bold]empty($POST)[/bold] doesn't truly report that the form is empty, I ended up creating a conditional for each field and, as the form is dynamic (it changes fields and field names depending upon another form's choice), I tried to make the basics more generic. It's not exactly elegant for it works as per the simplified sample following:

Code:
$FormSubmitted = "";
if (!empty($_POST['FileName'])) {$FormSubmitted = 1;}
if (!empty($_POST['Segment1'])) {$FormSubmitted = 1;}
if (!empty($_POST['Segment2'])) {$FormSubmitted = 1;}
if (!empty($_POST['Segment3'])) {$FormSubmitted = 1;}
if (!empty($_POST['Segment4'])) {$FormSubmitted = 1;}
if (!empty($_POST['Segment5'])) {$FormSubmitted = 1;}
if (!empty($_POST['Keywords'])) {$FormSubmitted = 1;}

Then I simply check on whether $FormSubmitted has a value and if it does, it runs the dynamic query but if it doesn't, it runs another query that looks for an entry with an [bold]ID = '-1'[/bold] which, of course, doesn't exist.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top