Page 1 of 1

Undefined variable

Posted: Thu Oct 07, 2010 1:32 pm
by doug76
Hello,
I think there may be a simple answe but I have been saring at this code for the last couple fo hours and canot see the error. I am going cross eyed.

I have writtena form that works apart from a bit where I have radio buttons for yes or no.

On the form I get the error:
Notice: Undefined variable... on line 134

for both the yes and no repsonses.

The rest fo teh code is fine and fthe form works except for this part.

Any help would be much appreicated

COde:

Code: Select all

<!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>Aliens Abducted Me - Report an Abduction</title>
  
<link rel="stylesheet" type="text/css" href="style.css" />

</head>

<body>
  
<h2>Aliens Abducted Me - Report an Abduction</h2>


<?php
  
require_once('connectvars3.php');

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

// Connect to the database
    
$dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name);

    

// Grab the report data from the POST
    
$first_name = mysqli_real_escape_string($dbc, trim($_POST['firstname']));
    
$last_name = mysqli_real_escape_string($dbc, trim($_POST['lastname']));
    
$email = mysqli_real_escape_string($dbc, trim($_POST['email']));
    
$when_it_happened = mysqli_real_escape_string($dbc, trim($_POST['whenithappened']));
    
$how_long = mysqli_real_escape_string($dbc, trim($_POST['howlong']));
    
$how_many = mysqli_real_escape_string($dbc, trim($_POST['howmany']));
    
$alien_description = mysqli_real_escape_string($dbc, trim($_POST['aliendescription']));
    
$what_they_did = mysqli_real_escape_string($dbc, trim($_POST['whattheydid']));
    
$fang_spotted = mysqli_real_escape_string($dbc, trim($_POST['fangspotted']));
    
$other = mysqli_real_escape_string($dbc, trim($_POST['other']));

    
if (!empty($first_name) && !empty($last_name) && !empty($when_it_happened) && !empty($how_long) && !empty($what_they_did)) {
      

// Write the data to the database
      
$query = "INSERT INTO aliens_abduction (first_name, last_name, email, when_it_happened, how_long, how_many, alien_description, what_they_did, 

fang_spotted, other) " .
        "VALUES ('$first_name', '$last_name', '$email', '$when_it_happened', '$how_long', '$how_many', '$alien_description', 

'$what_they_did', '$fang_spotted', '$other')";
      
mysqli_query($dbc, $query);

      

// Confirm success with the user
      echo '<p>Thanks for adding your abduction.</p>';
      
echo '<p><a href="index4.php"><< Back to the home page</a></p>';

      mysqli_close($dbc);
      
exit();
    
}
    else {
      
echo '<p class="error">Please enter your full name, date of abduction, how long you were abducted, and a brief description of the aliens.</p>';
    
}
  }

?>

  

<p>Share your story of alien abduction:</p>
  
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    
<label for="firstname">First name:</label>
    
<input type="text" id="firstname" name="firstname" value="
<?php 
if (!empty($first_name)) echo $first_name; 
?>" /> <br />
    
<label for="lastname">Last name:</label>
    
<input type="text" id="lastname" name="lastname" value="
<?php if (!empty($first_name)) echo $last_name; ?>" /><br />
    
<label for="email">What is your email address?</label>
    <input type="text" id="email" name="email" value="
<?php if (!empty($email)) echo $email; ?>" />
<br />
    
<label for="whenithappened">When did it happen?</label>
    <input type="text" id="whenithappened" name="whenithappened" value="
<?php if (!empty($when_it_happened)) echo $when_it_happened; else echo 'YYYY-MM-DD'; ?>" /><br />
    <label for="howlong">How long were you gone?

</label>
    <input type="text" id="howlong" name="howlong" value="

<?php if (!empty($how_long)) echo $how_long; ?>" /><br />
    <label for="howmany">How many did you see?</label>
    <input type="text" id="howmany" 

name="howmany" value="
<?php 
if (!empty($how_many)) echo $how_many; ?>" /><br />
    <label for="aliendescription">Describe them:</label>
    <input type="text" 

id="aliendescription" name="aliendescription" size="32" value="<?php if (!empty($alien_description)) echo $alien_description; ?>" /><br />
    
<label for="whattheydid">What did they do to you?</label>
    
<input type="text" id="whattheydid" name="whattheydid" size="32" value="<?php if (!empty($what_they_did))
echo $what_they_did; ?>" /><br />
   
<label for="fang_spotted">Have you seen my dog Fang?</label>
   
Yes <input id="fang_spotted" name="fang_spotted" type="radio" value="yes"
<?php echo ($fang_spotted == 'yes' ? 'checked="checked"' : ''); ?> 
    
No <input id="fang_spotted" name="fang_spotted" type="radio" value="no"  
<?php echo ($fang_spotted == 'no' ? 'checked="checked"' : ''); ?> /><br />
  <br />
    <label for="other">Anything else you want to add?</label>
    

<textarea id="other" name="other"><?php if (!empty($other)) echo $other; ?></textarea><br />
    <input type="submit" value="Report Abduction" 

name="submit" />
  
</form>

</body>

</html>



Re: Undefined variable

Posted: Thu Oct 07, 2010 1:57 pm
by Jonah Bron
It looks like this is line 134:

Code: Select all

id="aliendescription" name="aliendescription" size="32" value="<?php if (!empty($alien_description)) echo $alien_description; ?>" /><br />
Is that right?

$alien_description is defined at the beginning. The only issue I can think of is there's an error going on causing this variable to not be defined. Do you have error reporting on? If you don't, paste this at the beginning of your file:

Code: Select all

<?php
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
?>
Or, perhaps aliens are abducting that variable :D

Re: Undefined variable

Posted: Thu Oct 07, 2010 3:02 pm
by doug76
thnaks

the problem lies in the fandspotted. Sorry I should have specified the exact error message:
Undefined variable: fang_spotted ...in line 134

(the line speciifed is wrong I think)

I think there must be an error in the code below but cannot see one

<label for="fang_spotted">Have you seen my dog Fang?</label>

Yes <input id="fang_spotted" name="fang_spotted" type="radio" value="yes"
<?php echo ($fang_spotted == 'yes' ? 'checked="checked"' : ''); ?>

No <input id="fang_spotted" name="fang_spotted" type="radio" value="no"
<?php echo ($fang_spotted == 'no' ? 'checked="checked"' : ''); ?> /><br />
<br />

Re: Undefined variable

Posted: Thu Oct 07, 2010 3:15 pm
by Jonah Bron
Put this:

Code: Select all

<?php echo $fang_spotted; ?>
Into a random spot (after it's defined) in your file. If you get an error on that line, put it in a higher spot in the file. If you don't get an error (on that line), move it down. Keep doing this until you find out just where the error begins.