Jump to content

[SOLVED] User Input Validation


oceans

Recommended Posts

Dear People,

 

Please help me with the following;

I am intending to make a form on a page, on pushing submit the forms gets posted  onto itself so that it checks for empty fields and prompts user to get all fields filled, BUT the filled value should stay, I did this easily on ASP, but could not figure out in PHP.

 

My code is:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

<?PHP

//Transfer Data from Screen to Memory

for ($i=1; $i<=2; $i++)

{

$Input[$i]=$_REQUEST["Txt".$i];

echo $Input[$i];

}

?>

<form id="form1" name="form1" method="post" action="LogIn.php">

  <table width="499" border="0" cellpadding="5" cellspacing="0">

    <tr>

      <td width="131">Email Address </td>

      <td width="348"><input name="Txt1" type="text" id="Txt1" value="<?PHP $Input[1] ?>" size="30" maxlength="30"/></td>

    </tr>

    <tr>

      <td>Password</td>

      <td><input name="Txt2" type="password" id="Txt2" value="<?PHP $Input[2] ?>" size="30" maxlength="30"/></td>

    </tr>

    <tr>

      <td> </td>

      <td>

<?PHP

for ($i=1; $i<=2; $i++)

{

if ($Input[$i]=="")

{

echo"All Fields Should be Filled !";

break;

}

else

{

/*$con = mysql_connect("localhost","peter","abc123");

if ($con)

{

 

}

else

{

die('Could not connect: ' . mysql_error());

}

*/

}

}

 

?>

 

 

 

    </td>

    </tr>

    <tr>

      <td> </td>

      <td><input type="submit" name="Submit" value="Submit" /></td>

    </tr>

  </table>

</form>

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/47693-solved-user-input-validation/
Share on other sites

Thanks,

the error on the first run of the page is "PHP Notice: Undefined index: Txt1 in C:\Inetpub\wwwroot\MyPHP\Member\LogIn.php on line 13 PHP Notice: Undefined index: Txt2 in C:\Inetpub\wwwroot\MyPHP\Member\LogIn.php on line 13 "

but when filled with the values the error clears. you will uinderstnad if you stick my code to you dream weaver and run once

 

Try this out - in place of:

 

<?PHP
//Transfer Data from Screen to Memory
for ($i=1; $i<=2; $i++)
{
   $Input[$i]=$_REQUEST["Txt".$i];
   echo $Input[$i];
}
?>

 

Use:

 

<?PHP
//Transfer Data from Screen to Memory
for ($i=1; $i<=2; $i++)
{
if($input[$i]!="") {

   $Input[$i]=$_REQUEST["Txt".$i];
   echo $Input[$i];
   
   }
}
?>

 

That way - if the fields are blank, it will not perform that function.

Thanks for you help,

 

I get more errors

"

PHP Notice: Undefined variable: input in C:\Inetpub\wwwroot\MyPHP\Member\0101LogIn.php on line 17 PHP Notice: Undefined variable: input in C:\Inetpub\wwwroot\MyPHP\Member\0101LogIn.php on line 17 PHP Notice: Undefined variable: Input in C:\Inetpub\wwwroot\MyPHP\Member\0101LogIn.php on line 28 PHP Notice: Undefined variable: Input in C:\Inetpub\wwwroot\MyPHP\Member\0101LogIn.php on line 41

"

 

I understand what is happeneing, when the page first runs from top to bottom, it sees my assignment before the declaration of the variable.

I get the same error on the page

 

"PHP Notice: Undefined index: Txt1 in C:\Inetpub\wwwroot\MyPHP\Member\0101LogIn.php on line 13"

 

on the very first run, even before push a button. once i push the button this error goes away, thus I think the call for the variable "Txt1" before the computer reads the form could be the issue here, I could be wrong I was thinking logically here, if not may you suggest alternative for my motive, thanks

 

Dear occeans i check on my pc its ok, working fine.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?PHP
//Transfer Data from Screen to Memory
for ($i=1; $i<=2; $i++)
{
   $Input[$i]=$_POST["Txt".$i];
   echo $Input[$i];
}
?>
<form id="form1" name="form1" method="post" action="session.php">
  <table width="499" border="0" cellpadding="5" cellspacing="0">
    <tr>
      <td width="131">Email Address </td>
      <td width="348"><input name="Txt1" type="text" id="Txt1" value="<?PHP $Input[1] ?>" size="30" maxlength="30"/></td>
    </tr>
    <tr>
      <td>Password</td>
      <td><input name="Txt2" type="password" id="Txt2" value="<?PHP $Input[2] ?>" size="30" maxlength="30"/></td>
    </tr>
    <tr>
      <td> </td>
      <td>
<?PHP
for ($i=1; $i<=2; $i++)
{
   if ($Input[$i]=="")
   {
      echo"All Fields Should be Filled !";
      break;
   }
   else
   {
      /*$con = mysql_connect("localhost","peter","abc123");
      if ($con)
      {

      }
      else
      {
         die('Could not connect: ' . mysql_error());
      }
      */
   }
}

?>


      </td>
    </tr>
    <tr>
      <td> </td>
      <td><input type="submit" name="Submit" value="Submit" /></td>
    </tr>
  </table>
</form>
</body>
</html>

i think your using the wrong expression as your not telling the script what to do.

 

First u need the script to run after submit button which you have named submit.

 

if (isset($_POST['submit'])) {//checks if the submit button is clicked

  if (empty($_POST['text'])) {

 

echo "the text field is empty";

}

else { //Enter your continueation code// this is what you want them to do if the field text is not empty

}

}

 

 

 

hope it will help you any how... ciao

Thanks Friend, Finally this works,

May I thank all those who spend time on this.

I am new to PHP, but no bias ASP is breeze in relation, but never the less I have to learn PHP as I was told it is cheaper to host PHP then ASP, so rain or shine I will learn, I hope you people will help me here and there.

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.