Doug Posted October 11, 2010 Share Posted October 11, 2010 Hello, I think there may be a simple answer but I have been staring at this code for the last couple for hours and cannot see the error. I am going cross eyed! I have written a 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 responses. The rest of the code is fine and the functionality of uploading to the database works except for this part. Any help would be much appreciated Code: <!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>[u][/u] Quote Link to comment https://forums.phpfreaks.com/topic/215618-undefined-variable/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 11, 2010 Share Posted October 11, 2010 $_POST['fangspotted'] ^^^ That's not the name="...." you gave your form field. Quote Link to comment https://forums.phpfreaks.com/topic/215618-undefined-variable/#findComment-1121061 Share on other sites More sharing options...
Doug Posted October 11, 2010 Author Share Posted October 11, 2010 I think it is. I have tried both fangspotted and fang_spotted. There is no difference to the error message. Quote Link to comment https://forums.phpfreaks.com/topic/215618-undefined-variable/#findComment-1121063 Share on other sites More sharing options...
BlueSkyIS Posted October 11, 2010 Share Posted October 11, 2010 running your code, I get a different error: Notice: Undefined index: fangspotted in /Users/lesbrown/Sites/site1/headshopfinder.com/public_html/usort.php on line 56 PFMaBiSmAd is probably correct. Quote Link to comment https://forums.phpfreaks.com/topic/215618-undefined-variable/#findComment-1121071 Share on other sites More sharing options...
rwwd Posted October 11, 2010 Share Posted October 11, 2010 <textarea id="other" name="other"><?php if (!empty($other)){ echo $other; }?></textarea><br /> Need the braces there... There are a few other occurrences of that too. Rw Quote Link to comment https://forums.phpfreaks.com/topic/215618-undefined-variable/#findComment-1121075 Share on other sites More sharing options...
Doug Posted October 12, 2010 Author Share Posted October 12, 2010 thanks rwwd, the code completely works now. I just get the error message when I run the form code. Undefined variable: fang_spotted even though the code is working fine. Why would this be? Quote Link to comment https://forums.phpfreaks.com/topic/215618-undefined-variable/#findComment-1121546 Share on other sites More sharing options...
BlueSkyIS Posted October 12, 2010 Share Posted October 12, 2010 because you do something with $fang_spotted when it hasn't been defined. you're probably not getting an error, but a warning or notice. Quote Link to comment https://forums.phpfreaks.com/topic/215618-undefined-variable/#findComment-1121550 Share on other sites More sharing options...
Doug Posted October 13, 2010 Author Share Posted October 13, 2010 yes it is a notice: Notice: Undefined variable: fang_spotted Am I not defining $fang_spotted here: $fang_spotted = mysqli_real_escape_string($dbc, trim($_POST['fangspotted'])); Nothing is done to it before here. Quote Link to comment https://forums.phpfreaks.com/topic/215618-undefined-variable/#findComment-1121821 Share on other sites More sharing options...
premiso Posted October 13, 2010 Share Posted October 13, 2010 The reason is if the box is not checked, it does not get passed as a parameter to POST. $fang_spotted = !empty($_POST['fang_spotted'])?mysqli_real_escape_string($dbc, trim($_POST['fang_spotted'])):'no'; That should default the value to no if fang_spotted is not being passed to the POST data. This, may however be a flaw in your form code. As if one or the other should be checked, well that error should not occur. EDIT And indeed I did find the issue in your code: Yes <input id="fang_spotted" name="fang_spotted" type="radio" value="yes" <?php echo ($fang_spotted == 'yes' ? 'checked="checked"' : ''); ?> You never close that input statement. Change it to: Yes <input id="fang_spotted" name="fang_spotted" type="radio" value="yes" <?php echo ($fang_spotted == 'yes' ? 'checked="checked"' : ''); ?> /> And see if it works properly. Quote Link to comment https://forums.phpfreaks.com/topic/215618-undefined-variable/#findComment-1121833 Share on other sites More sharing options...
Doug Posted October 13, 2010 Author Share Posted October 13, 2010 sadly that had no effect. The notice remains Quote Link to comment https://forums.phpfreaks.com/topic/215618-undefined-variable/#findComment-1121845 Share on other sites More sharing options...
premiso Posted October 13, 2010 Share Posted October 13, 2010 This is what happens when you do not look at the line the error refers to. Indeed, you only set $fang_spotted when the form has been POST'ed and you try to use that variable no matter if the form was POST'ed or not, so the initial load will throw that error. To fix it: Yes <input id="fang_spotted" name="fang_spotted" type="radio" value="yes" <?php echo (!empty($fang_spotted) && $fang_spotted == 'yes') ? 'checked="checked"' : ''); ?> /> No <input id="fang_spotted" name="fang_spotted" type="radio" value="no" <?php echo (empty($fang_spotted) || $fang_spotted != 'yes') ? 'checked="checked"' : ''; ?> /><br /> That will test the variable to make sure that it is indeed populated and avoid errors for the initial page load. Quote Link to comment https://forums.phpfreaks.com/topic/215618-undefined-variable/#findComment-1121848 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.