Jump to content

php filter_has_var for validation help


webdevdea
Go to solution Solved by kicken,

Recommended Posts

could someone look at this and see if they can tell what I am doing wrong?

please and thanks :confused:  

<?php 

$chkerrors = FALSE;

if (!filter_has_var(INPUT_POST, "fName"));{
echo " please enter a value ";
$ckerrors = TRUE;
}

if (!filter_has_var(INPUT_POST, "lName"));{
echo " please enter a value ";
$ckerrors = TRUE;
}

if (!filter_has_var(INPUT_POST, "city"));{
echo " please enter a value ";
$ckerrors = TRUE;
}
if (!filter_has_var(INPUT_POST, "state"));{
echo " please enter a value ";
$ckerrors = TRUE;
}

if (!filter_has_var(INPUT_POST, "zipCode"));{

echo " please enter a value ";
$ckerrors = TRUE;
}
$fName = filter_input(INPUT_POST, "fName");
$lName = filter_input(INPUT_POST, "lName");
$city = filter_input(INPUT_POST, "city");
$state = filter_input(INPUT_POST, "state");
$zipCode = filter_input(INPUT_POST, "zipCode");

if($chkerrors == FALSE) {

echo <<<HERE
<h3> Hi there,  $fName $lName, <br /> so you live in $city,<br /> in the great state of $state.<br /> I hear the climate around $zipCode is great this time of year.
<br /> $fName, I hear that $state has alot to offer as far as recreation goes.<br />I hope that you have a great summer in $city. <br /></h3>
HERE;
}


?>
Link to comment
Share on other sites

Here is the HTML  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Assignment2</title>
<link rel = "stylesheet"
      type = "text/css"
      href = "whatsName.css" />
</head>
<body>
<h1>What's your name?</h1>
<h3>Writing a form for user input</h3>
<form method = "POST"
      action = "http://localhost/Assignments php class/Assignment2.php">
  <fieldset>
    <label>
      Please type your  first name:
    </label>
    <input type = "text"
           name = "fName"
           value = "" /> <br />
    Please type your  last name:
    <input type = "text"
           name = "lName"
           value = "" /> <br />
    Please type your  city:
    <input type = "text"
           name = "city"
           value = "" /> <br />
    Please type your  State:
    <input type = "text"
           name = "state"
           value = "" /> <br />
    Please type your  Zipcode:
    <input type = "text"
           name = "zipCode"
           value = "" /> <br />
    <input type = "submit" />
  </fieldset>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Assignment2</title>
</head>
<body>

<h3>Personal Information </h3>

<?php 
$chkerrors = FALSE;

if (!filter_has_var(INPUT_POST, "fName"));{
echo " please hit the back arrow and enter your name ";
$ckerrors = TRUE;
}

if (!filter_has_var(INPUT_POST, "lName"));{
echo " please enter a value ";
$ckerrors = TRUE;
}

if (!filter_has_var(INPUT_POST, "city"));{
echo " please enter a value ";
$ckerrors = TRUE;
}
if (!filter_has_var(INPUT_POST, "state"));{
echo " please enter a value ";
$ckerrors = TRUE;
}

if (!filter_has_var(INPUT_POST, "zipCode"));{

echo " please enter a value ";
$ckerrors = TRUE;
}
$fName = filter_input(INPUT_POST, "fName");
$lName = filter_input(INPUT_POST, "lName");
$city = filter_input(INPUT_POST, "city");
$state = filter_input(INPUT_POST, "state");
$zipCode = filter_input(INPUT_POST, "zipCode");

if($chkerrors == FALSE) {

echo <<<HERE
<h3> Hi there,  $fName $lName, <br /> so you live in $city,<br /> in the great state of $state.<br /> I hear the climate around $zipCode is great this time of year.
<br /> $fName, I hear that $state has alot to offer as far as recreation goes.<br />I hope that you have a great summer in $city. <br /></h3>
HERE;
}


?>

</body>
</html> 
Link to comment
Share on other sites

