-3

when I visit mainpage.php without going to settings, my page gives me the undefined variable error for all the variables, but when I input something in the settings and submit, it doesnt show anymore. Anyways, here's the code so far.

Mainpage.php

<html>
<head><title></title></head>
<body>
<a href="logout.php">Logout</a>
<a href="settings.php">Settings</a>

</form>
</body>
</html>

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$bio = $_POST['bio'];
$hobbies = $_POST['hobbies'];
$past = $_POST['past'];
$work = $_POST['work'];
echo $bio;
echo $email;
echo $name;
echo $hobbies;
echo $past;
echo $work;
?>

settings.php

<html>
<head><title></title></head>
<body>
<a href="logout.php">Logout</a>
<a href="mainpage.php">Main Page</a>

<form action="mainpage.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
Bio: <input type="text" name="bio"><br>
Hobbies: <input type="text" name="hobbies"><br>
Past School: <input type="text" name="past"><br>
Work History: <input type="text" name="work"><br>
<input type="submit">
</form>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Choose Profile Picture:</label>
<input type="file" name="file" id="file"> 
<input type="submit" name="pic" value="Submit">
</form>

</body>
</html>

I can't seem to find out how to get rid of the undefined error. Someone suggested isset I believe.

user2896254
  • 39
  • 1
  • 3
  • 7

2 Answers2

1

your $_POST is empty

mainpage.php - the file upload and the input fields belonged to two different forms and there were two submit buttons. the information would never be sent at the same time. I combined the forms into one form with just one submit button.

<html>
<head><title></title></head>
<body>
<a href="logout.php">Logout</a>
<a href="mainpage.php">Main Page</a>

<form action="upload_file.php" method="post" enctype="multipart/form-data">
   Name: <input type="text" name="name"><br>
   E-mail: <input type="text" name="email"><br>
   Bio: <input type="text" name="bio"><br>
   Hobbies: <input type="text" name="hobbies"><br>
   Past School: <input type="text" name="past"><br>
   Work History: <input type="text" name="work"><br>
   <!--input type="submit"-->
<!--/form-->

<!--form action="upload_file.php" method="post" enctype="multipart/form-data"-->
   <label for="file">Choose Profile Picture:</label>
   <input type="file" name="file" id="file">   
   <br/>
   <input type="submit" name="pic" value="Submit"> 
</form>

</body>
</html> 

upload_file.php - the file info and the input info belong to two different arrays.

<?php  

   // the reason we should check for each $_POST variable and not just
   // if (!empty($_POST)) { ... }
   // is because you will still get errors if one of those keys do not
   // exist inside of $_POST... if you do $file = $_FILE['file'] there
   // will be an error.  however, $_POST is always going to exist, so 
   // we don't need to check it.  the keys may or may not exist, though

   $name    = isset($_POST['name'])    ? $_POST['name']  : '';
   $email   = isset($_POST['email'])   ? $_POST['email'] : '';
   $bio     = isset($_POST['bio'])     ? $_POST['bio']   : '';
   $hobbies = isset($_POST['hobbies']) ? $_POST['hobbies'] : '';
   $past    = isset($_POST['past'])    ? $_POST['past']  : '';
   $work    = isset($_POST['work'])    ? $_POST['work']  : ''; 

   echo "bio:  $bio<br/>";
   echo "email:  $email<br/>";
   echo "name:  $name<br/>";
   echo "hobbies:  $hobbies<br/>";
   echo "past:  $bio<br/>";
   echo "work:  $work<br/>";  

   print "<pre>";

   if (isset($_POST))
   {
      print "this is the \$_POST information:\n"; 
      print_r($_POST); 
   }
   else { print "no post information sent."; }

   print "\n\n";

   if (isset($_FILES))
   {
      print "this is the \$_FILE information:\n"; 
      print_r($_FILES);
   }
   else { print "no file information sent."; }

   print "</pre>"; 
?> 

So... just a disclaimer, beware of SQL injection when working with $_POST values.

output will look like this... (so you can see the structure of $_POST $_FILE)

bio: three
email: two
name: one
hobbies: four
past: three
work: 
this is the $_POST information:
Array
(
    [name] => one
    [email] => two
    [bio] => three
    [hobbies] => four
    [past] => 
    [work] => 
    [pic] => Submit
)


this is the $_FILE information:
Array
(
    [file] => Array
        (
            [name] => Capture.PNG
            [type] => image/png
            [tmp_name] => C:\wamp\tmp\php1D7E.tmp
            [error] => 0
            [size] => 12968
        )

)

(also, note, I keep updating for future users who come across this question)

gloomy.penguin
  • 5,833
  • 6
  • 33
  • 59
0

Use check for empty $_POST

if (!empty($_POST)){
    $name = $_POST['name'];
    $email = $_POST['email'];
    /* ... */
}
rNix
  • 2,457
  • 1
  • 22
  • 28
  • `isset($_POST['name']) ? $_POST['name'] : '';` - is worse, because you should get notice if expect some value, which wasn't passed from form (for example, typo in name attr). – rNix Nov 07 '13 at 06:38