Jump to content

Recommended Posts

In the below script you see a string length counter

 

This script is working fine but i have a problem

 

When first time i open this page in my browser it show an error in line 2

(explained in image 2)

 

But when u enter the test string the error removed

 

I can't remove the 'test' variable

b'coz i have to work with it

 

Can anybody help me to to remove this erro line in the browser

 

 

<?php

$var = $_GET["test"];

$len = strlen($var);

 

if($len > 0)

{

echo $len;

}

else

{

echo "error: no input";

}

?>

<form action="var_test.php" method="GET">

<input type="text" name="test" >

<input type="submit" value="Click Here">

 

Image 1

untitledkc2.gif

 

Image 2

untitled2ab1.gif

 

Image 3

untitled3xb8.gif

Link to comment
https://forums.phpfreaks.com/topic/44530-problem-with-php-script/
Share on other sites

It is becuase when your first run the script the test variable is not set in the URI and this PHP displays the Undefined index notice message, but when you submit the form with filled filled in (or not) the test variable in the URI is then set in the URI

 

What you should do is check that the variable exists first before using it. You'd do this by using the isset function in an if statement. Take a look at the above example posted by papaface

try:

<?php
if (isset($_GET["test"]))   
{ 
$var = $_GET["test"];
   $len = strlen($var);
   
    echo $len;
}
?>
<form action="var_test.php" method="GET">
   <input type="text" name="test" >
   <input type="submit" value="Click Here">

 

 

thanks

 

now its without the 'Notice'

<?php

if($_GET){ 

$var = $_GET["test"];

  $len = strlen($var);

 

  if($len > 0)

  {

      echo $len;

  }

  else

  {

      echo "error: no input";

  }

}

?>

<form action="var_test.php" method="GET">

  <input type="text" name="test" >

  <input type="submit" value="Click Here">

 

-------------------------------

or

<?php

error_reporting(0);

...............

...............

?>

 

yesh i think this

'error reporting(0)'

 

is a goog trick

 

thanks

not its not. Do not ignore errors/messages PHP displays always try to fix them.

 

ya thats true

 

I mean by that

i just need to avoid the unwanted 'Notice' from my clean page

 

;) ;)

 

 

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.