The usual practice for seeking help on a forum is to tell us what is wrong.  Not to ask us what "might" be wrong.  Give us any error messages your are getting back.  And don't give us oodles of code to try and follow.  Isolate your problem (thru your normal debugging methods) to the section of code you suspect and just give us that.  Please.

Link to comment
Share on other sites

When one has problem, one usually traces thru the program with echo statements to help you know what is going on with an exit at some point so that you can track how far you got and remove them and add more later on in the stream.  That works fine in a development environment when you don't have a real debugging tool.  But in this case I think I've shown you the problem.

Link to comment
Share on other sites

Good catch!

 

And I got to thinking - if you are not applying a Filter with the 'filter_has_var' function, why use it at all?  Perhaps the user is new and doesn't realize that there is a more straight-forward way to capture input values from a form?  I actually had not heard of these functions until now.

Link to comment
Share on other sites

Also - notes on "filter_has_var" say that an empty value returns true.  That could be your problem.

 

Why not :

$var='';

if (isset($_GET['var']))

   $var = $_GET['var']

 

if ($var == '')

   (show error)

Im lost as to where it says that an empty value returns true 

is that in the code like this?

 

if (!filter_has_var(INPUT_POST, "lName"));{
echo " please enter a value ";
$ckerrors = TRUE;
}
should I not have $ckerrors = TRUE 
?
Link to comment
Share on other sites

Just noticed this:

