Jump to content

[SOLVED] Help with keeping values in form fields.


Vitalka

Recommended Posts

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>&nbsp</td>
	<td><input type="text" name="fname" value="'.$_POST['fname'].'"></td>
	<td rowspan="7">&nbsp</td>
</tr>
<tr>
	<td>Last Name:</td>
	<td>&nbsp</td>
	<td><input type="text" name="lname" value="'.$_POST['lname'].'"></td>
</tr>
<tr>
	<td>Login Name:</td>
	<td>&nbsp</td>
	<td><input type="text" name="login" value=""></td>
</tr>
<tr>
	<td>Choose a password:</td>
	<td>&nbsp</td>
	<td><input type="password" name="passworda"></td>
</tr>
<tr>
	<td>Re-enter password:</td>
	<td>&nbsp</td>
	<td><input type="password" name="passwordb"></td>
</tr>
<tr>
	<td>Choose rank:</td>
	<td>&nbsp</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>&nbsp</td>
	<td>&nbsp</td>
	<td><input type="Submit" name="Submit" value="Add User"></td>
</tr>
</table>
</form>
';
}
//-----------------------------------------------------------------------------------
?>

 

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>

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.  8)

$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.

 

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

 

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.