CS GO
CS GO

Reputation: 912

php display error before form submission

i have a simple php script:

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <title>MY WEB</title>
</head>
<body>
    <form action="" method="get">
        <input type="text" name="textval" value="<?php if( isset($_GET['textval']) ) echo htmlspecialchars($_GET['textval']) ?>" />
        <input type="submit" value="Check" />
    </form>
</body>
</html>
<?php
if ( empty($_GET['textval']) ) {
    echo "Please Fill The TextBox" . PHP_EOL;  // displays this
}
else{
    echo "OKAY" . PHP_EOL;
}
?>

But php throws this error just after opening the page and i didn't submitted the form !!!

how to solve this ?

Upvotes: 1

Views: 3224

Answers (5)

Abhishek Salian
Abhishek Salian

Reputation: 934

according to your code

empty($_GET['textval'])

will always give TRUE before form submission. So first check if your form was submitted or not.

first change submit to

<input type="submit" name="submit" value="Check" />

now check if submit is valid or not

 if(isset($_GET['textval'])) {
     if(isset($_POST['submitted'])){
         if ( empty($_GET['textval']) ) {
               echo "Please Fill The TextBox" . PHP_EOL;
         }
         else{
               echo "OKAY" . PHP_EOL;
         }
     } 
    }

or for your code add a hidden input with name 'submitted' and check for that.

Upvotes: 0

Robert
Robert

Reputation: 20286

You can also use JQuery to validate if fields are empty. This will not reload the page.

$("form").submit(function (e){
    if($("input[type='text']").val().length == 0)
    {
       alert('fill form');
       return false;
    }
});

Upvotes: 0

Raj Mohan
Raj Mohan

Reputation: 543

Try this

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <title>MY WEB</title>
</head>
<body>
    <form action="" method="get">
        <input type="text" name="textval" value="<?php if( isset($_GET['textval']) ) echo htmlspecialchars($_GET['textval']) ?>" />
        <input type="submit" value="Check" />
    </form>
</body>
</html>
<?php
if(isset($_GET['submitted'])){
  if ( empty($_GET['textval']) ) {
    echo "Please Fill The TextBox" . PHP_EOL;
  }
  else{
    echo "OKAY" . PHP_EOL;
  }
}
?>

Upvotes: 1

Mr. Alien
Mr. Alien

Reputation: 157334

As you edited your code, obviously it will show that statement as if condition will return true, so in this case, assign a name to your submit button, and than use like

<input type="submit" value="Check" name="submitted" />

<?php
if(isset($_GET['submitted'])) {
   if (empty($_GET['textval'])) {
       echo "Please Fill The TextBox" . PHP_EOL;  // displays this
   }
   else{
       echo "OKAY" . PHP_EOL;
   }
}
?>

Note: It is better if you use POST instead of GET as this doesn't seem like $_GET suitable thing, generally $_GET is used for search pages.

Upvotes: 1

zavg
zavg

Reputation: 11071

Your $_GET['textval'] is initially empty, so empty($_GET['textval']) condition is true.

That is why you have error on the first page load.

Upvotes: 0

Related Questions