1

I am new to the web developing world.Please bare w/ me for having some mistakes and insufficient knowledge.

I'm trying to study the validation process of a users input. Below is my Code:

<?php

if($_POST['formSubmit'] == "Search") 
{
  $errorMessage = "";

  if(empty($_POST['formName'])) 
  {
    $errorMessage .= "<li>No Input</li>";
  }

  $varName = $_POST['formName'];

  if(!empty($errorMessage)) 
  {
    echo("<p>There was an error with your form:</p>\n");
    echo("<ul>" . $errorMessage . "</ul>\n");
  } 

}

?>

<form action="index.php" method="post">
<input type="text" name="formName" value="<?=$varName;?>">
<input type="Submit" name="formSubmit" value=" Search">
</form>

What I think should happen is when the user click the search button without inputting anything an error message will pop-up, but I don't understand why its not responding or echoing the error message, i've check the names and values but Alas, Appreciate all the help/suggestions you could give tnx.

Thanks for the reply everyone, got it into working ^_^

2
  • 1
    The value of your submit button has an extra space in front, so the if block doesn't execute. Commented Dec 5, 2013 at 10:14
  • Also if you just compare $_POST['formSubmit'] in a if and for any reason someone doesn't pass any parameter with that name it will pop a warning. To avoid this you can check if any value was passed or not before comparing it using isset() Commented Dec 5, 2013 at 10:20

6 Answers 6

1

Your $_POST['formSubmit'] doesn't contain Search. It contains Search (with space in front of it).

Because of that PHP will never validate your form.

Sign up to request clarification or add additional context in comments.

1 Comment

wow didn't see that coming thank you sir, I feel like a total loser haha. It worked, Love this community thanks
1

This works:

<?php

if($_POST['formSubmit'] == "Search") 
{
  $errorMessage = "";

  if(empty($_POST['formName'])) 
  {
    $errorMessage .= "<li>No Input</li>";
  }

  $varName = $_POST['formName'];

  if(!empty($errorMessage)) 
  {
    echo("<p>There was an error with your form:</p>\n");
    echo("<ul>" . $errorMessage . "</ul>\n");
  } 

}

?>

<form action="index.php" method="post">
<input type="text" name="formName" value="<?=$varName;?>">
<input type="Submit" name="formSubmit" value="Search">
</form>

Comments

1

you need to use isset() function

<?php
if(isset($_POST['formSubmit'])) 
{
  $errorMessage = "";

  if(empty($_POST['formName'])) 
  {
   echo $errorMessage .= "No Input";
  }
 else
 {
    $varName = $_POST['formName'];
 } 

}

 ?>

<form action="index.php" method="post">
<input type="text" name="formName" value="<?=$varName;?>">
<input type="Submit" name="formSubmit" value="Search">
</form>

1 Comment

still got the typo :p second line.
0

You giving wrong name to your submit button .

<input type="Submit" name="formSubmit" value=" Search">

it should be

<input type="Submit" name="formSubmit" value="Search">

Comments

0

Change this line

if($_POST['formSubmit'] == "Search") 

to

if(isset($_POST['formSubmit'])) 

Comments

0

I am making some corrections. Please use this.

<?php
if($_POST['formSubmit'] == "Search") {
    $errorMessage = "";
    if(empty($_POST['formName'])) {
        $errorMessage .= "<li>No Input</li>";
    } else {
        $varName = $_POST['formName'];
    }
    if(!empty($errorMessage)) {
        echo("<p>There was an error with your form:</p>\n");
        echo("<ul>" . $errorMessage . "</ul>\n");
    } 
}
?>

<form action="index.php" method="post">
    <input type="text" name="formName" value="<?=$varName;?>">
    <input type="Submit" name="formSubmit" value="Search">
</form>
  1. Corrected the condition if($_POST['formSubmit'] == "Search")
  2. IF $_POST['formName'] is empty then no need for assigning so put it inside else condition.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.