Undefined variable in each field box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
doug76
Forum Commoner
Posts: 26
Joined: Tue Aug 24, 2010 7:44 am

Undefined variable in each field box

Post by doug76 »

I have entered the code for a simple form with checking for empty fiels and valid phone number. I get an error code in each of the forms boxes:

<br /><b>Notice</b>: Undefined variable: firstname in <b> on line <b>138</b><br />

I get this repeated for each topic: lastname, email, phone, resume

I have tried using the line: $firstname = isset($_POST['firstname']) ? $_POST['firstname'] : ''; but this throws up even more problems as then I can't even submit the form.

The code below works except I get the error messages in the boxes. Functionality isn't effected

I assume there is a simple answer but cannot see where the problem lies. All names match:

All help much appreicated

[syntax=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" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<title>Risky Jobs - Registration</title>


<link rel="stylesheet" type="text/css" href="style.css" />

</head>

<body>

<img src="riskyjobs_title.gif" alt="Risky Jobs" />

<img src="riskyjobs_fireman.jpg" alt="Risky Jobs" style="float:right" />


<h3>Risky Jobs - Registration</h3>


<?php

if (isset($_POST['submit'])) {

$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$job = $_POST['job'];
$resume = $_POST['resume'];
$output_form = 'no';

if (empty($first_name)) {

// $first_name is blank

echo '<p class="error">You forgot to enter your first name.</p>';

$output_form = 'yes';

if (empty($last_name)) {

// $last_name is blank

echo '<p class="error">You forgot to enter your last name.</p>';

$output_form = 'yes';

}

if (empty($email)) {

// $email is blank

echo '<p class="error">You forgot to enter your email address.</p>';

$output_form = 'yes';

}


if(!preg_match('/^\(?[2-9]\d{2}\)?[-\s]\d{3}-\d{4}$/', $phone)) {
// $phone is invalid
echo '<p class="error">Your phone number is invalid.</p>';

$output_form = 'yes';

}

if (empty($job)) {

// $job is blank

echo '<p class="error">You forgot to enter your desired job.</p>';

$output_form = 'yes';

}


if (empty($resume)) {


// $resume is blank

echo '<p class="error">You forgot to enter your resume.</p>';

$output_form = 'yes';
}
}

else {

$output_form = 'yes';

}


if ($output_form == 'yes') {
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<p>Register with Risky Jobs, and post your resume.</p>

<table>

<tr>
<td><label for="firstname">First Name:</label></td>

<td><input id="firstname" name="firstname" type="text" value="<?php echo $first_name; ?>"/>
</td></tr>

<tr>
<td><label for="lastname">Last Name:</label></td>

<td><input id="lastname" name="lastname" type="text" value="<?php echo $last_name; ?>"/>
</td></tr>

<tr>
<td><label for="email">Email:</label></td>

<td><input id="email" name="email" type="text" value="<?php echo $email; ?>"/>
</td></tr>

<tr>
<td><label for="phone">Phone:</label></td>
<td><input id="phone" name="phone" type="text" value="<?php echo $phone; ?>"/>
</td></tr>

<tr>
<td><label for="job">Desired Job:</label>
</td>

<td><input id="job" name="job" type="text" value="<?php echo $job; ?>"/>
</td>
</tr>

</table>

<p>
<label for="resume">Paste your resume here:</label><br />

<textarea id="resume" name="resume" rows="4" cols="40">

<?php echo $resume; ?>
</textarea><br />


<input type="submit" name="submit" value="Submit" />
</p>
</form>


<?php

}
else if ($output_form == 'no') {
echo '<p>' . $first_name . ' ' . $last_name . ', thanks for registering with Risky Jobs!</p>';


// code to insert data into the RiskyJobs database...

}

?>
</body>
</html>
[/syntax]
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Re: Undefined variable in each field box

Post by jabbaonthedais »

Just define all your variables to be blank before you check if they are set. So at the top of your script put,

Code: Select all

$first_name = "";
$last_name = ""; 
$email = ""; 
$phone = ""; 
$job = ""; 
$resume = "" 
doug76
Forum Commoner
Posts: 26
Joined: Tue Aug 24, 2010 7:44 am

Re: Undefined variable in each field box

Post by doug76 »

So simple, so obvious. I can't believe I missed that.

Thanks very much
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Undefined variable in each field box

Post by John Cartwright »

At the top of my "scripts" where I expect user input, I use the ternary to initialize and add default values to input.

Code: Select all

$first_name = !empty($_POST['firstname']) ? $_POST['firstname'] : '';
$last_name = !empty($_POST['lastname']) ? $_POST['lastname'] : '';
//etc
Post Reply