oceans Posted April 19, 2007 Share Posted April 19, 2007 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> Quote Link to comment Share on other sites More sharing options...
btherl Posted April 19, 2007 Share Posted April 19, 2007 Try <?php echo $Input[1] ?> Quote Link to comment Share on other sites More sharing options...
oceans Posted April 19, 2007 Author Share Posted April 19, 2007 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 Quote Link to comment Share on other sites More sharing options...
oceans Posted April 19, 2007 Author Share Posted April 19, 2007 power thanks echo works, can you help me with the first run error. Quote Link to comment Share on other sites More sharing options...
JSHINER Posted April 19, 2007 Share Posted April 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
oceans Posted April 19, 2007 Author Share Posted April 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
JSHINER Posted April 19, 2007 Share Posted April 19, 2007 I hate to ditch out on this - but it's 3 am here - Should probably get some sleep. Plus, you don't want me helping you when I'm tired Never a good idea. But rest assured, someone here will help you out. Quote Link to comment Share on other sites More sharing options...
oceans Posted April 19, 2007 Author Share Posted April 19, 2007 Thanks friend, I hope somw one will come to my aid! Quote Link to comment Share on other sites More sharing options...
oceans Posted April 19, 2007 Author Share Posted April 19, 2007 Can any one help me on this, it appears tricky to me in PHP Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted April 19, 2007 Share Posted April 19, 2007 $_POST is more good $_REQUEST. try this: $Input[$i]=$_POST["Txt".$i]; Quote Link to comment Share on other sites More sharing options...
oceans Posted April 19, 2007 Author Share Posted April 19, 2007 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 Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted April 19, 2007 Share Posted April 19, 2007 This will do the trick. $Input[$i]=$_POST["Txt1".$i]; Quote Link to comment Share on other sites More sharing options...
oceans Posted April 19, 2007 Author Share Posted April 19, 2007 No not working, similar error I think our code "$Input[$i]=$_POST["Txt".$i];" is correct "$Input[$i]=$_POST["Txt1".$i];" is incorrect as there is no Txt11 in the form Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted April 19, 2007 Share Posted April 19, 2007 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> Quote Link to comment Share on other sites More sharing options...
cyrilsnodgrass Posted April 19, 2007 Share Posted April 19, 2007 When using $_POST (which is an array itself) I think you reference inividual elements as follows $input[$i] = $_POST['Txt'][$i]; Using a dot is concatenating the index to the value in earlier examples - I think anyway ! Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted April 19, 2007 Share Posted April 19, 2007 Nope I think this is the right: $Input[$i]=$_POST["Txt".$i]; Quote Link to comment Share on other sites More sharing options...
valtido Posted April 19, 2007 Share Posted April 19, 2007 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 Quote Link to comment Share on other sites More sharing options...
oceans Posted April 19, 2007 Author Share Posted April 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.