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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

foms in php problem 2

Status
Not open for further replies.

someoneneedshelps

Technical User
Mar 3, 2016
75
GB
Why does this show me the fields required message?

Code:
$urlstr = filter_input(INPUT_POST, 'urlstr');
$shippingcost = filter_input(INPUT_POST, 'shipping');

if (!empty($urlstr) && !empty($shippingcost))
{
   .....do code
}
elseif(isset($_POST['Submit'])) 
{
	echo "both fields required<br>";
}
else
{
   display form at astart...
}
 
Why does this show me the fields required message?

Because you check for them being empty before you check for the form being submitted.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Thanks, Feherke, I should have looked it up myself.

Well, assume the form is not submitted, then the elements are empty, so elseif runs, should detect no submit, so the last branch creating the form should run. I would check for submit first, too, though.

Bye, Olaf.
 
The ideal order of server-side form verification should be;


check for submit -> check for 'bot' submit -> check values -> process data

then should any step fail you can exit with an appropriate error code or message without trying to process unnecessarily or process erroneous data.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
What is wrong with this please? have two fields and both must have value but im getting lost, I have the submit button and the values think about and the form to display

Code:
$urlstr = filter_input(INPUT_POST, 'urlstr');
$shipcost = filter_input(INPUT_POST, 'shipcost');

if(!empty($urlstr) && !empty($shippingcost))
{
	echo $urlstr;
}
 elseif(isset($_POST['submit']) && empty($urlstr))
{
		echo "Enter url";
} 
else
{
?>
	 <form action="import.php" method="post">
	 <br><input type="text" name="shipcost" size=28 value="Enter Shipping Cost ie: 4.50" onclick="this.value=''"><br>
	 <br><input type="text" name="urlstr" size=180 value="Enter URL from Address bar top of the browser" onclick="this.value=''"><br>
	 
	  <input type="Submit">
	 </form>
	<?php
}
?>
 
even this don't work, just keeps going back to the form

Code:
$urlstr = filter_input(INPUT_POST, 'urlstr');
$shipcost = filter_input(INPUT_POST, 'shipcost');

if(isset($_POST['submit']))
{
	if(!empty($urlstr) && !empty($shipcost))
	{
		echo $urlstr;
	}
	 else
	{
			echo "Both fields must be entered";
	} 
}
else
{
?>
	 <form action="import.php" method="post">
	 <br><input type="text" name="shipcost" size=28 value="Enter Shipping Cost ie: 4.50" onclick="this.value=''"><br>
	 <br><input type="text" name="urlstr" size=180 value="Enter URL from Address bar top of the browser" onclick="this.value=''"><br>
	 
	  <input type="Submit">
	 </form>
	<?php
}
?>
 
Code:
var_dump($_POST);
See whether you spot s 'submit' element in that array dump.
Not?
Simple reason: $_POST['submit'] is never set, because you didn't give th Submit button the NAME 'submit'. The associative $_POST array only contains named form elements with their name as the element name.

Bye, Olaf.
 
....mmmh.. what does this var_dump say about my button?

Code:
array(3) { ["shippingcost"]=> string(0) "" ["urlstr"]=> string(0) "" ["submit"]=> string(12) "Submit Query" }
 
....mmmh.. what does this var_dump say about my button?

It says you have 3 values. 2 of which are empty (shippingcost, and urlstr), and a third one named "submit" that has a 12 character value of "Submit Query".





----------------------------------
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
 
....mmmh.. what does this var_dump say about my button?

var_dump($_POST) is not about your button, it's about the whole $_POST array. You have three input controls, so you have three array elements.
Submit Query is your submit button caption, obviously.

If you don't know that $_POST contains the other inputs, too, you haven't understood your own filter_input() calls.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top