<!DOCTYPE html>
<html lang="hu">
<head>
 <title>Form</title>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <link rel="stylesheet" href="reg.css"/>
 <script type="text/javascript" src="reg.js"></script>
</head>
<body>

<h1>Let us have a party!</h1>

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

if(isset($_POST['submit'])){
 $showtheform=false;
 $name=$_POST['name'];
 $email=$_POST['email'];
 $age=$_POST['age'];
 $class=$_POST['class'];
 $gender=$_POST['gender'];
 $aboutMe=$_POST['aboutMe'];
 
 if(!empty($name) && !empty($email) && !empty($age) && $class!=0 && !empty($gender) && isset($_POST['drink']) && !empty($aboutMe) && !empty($_FILES['picture']['name'])){
 	
	foreach($_POST['drink'] as $drink){$drinks.=$drink.' ';} 	
 	
 	$pictureError=$_FILES['picture']['error'];
 	$pictureType=$_FILES['picture']['type'];
 	$pictureSize=$_FILES['picture']['size'];
 	
 	if($pictureError==0 && $pictureSize>0 && $pictureSize<=MAXFS && ($pictureType=='image/gif' || $pictureType=='image/jpeg' || $pictureType=='image/pjpeg' || $pictureType=='image/png')){                    
    $tempFolder=$_FILES['picture']['tmp_name'];
    $picture=time().$_FILES['picture']['name'];
    $target=PATH.$picture;
    
    if(move_uploaded_file($tempFolder, $target)){
     $dbc=mysqli_connect(HOST, USER, PASSWD, DB) or die('Error connection...');
     $query="INSERT INTO participants VALUES(0, '$name', '$email', '$age', '$class', '$gender', '$drinks', '$aboutMe', '$picture', NOW())";
     mysqli_query($dbc, $query) or die('Error...');	
     mysqli_close($dbc);
     echo '<h3>Succesful registration</h3>';
     echo '<h3><a href="'.$_SERVER['PHP_SELF'].'">Anyone else?&gt;&gt;<a></h3>';
    }else{echo '<h3>There sis no picture..</h3>'; $showtheform=true;}
 	
 	}else{echo '<h3>Error: gif, jpg, jpeg, png are acceptable! Maximum size: '.MAXFS.' bytes. It has to be greater then 0 bytes!</h3>'; $showtheform=true;}
 
 }else{echo '<h3>There are some empty fields...</h3>'; $showtheform=true;}
	
}else{$showtheform=true;}

if($showtheform){
?>
<form id="formId" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAXFS; ?>"/>
<fieldset>
 <p>Name: <input type="text" id="name" name="name" value="<?php if(!empty($name)){echo $name;} ?>"/></p>
 <p>Email: <input type="text" id="email" name="email" value="<?php if(!empty($email)){echo $email;} ?>"/></p> 
 <p>Age: <input type="text" id="age" name="age" value="<?php if(!empty($age)){echo $age;} ?>"/></p>
</fieldset>

<fieldset>
 <p>Class: 
  <select id="class" name="class">
   <option value="0">Select...</option>
   <option value="1">BSc: first</option>
   <option value="2">BSc: second</option>
   <option value="3">BSc: third</option>
   <option value="4">BSc: nth</option>
   <option value="5">BSc: infinity</option>
   <option value="6">Ok, I will be a baker....</option>
  </select>
 </p>
</fieldset>

<fieldset>
 <p>
  Gender: male<input type="radio" name="gender" value="male"/>	female<input type="radio" name="gender" value="female"/>	
  ufo<input type="radio" name="gender" value="ufo"/>	unknown<input type="radio" name="gender" value="unknown"/>
 </p>
</fieldset>

<fieldset>
 Drinks:<br/>
 <p>
  <img src="pictures/bier.jpg" class="drink"/><input type="checkbox" name="drink[]" id="drink1" value="bier"/>
  <img src="pictures/wine.jpg" class="drink"/><input type="checkbox" name="drink[]" id="drink2" value="wine"/>
 </p>
 <p>
  I do not have time! Give me a pangalactic gargleblaster! <input type="checkbox" name="drink[]" id="drink3" value="pangalactic_gargleblaster"/>
 </p>
</fieldset>

<fieldset>
 <label for="aboutMe">About me:</label><br/>
 <textarea name="aboutMe" wrap="hard" cols="40" rows="10" id="aboutMe" placeholder="Something about you..."><?php if(!empty($aboutMe)){echo $aboutMe;} ?></textarea>
</fieldset>

<fieldset>
  <p>Show yourself: <input type="file" name="picture"/></p>
</fieldset>

<fieldset id="last">
 <p id="buttons">
  <p><input type="reset" value="Reset"></p>
  <p><input type="submit" name="submit" value="I will be there" id="reg"></p>
 </p>
</fieldset>

<p><ul id="errorList"></ul></p>
</form>
<?php
}
?>

<h2><a href="main.html">&lt;&lt;Back to the main page</a></h2>

</body>
</html>