Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FILE 1 : jquery_form.php
- START
- <?php
- // If the form was submitted, scrub the input (server-side validation)
- // see below in the html for the client-side validation using jQuery
- $name = '';
- $gender = '';
- $address = '';
- $email = '';
- $username = '';
- $password = '';
- $output = '';
- if($_POST) {
- // collect all input and trim to remove leading and trailing whitespaces
- $name = trim($_POST['name']);
- $gender = trim($_POST['gender']);
- $address = trim($_POST['address']);
- $email = trim($_POST['email']);
- $username = trim($_POST['username']);
- $password = trim($_POST['password']);
- $errors = array();
- // Validate the input
- if (strlen($name) == 0)
- array_push($errors, "Please enter your name");
- if (!(strcmp($gender, "Male") || strcmp($gender, "Female") || strcmp($gender, "Other")))
- array_push($errors, "Please specify your gender");
- if (strlen($address) == 0)
- array_push($errors, "Please specify your address");
- if (!filter_var($email, FILTER_VALIDATE_EMAIL))
- array_push($errors, "Please specify a valid email address");
- if (strlen($username) == 0)
- array_push($errors, "Please enter a valid username");
- if (strlen($password) < 5)
- array_push($errors, "Please enter a password. Passwords must contain at least 5 characters.");
- // If no errors were found, proceed with storing the user input
- if (count($errors) == 0) {
- array_push($errors, "No errors were found. Thanks!");
- }
- //Prepare errors for output
- $output = '';
- foreach($errors as $val) {
- $output .= "<p class='output'>$val</p>";
- }
- }
- ?>
- <html>
- <head>
- <!-- Define some CSS -->
- <style type="text/css">
- .label {width:100px;text-align:right;float:left;padding-right:10px;font-weight:bold;}
- #register-form label.error, .output {color:#FB3A3A;font-weight:bold;}
- </style>
- <!-- Load jQuery and the validate plugin -->
- <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
- <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
- <!-- jQuery Form Validation code -->
- <script>
- // When the browser is ready...
- $(function() {
- // Setup form validation on the #register-form element
- $("#register-form").validate({
- // Specify the validation rules
- rules: {
- name: "required",
- gender: "required",
- address: "required",
- email: {
- required: true,
- email: true
- },
- username: "required",
- password: {
- required: true,
- minlength: 5
- }
- },
- // Specify the validation error messages
- messages: {
- name: "Please enter your name",
- gender: "Please specify your gender",
- address: "Please enter your address",
- email: "Please enter a valid email address",
- username: "Please enter a valid username",
- password: {
- required: "Please provide a password",
- minlength: "Your password must be at least 5 characters long"
- }
- },
- submitHandler: function(form) {
- form.submit();
- }
- });
- });
- </script>
- </head>
- <body>
- <?php echo $output; ?>
- <!-- The form that will be parsed by jQuery before submit -->
- <form action="" method="post" id="register-form" novalidate="novalidate">
- <div class="label">Name</div><input type="text" id="name" name="name" value="<?= $name; ?>" /><br />
- <div class="label">Gender</div><select id="gender" name="gender" >
- <option value="Female">Female</option>
- <option value="Male">Male</option>
- <option value="Other">Other</option>
- </select><br />
- <div class="label">Address</div><input type="text" id="address" name="address" value="<?= $address; ?>" /><br />
- <div class="label">Email</div><input type="text" id="email" name="email" value="<?= $email; ?>" /><br />
- <div class="label">Username</div><input type="text" id="username" name="username" value="<?= $username; ?>" /><br />
- <div class="label">Password</div><input type="password" id="password" name="password" /><br />
- <div style="margin-left:140px;"><input type="submit" name="submit" value="Submit" /></div>
- </form>
- </body>
- </html>
- END
- FILE 2: collect.php
- START
- <?php
- /**
- * Created by PhpStorm.
- * User: PC
- * Date: 11/9/2018
- * Time: 2:29 PM
- */
- session_start(); //startuvanje na sesija
- if(isset($_GET['submit']) ){
- if (isset($_GET['ime']) && !empty($_GET['ime']) &&
- isset($_GET['prezime']) && !empty($_GET['prezime']) &&
- isset($_GET['email']) && !empty($_GET['email']) &&
- isset($_GET['pol'])) {
- $name = $_GET['ime'];
- $surname = $_GET['prezime'];
- $email = $_GET['email'];
- $gender = $_GET['pol'];
- if ($gender == 0){
- $gender = "Zensko";
- } else if ($gender == 1){
- $gender = "Masko";
- }
- echo " <p> УСПЕШНО ЛОГИРАЊЕ ! </p> </br>";
- echo " <span>$name"." "."$surname</span> </br>";
- echo "<span>$email</span> </br>";
- echo "<span>$gender</span> </br>";
- if (isset($_GET['rememberme'])) {
- setcookie('user', $name, time() + 7200)
- }
- $sessionId = session_id();
- setcookie('id', $sessionId, time() + 3600);
- $_SESSION['id'] = $id;
- $_SESSION['user'] = $name;
- $_SESSION['LAST_ACTIVITY'] = time();
- }
- }else{
- echo "Please fill in the required fields. </br>";
- echo "</br>";
- echo "<a href='forma.php'>Back to form</a>";
- }
- $ime = $_SESSION['user'];
- if (isset($_SESSION['id']) && isset($_SESSION['LAST_ACTIVITY']) && isset($_COOKIE['id']) && (time() - $_SESSION['LAST_ACTIVITY'] < 3600)) {
- echo "Hello " . $ime . " u are still logged in! </br> ";
- }
- END
- FILE 3: form.php
- START
- <?php
- /**
- * Created by PhpStorm.
- * User: PC
- * Date: 11/9/2018
- * Time: 2:30 PM
- */
- if(!isset($_COOKIE['user'])) {
- echo "<h2> Hello" . $_COOKIE['user'] . "'! </h2>";
- } else {
- ?>
- <html>
- <head>
- <title>Vezba so formi</title>
- </head>
- <body>
- <form method="GET" action=”collect.php”>
- <h3>Registracija:</h3>
- Ime: <input type="text" name="ime" /> <br />
- Prezime: <input type="text" name="prezime" /> <br />
- Email: <input type="text" name="email" /> <br />
- Masko: <input type="radio" name="pol" value="1" />
- Zensko: <input type="radio" name="pol" value="0" /> <br />
- Zapamti me: <input type="checkbox" name="rememberme" value="checked"/> <br/>
- <input type="submit" name="submit" value="Vnesi" />
- </form>
- </body>
- </html>
- <?php
- }
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement