Jump to content

Problem with PHP script


indianlibra

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

 

;) ;)

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.