if (!filter_has_var(INPUT_POST, "fName"));{

All your if statements have ; after them, rendering them useless.

 

You could combine filter_has_var() and empty() also.

oh ok i will remove the ; from the code and see what happens, the book has us using the filter_has_var statement.. so its not an option to do it without that.. yes I am very new to this lanuguage.. two weeks  in.. 

Link to comment
Share on other sites

Now its not doing anything if you leave the fields empty but it is putting the information if if you put data into the fields  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Assignment2</title>
</head>
<body>

<h3>Personal Information </h3>

<?php 

$chkerrors = FALSE;

if (!filter_has_var(INPUT_POST, "fName")){
echo " please hit the back arrow and enter your name ";
$ckerrors = TRUE;
}

if (!filter_has_var(INPUT_POST, "lName")){
echo " please hit the back arrow and enter your Last Name ";
$ckerrors = TRUE;
}

if (!filter_has_var(INPUT_POST, "city")){
echo " please hit the back arrow and enter your city";
$ckerrors = TRUE;
}
if (!filter_has_var(INPUT_POST, "state")){
echo " please hit the back arrow and enter your state";
$ckerrors = TRUE;
}

if (!filter_has_var(INPUT_POST, "zipCode")){

echo " please hit the back arrow and enter your zipcode ";
$ckerrors = TRUE;
}
$fName = filter_input(INPUT_POST, "fName");
$lName = filter_input(INPUT_POST, "lName");
$city = filter_input(INPUT_POST, "city");
$state = filter_input(INPUT_POST, "state");
$zipCode = filter_input(INPUT_POST, "zipCode");

if($chkerrors == FALSE) {

echo <<<HERE
<h3> Hi there,  $fName $lName, <br /> so you live in $city,<br /> in the great state of $state.<br /> I hear the climate around $zipCode is great this time of year.
<br /> $fName, I hear that $state has alot to offer as far as recreation goes.<br />I hope that you have a great summer in $city. <br /></h3>
HERE;
}


?>

</body>
</html> 
Link to comment
Share on other sites

Truly - since you are not doing a filter there is no sense using this new function.  See my example for handling input above.  I believe that's how most people process input.  That and escaping the potential "bad" data input that could go into a db.

 

as for your if statements - look them up in the php manual - the php programmer's best friend.

 

if (conditions)

{

    statement;

   statement;

}

else

{

  statement;

  statement;

}

 

Note the placement of the semis.

Link to comment
Share on other sites

Should I have the &&  at the end of each if statement? should I use else statements? 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Assignment2</title>
</head>
<body>

<h3>Personal Information </h3>

<?php 

$chkErrors = FALSE;

if (isset($_POST['fName'])){
echo " please hit the back arrow and enter your name ";
$chkErrors= TRUE;
}

if (isset($_POST['lName'])){
echo " please hit the back arrow and enter your Last Name ";
$chkErrors = TRUE;
}

if (isset($_POST['city'])){
echo " please hit the back arrow and enter your city";
$chkErrors = TRUE;
}
if (isset($_POST['state'])){
echo " please hit the back arrow and enter your state";
$chkErrors= TRUE;
}

if (isset($_POST['zipCode'])){

echo " please hit the back arrow and enter your zipcode ";
$chkErrors= TRUE;
}
$fName = filter_input(INPUT_POST, "fName");
$lName = filter_input(INPUT_POST, "lName");
$city = filter_input(INPUT_POST, "city");
$state = filter_input(INPUT_POST, "state");
$zipCode = filter_input(INPUT_POST, "zipCode");

if($chkErrors == FALSE) {

echo <<<HERE
<h3> Hi there,  $fName $lName, <br /> so you live in $city,<br /> in the great state of $state.<br /> I hear the climate around $zipCode is great this time of year.
<br /> $fName, I hear that $state has alot to offer as far as recreation goes.<br />I hope that you have a great summer in $city. <br /></h3>
HERE;
}


?>

</body>
</html> 
Link to comment
Share on other sites

when i hit submit its not telling the user that they left a field empty and go back and fill it in..

filter_has_var does not check if the given field has been filled in, it merely checks if it was included with the submission. A field with an empty value is still posted to the server, so it passes that test. The isset() function works the same way.

 

If you want to check for an empty field, compare the value against the empty string ('')

if (!filter_has_var(INPUT_POST, 'fName') || $_POST['fName'] == ''){
  echo " please hit the back arrow and enter your name ";
  $chkErrors= TRUE;
}
Note that you can use empty(), mentioned above which is similar to the above. The main issue there is that an input of 0 would be considered empty. As such, any fields in which 0 is a valid input need to be processed as above, not using empty().
Link to comment
Share on other sites

When I hit submit I keeps saying hit the back error and enter your last name, even though the field has input?  


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Assignment2</title>
</head>
<body>
 
<h3>Personal Information </h3>
 
<?php 
 
$chkErrors = False;
 
if (!filter_has_var(INPUT_POST, 'fName') || $_POST['fName'] == ''){
  echo " please hit the back arrow and enter your  first name ";
  $chkErrors= TRUE;
}
else
 
if (!filter_has_var(INPUT_POST, 'lName') || $_POST['lname'] == ''){
  echo " please hit the back arrow and enter your last name ";
  $chkErrors= TRUE;
}
else
 
if (!filter_has_var(INPUT_POST, 'city') || $_POST['city'] == ''){
  echo " please hit the back arrow and enter your city ";
  $chkErrors= TRUE;
}
else
 
if (!filter_has_var(INPUT_POST, 'state') || $_POST['state'] == ''){
  echo " please hit the back arrow and enter your state";
  $chkErrors= TRUE;
}
else
 
 
if (!filter_has_var(INPUT_POST, 'zipCode') || $_POST['zipCode'] == ''){
  echo " please hit the back arrow and enter your zip ";
  
  
  $chkErrors= TRUE;
}
$fName = filter_input(INPUT_POST, "fName");
$lName = filter_input(INPUT_POST, "lName");
$city = filter_input(INPUT_POST, "city");
$state = filter_input(INPUT_POST, "state");
$zipCode = filter_input(INPUT_POST, "zipCode");
 
if($chkErrors == FALSE) {
 
echo <<<HERE
<h3> Hi there,  $fName $lName, <br /> so you live in $city,<br /> in the great state of $state.<br /> I hear the climate around $zipCode is great this time of year.
<br /> $fName, I hear that $state has alot to offer as far as recreation goes.<br />I hope that you have a great summer in $city. <br /></h3>
HERE;
}
 
 
?>
 
</body>
</html> 
Edited by webdevdea
Link to comment
Share on other sites

Am I getting close? 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Assignment2</title>
</head>
<body>

<h3>Personal Information </h3>

<?php 

$chkErrors = FALSE;

if (isset($_POST['fName'])){
echo " please hit the back arrow and enter your name ";
$chkErrors= TRUE;
}
else

if (isset($_POST['lName'])){
echo " please hit the back arrow and enter your Last Name ";
$chkErrors = TRUE;
}
else

if (isset($_POST['city'])){
echo " please hit the back arrow and enter your city";
$chkErrors = TRUE;
}
else

if (isset($_POST['state'])){
echo " please hit the back arrow and enter your state";
$chkErrors= TRUE;
}
else

if (isset($_POST['zipCode'])){

echo " please hit the back arrow and enter your zipcode ";
$chkErrors= TRUE;
}

$fName = filter_input(INPUT_POST, "fName");
$lName = filter_input(INPUT_POST, "lName");
$city = filter_input(INPUT_POST, "city");
$state = filter_input(INPUT_POST, "state");
$zipCode = filter_input(INPUT_POST, "zipCode");

if($chkErrors == FALSE) {

echo <<<HERE
<h3> Hi there,  $fName $lName, <br /> so you live in $city,<br /> in the great state of $state.<br /> I hear the climate around $zipCode is great this time of year.
<br /> $fName, I hear that $state has alot to offer as far as recreation goes.<br />I hope that you have a great summer in $city. <br /></h3>
HERE;
}


?>

</body>
</html> 

Link to comment
Share on other sites

Check this solution.. thanks for all our help I could never figure out the other options 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
body
{
background-color:#d0e4fe;
}
h1
{
color:orange;
text-align:center;
}
p
{
font-family:"Times New Roman";
font-size:20px;
}
</style>

<title>Validate-Assignment 3</title>

</head>
<body>
<h1>ASSIGNMENT 3
PHP FORM VALIDATION</h1>
<h3>Please put information into the fields.</h3>
<form method = "POST"
      action = "http://localhost/Assignments php class/die.php">
  <fieldset>
    <label>
      Please type your  first name:
    </label>
    <input type = "text"
           name = "fName"
           value = "" /> <br />
    Please type your  last name:
    <input type = "text"
           name = "lName"
           value = "" /> <br />
    Please type your  city:
    <input type = "text"
           name = "city"
           value = "" /> <br />
    Please type your  State:
    <input type = "text"
           name = "state"
           value = "" /> <br />
    Please type your  Zipcode:
    <input type = "text"
           name = "zipCode"
           value = "" /> <br />
    <input type = "submit" />
  </fieldset>
</form>
</body>
</html> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
body
{
background-color:#FF00FF;
}
h1
{
color:orange;
text-align:center;
}
p
{
font-family:"Times New Roman";
font-size:20px;
}
h1 {text-align:center;
}
</style>
<title>Assignment3</title>
</head>
<body>

<h1>Personal Information </h1>

<?php 
if (!$_POST['fName'] ) 
{
die('You did not complete The Name field, please go back');
}
if (!$_POST['lName'] ) 
{
die('You did not complete all of the Last Name fields please go back');
}
if (!$_POST['state'] ) 
{
die('You did not complete all of the state fields please go back');
}
if (!$_POST['city'] ) 
{
die('You did not complete all of the city please go back');
}
if (!$_POST['zipCode'] ) 
{
die('You did not complete all of the zipcodefields please go back');
}



$fName = filter_input(INPUT_POST, "fName");
$lName = filter_input(INPUT_POST, "lName");
$city = filter_input(INPUT_POST, "city");
$state = filter_input(INPUT_POST, "state");
$zipCode = filter_input(INPUT_POST, "zipCode");



echo <<<HERE
<h3> Hi there,  $fName $lName, <br /> so you live in $city,<br /> in the great state of $state.<br /> I hear the climate around $zipCode is great this time of year.
<br /> $fName, I hear that $state has alot to offer as far as recreation goes.<br />I hope that you have a great summer in $city. <br /></h3>
HERE;



?>

</body>
</html> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.