Vitalka Posted July 29, 2009 Share Posted July 29, 2009 Hello. I'm trying to write my script to add new users and I get following errors: Notice: Undefined index: fname in C:\wamp\www\add_user.php on line 52 Notice: Undefined index: lname in C:\wamp\www\add_user.php on line 58 I understand that it happens because I didn't assign values to those variables, but I can't assign them because values need come from the same form. How can I solve this errors? Here are the lines errors refer to: line52: <td><input type="text" name="fname" value="'.$_POST['fname'].'"></td> line58: <td><input type="text" name="lname" value="'.$_POST['lname'].'"></td> My goal is to have previously entered values show up in case a user needs to fix some errors or enter missing values. Here is my script: <?php //--------------------------------------------------------------------------------- if (isset($_POST['Submit'])){ //$fname = $_POST['fname']; //insert(); add_user_form(); } else { add_user_form(); } //--------------------------------------------------------------------------------- // array that holds possible errors $ferrors = array('First name missing.', 'Last name missing.', 'Login missing.', 'Your login name is taken. Choose a different login name.', 'Password is missing', 'Passwords did not match'); //----------------------------------------------------------------------------------- // function that opens connection and inserts data function insert(){ $connection = mysql_connect ("secret","secret","secret") or die(mysql_error()); // open connection $database = mysql_select_db("secret", $connection); // choose database $fname = $_POST['fname']; $lname = $_POST['lname']; $login = $_POST['login']; $password = $_POST['passworda']; $rank = $_POST['rank']; // insert data $query = mysql_query("INSERT INTO thm_accounts (id,login,fname,lname,password,rank) VALUES ('','$login','$fname','$lname','$password','$rank')") or die(mysql_error()); header( 'Location: ./user_list.php' ); } //----------------------------------------------------------------------------------- // function that displays form for adding users function add_user_form(){ echo ' <form name="add_user" action="./add_user.php" method="post"> <table border="1"> <tr> <td>First Name:</td> <td> </td> <td><input type="text" name="fname" value="'.$_POST['fname'].'"></td> <td rowspan="7"> </td> </tr> <tr> <td>Last Name:</td> <td> </td> <td><input type="text" name="lname" value="'.$_POST['lname'].'"></td> </tr> <tr> <td>Login Name:</td> <td> </td> <td><input type="text" name="login" value=""></td> </tr> <tr> <td>Choose a password:</td> <td> </td> <td><input type="password" name="passworda"></td> </tr> <tr> <td>Re-enter password:</td> <td> </td> <td><input type="password" name="passwordb"></td> </tr> <tr> <td>Choose rank:</td> <td> </td> <td><select name="rank"> <option value=""></option <option value="3">1</option> <option value="3">2</option> <option value="1">3</option> </select></td> </tr> <tr> <td> </td> <td> </td> <td><input type="Submit" name="Submit" value="Add User"></td> </tr> </table> </form> '; } //----------------------------------------------------------------------------------- ?> Link to comment https://forums.phpfreaks.com/topic/167921-solved-help-with-keeping-values-in-form-fields/ Share on other sites More sharing options...
play_ Posted July 29, 2009 Share Posted July 29, 2009 $_POST is inside the scope of the function. try doing this function add_user_form(){ global $_POST; .... Link to comment https://forums.phpfreaks.com/topic/167921-solved-help-with-keeping-values-in-form-fields/#findComment-885677 Share on other sites More sharing options...
fooDigi Posted July 29, 2009 Share Posted July 29, 2009 well they really aren't "errors", they are just notices. which you can shut off in the php configuration. but if you can't or would rather not, see if they exist first... ... using shorthand if/else statement, echo an empty string if it isn't set ... <td><input type="text" name="fname" value="'.(isset($_POST['fname'])?$_POST['fname']:'').'"></td> Link to comment https://forums.phpfreaks.com/topic/167921-solved-help-with-keeping-values-in-form-fields/#findComment-885709 Share on other sites More sharing options...
Vitalka Posted July 29, 2009 Author Share Posted July 29, 2009 alright, i tried your suggestions and here are the results: Did not work, I was still getting notices. function add_user_form(){ global $_POST; The following worked. $fname = (isset($_POST['fname'])?$_POST['fname']:''); $lname = (isset($_POST['lname'])?$_POST['lname']:''); <td><input type="text" name="fname" value="'.$fname.'"></td> <td><input type="text" name="lname" value="'.$lname.'"></td> I would be ever grateful if any one would explain syntax that fooDigi proposed. I'm aware of what isset is used for but what about that question mark? Is it a part of isset? I shall look it up in php manual as well. Link to comment https://forums.phpfreaks.com/topic/167921-solved-help-with-keeping-values-in-form-fields/#findComment-886220 Share on other sites More sharing options...
MatthewJ Posted July 29, 2009 Share Posted July 29, 2009 It is called a Ternary http://us2.php.net/ternary Just scroll the page down a bit and you'll see it. Link to comment https://forums.phpfreaks.com/topic/167921-solved-help-with-keeping-values-in-form-fields/#findComment-886225 Share on other sites More sharing options...
Vitalka Posted July 29, 2009 Author Share Posted July 29, 2009 Thank you for the link. I also found a more watered down explanation for this here: http://en.wikipedia.org/wiki/Ternary_operation I understand that it works as a single line if statement. $variable = condition ? if true : if false Link to comment https://forums.phpfreaks.com/topic/167921-solved-help-with-keeping-values-in-form-fields/#findComment-886237 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.