almystersv Posted January 8, 2008 Share Posted January 8, 2008 Hi Guys, In many of my pages i am building, for example the ADD NEW EMPLOYEE page, i have a number of error messages that appear if the user leaves a text box blank. The problem I have is that If the user does leave a text box empty and the relevant text box is displayed then it completely clears what they have written so they must start again. How do I keep the text that has already been put into the form when these messages are displayed? Here is my code for the add user form.. <?php session_start(); if (isset($_SESSION['usernameAdmin']) == false){ header("Location: login.php"); exit(); } require "connect.php"; ?> <!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> <title>Add User</title> </head> <body> <?php include ("headerAdmin.php"); ?> <h1 align="center" class="style1"> </h1> <form action="addUserQuery.php" method="post"> <table width="100%" border="0"> <tr> <td> </td> <td> <h2 align="left">Create User</h2></td> <td> </td> </tr> <tr> <td width="28%"> </td> <td width="31%"> </td> <td width="41%"> </td> </tr> <tr> <td>Title:</td> <td><input name="title" type="text" /></td> <td><span class="style2"> <?php if(isset($_GET['message1'])) { echo $_GET['message1']; } ?> </span></td> </tr> <tr> <td> </td> <td> </td> <td> </td> </tr> <tr> <td>First Name:</td> <td><input name="fName" type="text" /></td> <td><span class="style2"> <?php if(isset($_GET['message2'])) { echo $_GET['message2']; } ?> </span></td> </tr> <tr> <td> </td> <td> </td> <td> </td> </tr> <tr> <td>Surname:</td> <td><input name="sName" type="text" /></td> <td><span class="style2"> <?php if(isset($_GET['message3'])) { echo $_GET['message3']; } ?> </span></td> </tr> <tr> <td> </td> <td> </td> <td> </td> </tr> <tr> <td>Job Role:</td> <td><input name="jobRole" type="text" /></td> <td><span class="style2"> <?php if(isset($_GET['message4'])) { echo $_GET['message4']; } ?> </span></td> </tr> <tr> <td> </td> <td> </td> <td> </td> </tr> <tr> <td>Username:</td> <td><input name="username" type="text" /></td> <td><span class="style2"> <?php if(isset($_GET['message5'])) { echo $_GET['message5']; } ?> </span></td> </tr> <tr> <td> </td> <td> </td> <td> </td> </tr> <tr> <td>Password:</td> <td><input name="password" type="password" /></td> <td><span class="style2"> <?php if(isset($_GET['message6'])) { echo $_GET['message6']; } ?> </span></td> </tr> <tr> <td> </td> <td><?php if(isset($_GET['message7'])) { echo $_GET['message7']; } ?></td> <td><input name="Save" type="submit" value="Save" /></td> </tr> </table> </form> </body> </html> And here is the code that runs the query... <?php require "connect.php"; $title = $_POST['title']; $fName = $_POST['fName']; $sName = $_POST['sName']; $jobRole = $_POST['jobRole']; $username = $_POST['username']; $password = $_POST['password']; if($title == "") { $message1 = "Please enter the employees title"; header("Location: UserADD.php?message1=$message1"); exit(); } if($fName == "") { $message2 = "Please enter the employees first name"; header("Location: UserADD.php?message2=$message2"); exit(); } if($sName == "") { $message3 = "Please enter the employees surname"; header("Location: UserADD.php?message3=$message3"); exit(); } if($jobRole == "") { $message4 = "Please enter the employees job role"; header("Location: UserADD.php?message4=$message4"); exit(); } if($username == "") { $message5 = "Please enter your Username"; header("Location: UserADD.php?message5=$message5"); exit(); } if($password == "") { $message6 = "Please enter your Password"; header("Location: UserADD.php?message6=$message6"); exit(); } else if($title == !null || $fName == !null || $sName == !null || $jobRole == !null || $username == !null || $password == !null) { $query = "insert into employee values ('','".$title."','".$fName."','".$sName."','".$jobRole."','".$username."','".$password."','y')"; $result = mysql_query($query, $connection) or die ("Unable to perform query<br>$query"); $message7 = "'".$fName."' '".$sName."' added successfully. "; header("Location: UserADD.php?message7=$message7"); exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/85011-displaying-error-messages-without-losing-what-the-user-has-typed-in-the-text-box/ Share on other sites More sharing options...
priti Posted January 8, 2008 Share Posted January 8, 2008 hi, 1. start your session 2. register all variable in session so you can easily access them when you will go again on the same page. i.e $title,$fName,$sName,$jobRole............. $_SESSION['title']=$title; let me explain it will ex. <form action='filename.php' method='post> <input type='text' name='txttile' value="<?php $_SESSION['title'] ?>"> </form> in filename.php $title=$_POST['title']; $_SESSION['title']=$title; and rest f the code... . . . . . . . . . . . . . .. . ...................... Regards Quote Link to comment https://forums.phpfreaks.com/topic/85011-displaying-error-messages-without-losing-what-the-user-has-typed-in-the-text-box/#findComment-433498 Share on other sites More sharing options...
almystersv Posted January 8, 2008 Author Share Posted January 8, 2008 Hello, Correct me if i have misunderstood your solution, but i do not want the text box to autofill with the variables of the user that is currently logged in, it is an administrational function on the site where the admin can add new users to the system. I just want the text the user has entered into the form to stay when the error messages appear. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/85011-displaying-error-messages-without-losing-what-the-user-has-typed-in-the-text-box/#findComment-433521 Share on other sites More sharing options...
Ken2k7 Posted January 8, 2008 Share Posted January 8, 2008 Then don't use header(). Just display the information with echo and then user what priti said: <form action='filename.php' method='post> <input type='text' name='txttile' value="<?php $_SESSION['title'] ?>"> </form> You can say $_POST['title'] as well, I think. Quote Link to comment https://forums.phpfreaks.com/topic/85011-displaying-error-messages-without-losing-what-the-user-has-typed-in-the-text-box/#findComment-433528 Share on other sites More sharing options...
almystersv Posted January 8, 2008 Author Share Posted January 8, 2008 ??? Sorry. But isnt that value="<?php $_SESSION['title'] ?>" just going to pre-fill the textbox with the title of the user that is currently logged in?! Quote Link to comment https://forums.phpfreaks.com/topic/85011-displaying-error-messages-without-losing-what-the-user-has-typed-in-the-text-box/#findComment-433531 Share on other sites More sharing options...
nikefido Posted January 8, 2008 Share Posted January 8, 2008 I'm not clear - the problem is your error messages redirect to new page, and when they go back to the form it's not longer filled out? Whether or not the data in the form is filled out or not still depends on the browser, no? Anyway.... the most elegant solution to this (elegant = PITA ) would be to use AJAX to validate your form. This requires knowledge of JS, however (and some cross-browser issues that need to be addressed but are well known and not insurmountable). This would work well because AJAX will not refresh or redirect your page, which I assume is causing you to lose the data in the form edit: in fact, you might want to consider looking up some javascript so you can do all form validation with it - this will run the validation on the client-side so you don't need to worry about server performance when validating. This would also eliminate the need for using AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/85011-displaying-error-messages-without-losing-what-the-user-has-typed-in-the-text-box/#findComment-433648 Share on other sites More sharing options...
revraz Posted January 8, 2008 Share Posted January 8, 2008 The $_POST variable will work fine, if you submit and there is a error, as long as you echo the $_POST data as the value, it will put it in the input box for you. Quote Link to comment https://forums.phpfreaks.com/topic/85011-displaying-error-messages-without-losing-what-the-user-has-typed-in-the-text-box/#findComment-433649 Share on other sites More sharing options...
simcoweb Posted January 8, 2008 Share Posted January 8, 2008 Use this code for each of your form fields if you want whatever you've posted to remain in the boxes in the event there's an error that returns you back to the form so you don't have to fill it out again. This sample is for the 'Phone' field. It won't display anything unless the form has actually been submitted. <input type="text" name="Phone" size="20" value="<?php if (isset($_POST['Phone'])) echo $_POST['Phone']; ?>"> Quote Link to comment https://forums.phpfreaks.com/topic/85011-displaying-error-messages-without-losing-what-the-user-has-typed-in-the-text-box/#findComment-433652 Share on other sites More sharing options...
revraz Posted January 8, 2008 Share Posted January 8, 2008 Don't even need the if statement, if there is nothing in it, it will be blank. Quote Link to comment https://forums.phpfreaks.com/topic/85011-displaying-error-messages-without-losing-what-the-user-has-typed-in-the-text-box/#findComment-433655 Share on other sites More sharing options...
almystersv Posted February 8, 2008 Author Share Posted February 8, 2008 Hi, Thanks for looking at this. I have editted the code on my page as so... But it still clears the text boxes if once the submit button has been hit an empty field exists and the respective error message appears. Here is my code. <?php session_start(); if (isset($_SESSION['username']) == false && ($_SESSION['type']) == 'user'){ header("Location: login.php"); exit(); } require "connect.php"; ?> <!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> <title>Add User</title> <link rel="stylesheet" type="text/css" href="mystylelogin.css" /> <style type="text/css"> <!-- #Layer2 { position:absolute; width:830px; height:115px; z-index:1; left: 72px; top: 322px; } .style1 {font-family: Verdana, Arial, Helvetica, sans-serif} #Layer3 { position:absolute; width:200px; height:115px; z-index:2; } .style2 {color: #FF0000} #Layer19 { position:absolute; width:199px; height:48px; z-index:3; left: 68px; top: 261px; } --> </style> </head> <body> <?php include ("headerAdmin.php"); ?> <div id="Layer3"> <img src="images/AdminMenuCreateUser.gif" border="0" usemap="#Map" /> <map name="Map" id="Map"><area shape="rect" coords="162,48,292,84" href="UserEDIT.php" /> <area shape="rect" coords="344,53,473,79" href="ProductADD.php" /> <area shape="rect" coords="500,49,628,81" href="ProductEDIT.php?var=URN" /> <area shape="rect" coords="678,52,800,80" href="TaskAdmin.php?var=taskID" /> <area shape="rect" coords="825,8,946,46" href="logout.php" /> <area shape="rect" coords="844,52,954,79" href="welcome.php?var=msgDate" alt="Click here to Access the Main System" /> </map></div> <h1 align="center" class="style1"> </h1> <div id="Layer19"> <h1>Create User</h1></div> <form action="addUserQuery.php" method="post"> <div id="Layer2"> <table width="100%" border="0"> <hr /> <tr> <td width="17%">Title:</td> <td width="37%"><select name="title"> <option >Title</option> <option >Mr</option> <option >Mrs</option> <option >Ms</option> <option >Miss</option> </select></td> <td width="7%"> </td> <td width="17%">Username:</td> <td width="22%"><input name="username" type="text" value="<?php if (isset($_POST['username'])) echo $_POST['username'];?>" /></td> </tr> <tr> <td> </td> <td><span class="style2"> <?php if(isset($_GET['message1'])) { echo $_GET['message1']; } ?> </span></td> <td> </td> <td> </td> <td><span class="style2"> <?php if(isset($_GET['message5'])) { echo $_GET['message5']; } ?> </span></td> </tr> <tr> <td>First Name:</td> <td><input name="fName" type="text" value="<?php if (isset($_POST['fName'])) echo $_POST['fName'];?>" /></td> <td> </td> <td>Password:</td> <td><input name="password" type="password" value="<?php if (isset($_POST['password'])) echo $_POST['password'];?>" /></td> </tr> <tr> <td> </td> <td><span class="style2"> <?php if(isset($_GET['message2'])) { echo $_GET['message2']; } ?> </span></td> <td> </td> <td> </td> <td><span class="style2"> <?php if(isset($_GET['message6'])) { echo $_GET['message6']; } ?> </span></td> </tr> <tr> <td>Surname:</td> <td><input name="sName" type="text" value="<?php if (isset($_POST['sName'])) echo $_POST['sName'];?>" /></td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><span class="style2"> <?php if(isset($_GET['message3'])) { echo $_GET['message3']; } ?> </span></td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td>Job Role:</td> <td><input type="text" name="jobRole" size="50" value="<?php if (isset($_POST['jobRole'])) echo $_POST['jobRole']; ?>"></td> <td> </td> <td>Access: </td> <td><select name="type"> <option >Access</option> <option >user</option> <option >admin</option> </select></td> </tr> <tr> <td> </td> <td><span class="style2"> <?php if(isset($_GET['message4'])) { echo $_GET['message4']; } ?> </span></td> <td> </td> <td> </td> <td><span class="style2"> <?php if(isset($_GET['message8'])) { echo $_GET['message8']; } ?> </span></td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><?php if(isset($_GET['message7'])) { echo $_GET['message7']; } ?></td> <td> </td> <td> </td> <td><input name="Save" type="submit" value="Create User" /></td> </tr> </table> </div> </form> </body> </html> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/85011-displaying-error-messages-without-losing-what-the-user-has-typed-in-the-text-box/#findComment-462068 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.