Advertisement
SmaJli

VtoraLab_ISOK

Nov 10th, 2018
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.09 KB | None | 0 0
  1. FILE 1 : jquery_form.php
  2. START
  3. <?php
  4.  
  5. // If the form was submitted, scrub the input (server-side validation)
  6. // see below in the html for the client-side validation using jQuery
  7.  
  8. $name = '';
  9. $gender = '';
  10. $address = '';
  11. $email = '';
  12. $username = '';
  13. $password = '';
  14. $output = '';
  15.  
  16. if($_POST) {
  17.     // collect all input and trim to remove leading and trailing whitespaces
  18.     $name = trim($_POST['name']);
  19.     $gender = trim($_POST['gender']);
  20.     $address = trim($_POST['address']);
  21.     $email = trim($_POST['email']);
  22.     $username = trim($_POST['username']);
  23.     $password = trim($_POST['password']);
  24.  
  25.     $errors = array();
  26.  
  27.     // Validate the input
  28.     if (strlen($name) == 0)
  29.         array_push($errors, "Please enter your name");
  30.  
  31.     if (!(strcmp($gender, "Male") || strcmp($gender, "Female") || strcmp($gender, "Other")))
  32.         array_push($errors, "Please specify your gender");
  33.  
  34.     if (strlen($address) == 0)
  35.         array_push($errors, "Please specify your address");
  36.  
  37.     if (!filter_var($email, FILTER_VALIDATE_EMAIL))
  38.         array_push($errors, "Please specify a valid email address");
  39.  
  40.     if (strlen($username) == 0)
  41.         array_push($errors, "Please enter a valid username");
  42.  
  43.     if (strlen($password) < 5)
  44.         array_push($errors, "Please enter a password. Passwords must contain at least 5 characters.");
  45.  
  46.     // If no errors were found, proceed with storing the user input
  47.     if (count($errors) == 0) {
  48.         array_push($errors, "No errors were found. Thanks!");
  49.     }
  50.  
  51.     //Prepare errors for output
  52.     $output = '';
  53.     foreach($errors as $val) {
  54.         $output .= "<p class='output'>$val</p>";
  55.     }
  56.  
  57. }
  58.  
  59. ?>
  60.  
  61. <html>
  62. <head>
  63.     <!-- Define some CSS -->
  64.     <style type="text/css">
  65.         .label {width:100px;text-align:right;float:left;padding-right:10px;font-weight:bold;}
  66.         #register-form label.error, .output {color:#FB3A3A;font-weight:bold;}
  67.     </style>
  68.  
  69.     <!-- Load jQuery and the validate plugin -->
  70.     <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  71.     <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
  72.  
  73.     <!-- jQuery Form Validation code -->
  74.     <script>
  75.  
  76.         // When the browser is ready...
  77.         $(function() {
  78.  
  79.             // Setup form validation on the #register-form element
  80.             $("#register-form").validate({
  81.  
  82.                 // Specify the validation rules
  83.                 rules: {
  84.                     name: "required",
  85.                     gender: "required",
  86.                     address: "required",
  87.                     email: {
  88.                         required: true,
  89.                         email: true
  90.                     },
  91.                     username: "required",
  92.                     password: {
  93.                         required: true,
  94.                         minlength: 5
  95.                     }
  96.                 },
  97.  
  98.                 // Specify the validation error messages
  99.                 messages: {
  100.                     name: "Please enter your name",
  101.                     gender: "Please specify your gender",
  102.                     address: "Please enter your address",
  103.                     email: "Please enter a valid email address",
  104.                     username: "Please enter a valid username",
  105.                     password: {
  106.                         required: "Please provide a password",
  107.                         minlength: "Your password must be at least 5 characters long"
  108.                     }
  109.                 },
  110.  
  111.                 submitHandler: function(form) {
  112.                     form.submit();
  113.                 }
  114.             });
  115.  
  116.         });
  117.  
  118.     </script>
  119. </head>
  120. <body>
  121. <?php echo $output; ?>
  122. <!--  The form that will be parsed by jQuery before submit  -->
  123. <form action="" method="post" id="register-form" novalidate="novalidate">
  124.  
  125.     <div class="label">Name</div><input type="text" id="name" name="name" value="<?= $name; ?>" /><br />
  126.     <div class="label">Gender</div><select id="gender" name="gender" >
  127.     <option value="Female">Female</option>
  128.     <option value="Male">Male</option>
  129.     <option value="Other">Other</option>
  130.     </select><br />
  131.     <div class="label">Address</div><input type="text" id="address" name="address" value="<?= $address; ?>" /><br />
  132.     <div class="label">Email</div><input type="text" id="email" name="email" value="<?= $email; ?>" /><br />
  133.     <div class="label">Username</div><input type="text" id="username" name="username" value="<?= $username; ?>" /><br />
  134.     <div class="label">Password</div><input type="password" id="password" name="password" /><br />
  135.     <div style="margin-left:140px;"><input type="submit" name="submit" value="Submit" /></div>
  136.  
  137. </form>
  138.  
  139. </body>
  140. </html>
  141.  
  142. END
  143.  
  144. FILE 2: collect.php
  145. START
  146. <?php
  147. /**
  148.  * Created by PhpStorm.
  149.  * User: PC
  150.  * Date: 11/9/2018
  151.  * Time: 2:29 PM
  152.  */
  153. session_start(); //startuvanje na sesija
  154. if(isset($_GET['submit']) ){
  155.  
  156.  
  157.     if (isset($_GET['ime']) && !empty($_GET['ime']) &&
  158.         isset($_GET['prezime']) && !empty($_GET['prezime']) &&
  159.         isset($_GET['email']) && !empty($_GET['email']) &&
  160.         isset($_GET['pol'])) {
  161.  
  162.         $name = $_GET['ime'];
  163.         $surname = $_GET['prezime'];
  164.         $email = $_GET['email'];
  165.         $gender = $_GET['pol'];
  166.         if ($gender == 0){
  167.             $gender = "Zensko";
  168.         } else if ($gender == 1){
  169.             $gender = "Masko";
  170.         }
  171.  
  172.         echo " <p> УСПЕШНО ЛОГИРАЊЕ ! </p> </br>";
  173.         echo " <span>$name"." "."$surname</span> </br>";
  174.         echo "<span>$email</span> </br>";
  175.         echo "<span>$gender</span> </br>";
  176.  
  177.         if (isset($_GET['rememberme'])) {
  178.             setcookie('user', $name, time() + 7200)
  179.         }
  180.  
  181.         $sessionId = session_id();
  182.         setcookie('id', $sessionId, time() + 3600);
  183.         $_SESSION['id'] = $id;
  184.         $_SESSION['user'] = $name;
  185.         $_SESSION['LAST_ACTIVITY'] = time();
  186.     }
  187.  
  188.  
  189. }else{
  190.     echo "Please fill in the required fields. </br>";
  191.     echo "</br>";
  192.     echo "<a href='forma.php'>Back to form</a>";
  193. }
  194. $ime = $_SESSION['user'];
  195. if (isset($_SESSION['id']) && isset($_SESSION['LAST_ACTIVITY']) && isset($_COOKIE['id']) && (time() - $_SESSION['LAST_ACTIVITY'] < 3600)) {
  196.     echo "Hello " . $ime . " u are still logged in! </br> ";
  197. }
  198.  
  199.  
  200. END
  201.  
  202. FILE 3: form.php
  203. START
  204. <?php
  205. /**
  206.  * Created by PhpStorm.
  207.  * User: PC
  208.  * Date: 11/9/2018
  209.  * Time: 2:30 PM
  210.  */
  211.  
  212. if(!isset($_COOKIE['user'])) {
  213.  
  214.     echo "<h2> Hello" . $_COOKIE['user'] . "'! </h2>";
  215. } else {
  216. ?>
  217.  
  218. <html>
  219. <head>
  220.     <title>Vezba so formi</title>
  221. </head>
  222. <body>
  223. <form method="GET" action=”collect.php”>
  224.     <h3>Registracija:</h3>
  225.     Ime: <input type="text" name="ime" /> <br />
  226.     Prezime: <input type="text" name="prezime" /> <br />
  227.     Email: <input type="text" name="email" /> <br />
  228.     Masko: <input type="radio" name="pol" value="1" />
  229.     Zensko: <input type="radio" name="pol" value="0" /> <br />
  230.     Zapamti me: <input type="checkbox" name="rememberme" value="checked"/> <br/>
  231.     <input type="submit" name="submit" value="Vnesi" />
  232. </form>
  233. </body>
  234. </html>
  235. <?php
  236. }
  237.  
  238. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement