Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Form Validation
- <!DOCTYPE HTML>
- <html>
- <head>
- <style>
- .error {color: #FF0000;}
- </style>
- </head>
- <body>
- <?php
- // define variables and set to empty values
- $nameErr = $emailErr = $genderErr = $websiteErr = "";
- $name = $email = $gender = $comment = $website = "";
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- if (empty($_POST["name"])) {
- $nameErr = "Name is required";
- } else {
- $name = test_input($_POST["name"]);
- // check if name only contains letters and whitespace
- if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
- $nameErr = "Only letters and white space allowed";
- }
- }
- if (empty($_POST["email"])) {
- $emailErr = "Email is required";
- } else {
- $email = test_input($_POST["email"]);
- // check if e-mail address is well-formed
- if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $emailErr = "Invalid email format";
- }
- }
- if (empty($_POST["website"])) {
- $website = "";
- } else {
- $website = test_input($_POST["website"]);
- // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
- if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
- $websiteErr = "Invalid URL";
- }
- }
- if (empty($_POST["comment"])) {
- $comment = "";
- } else {
- $comment = test_input($_POST["comment"]);
- }
- if (empty($_POST["gender"])) {
- $genderErr = "Gender is required";
- } else {
- $gender = test_input($_POST["gender"]);
- }
- }
- function test_input($data) {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
- ?>
- <h2>PHP Form Validation Example</h2>
- <p><span class="error">* required field.</span></p>//Form Validation
- <!DOCTYPE HTML>
- <html>
- <head>
- <style>
- .error {color: #FF0000;}
- </style>
- </head>
- <body>
- <?php
- // define variables and set to empty values
- $nameErr = $emailErr = $genderErr = $websiteErr = "";
- $name = $email = $gender = $comment = $website = "";
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- if (empty($_POST["name"])) {
- $nameErr = "Name is required";
- } else {
- $name = test_input($_POST["name"]);
- // check if name only contains letters and whitespace
- if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
- $nameErr = "Only letters and white space allowed";
- }
- }
- if (empty($_POST["email"])) {
- $emailErr = "Email is required";
- } else {
- $email = test_input($_POST["email"]);
- // check if e-mail address is well-formed
- if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $emailErr = "Invalid email format";
- }
- }
- if (empty($_POST["website"])) {
- $website = "";
- } else {
- $website = test_input($_POST["website"]);
- // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
- if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
- $websiteErr = "Invalid URL";
- }
- }
- if (empty($_POST["comment"])) {
- $comment = "";
- } else {
- $comment = test_input($_POST["comment"]);
- }
- if (empty($_POST["gender"])) {
- $genderErr = "Gender is required";
- } else {
- $gender = test_input($_POST["gender"]);
- }
- }
- function test_input($data) {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
- ?>
- <h2>PHP Form Validation Example</h2>
- <p><span class="error">* required field.</span></p>
- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- Name: <input type="text" name="name" value="<?php echo $name;?>">
- <span class="error">* <?php echo $nameErr;?></span>
- <br><br>
- E-mail: <input type="text" name="email" value="<?php echo $email;?>">
- <span class="error">* <?php echo $emailErr;?></span>
- <br><br>
- Website: <input type="text" name="website" value="<?php echo $website;?>">
- <span class="error"><?php echo $websiteErr;?></span>
- <br><br>
- Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
- <br><br>
- Gender:
- <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
- <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
- <span class="error">* <?php echo $genderErr;?></span>
- <br><br>
- <input type="submit" name="submit" value="Submit">
- </form>
- <?php
- echo "<h2>Your Input:</h2>";
- echo $name;
- echo "<br>";
- echo $email;
- echo "<br>";
- echo $website;
- echo "<br>";
- echo $comment;
- echo "<br>";
- echo $gender;
- ?>
- </body>
- </html>
- ---------------------------------------------------------
- //FILES
- file_uploads = On
- HTML FILE
- <!DOCTYPE html>
- <html>
- <body>
- <form action="upload.php" method="post" enctype="multipart/form-data">
- Select image to upload:
- <input type="file" name="fileToUpload" id="fileToUpload">
- <input type="submit" value="Upload Image" name="submit">
- </form>
- </body>
- </html>
- COMPLETE PHP FILE
- <?php
- $target_dir = "uploads/";
- $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
- $uploadOk = 1;
- $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
- // Check if image file is a actual image or fake image
- if(isset($_POST["submit"])) {
- $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
- if($check !== false) {
- echo "File is an image - " . $check["mime"] . ".";
- $uploadOk = 1;
- } else {
- echo "File is not an image.";
- $uploadOk = 0;
- }
- }
- // Check if file already exists
- if (file_exists($target_file)) {
- echo "Sorry, file already exists.";
- $uploadOk = 0;
- }
- // Check file size
- if ($_FILES["fileToUpload"]["size"] > 500000) {
- echo "Sorry, your file is too large.";
- $uploadOk = 0;
- }
- // Allow certain file formats
- if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
- && $imageFileType != "gif" ) {
- echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
- $uploadOk = 0;
- }
- // Check if $uploadOk is set to 0 by an error
- if ($uploadOk == 0) {
- echo "Sorry, your file was not uploaded.";
- // if everything is ok, try to upload file
- } else {
- if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
- echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
- } else {
- echo "Sorry, there was an error uploading your file.";
- }
- }
- ?>
- --------------------------------------------------
- SQL operations
- open connection
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- // Create connection
- $conn = new mysqli($servername, $username, $password);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- echo "Connected successfully";
- ?>
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- // Create connection
- $conn = new mysqli($servername, $username, $password);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- Create database
- $sql = "CREATE DATABASE myDB";
- if ($conn->query($sql) === TRUE) {
- echo "Database created successfully";
- } else {
- echo "Error creating database: " . $conn->error;
- }
- $conn->close();
- ?>
- CREATE TABLE
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $dbname = "myDB";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- //sql to create table
- $sql = "CREATE TABLE MyGuests (
- id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
- firstname VARCHAR(30) NOT NULL,
- lastname VARCHAR(30) NOT NULL,
- email VARCHAR(50),
- reg_date TIMESTAMP
- )";
- if ($conn->query($sql) === TRUE) {
- echo "Table MyGuests created successfully";
- } else {
- echo "Error creating table: " . $conn->error;
- }
- $conn->close();
- ?>
- INSERT DATA
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $dbname = "myDB";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $sql = "INSERT INTO MyGuests (firstname, lastname, email)
- VALUES ('John', 'Doe', 'john@example.com')";
- if ($conn->query($sql) === TRUE) {
- echo "New record created successfully";
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- $conn->close();
- ?>
- INSERT MULTIPLE
- $sql = "INSERT INTO MyGuests (firstname, lastname, email)
- VALUES ('John', 'Doe', 'john@example.com');";
- $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
- VALUES ('Mary', 'Moe', 'mary@example.com');";
- $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
- VALUES ('Julie', 'Dooley', 'julie@example.com')";
- LAST ID:
- $conn->insert_id;
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $dbname = "myDB";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- // prepare and bind
- $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
- $stmt->bind_param("sss", $firstname, $lastname, $email);
- // set parameters and execute
- $firstname = "John";
- $lastname = "Doe";
- $email = "john@example.com";
- $stmt->execute();
- $firstname = "Mary";
- $lastname = "Moe";
- $email = "mary@example.com";
- $stmt->execute();
- $firstname = "Julie";
- $lastname = "Dooley";
- $email = "julie@example.com";
- $stmt->execute();
- echo "New records created successfully";
- $stmt->close();
- $conn->close();
- ?>
- //Form Validation
- <!DOCTYPE HTML>
- <html>
- <head>
- <style>
- .error {color: #FF0000;}
- </style>
- </head>
- <body>
- <?php
- // define variables and set to empty values
- $nameErr = $emailErr = $genderErr = $websiteErr = "";
- $name = $email = $gender = $comment = $website = "";
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- if (empty($_POST["name"])) {
- $nameErr = "Name is required";
- } else {
- $name = test_input($_POST["name"]);
- // check if name only contains letters and whitespace
- if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
- $nameErr = "Only letters and white space allowed";
- }
- }
- if (empty($_POST["email"])) {
- $emailErr = "Email is required";
- } else {
- $email = test_input($_POST["email"]);
- // check if e-mail address is well-formed
- if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $emailErr = "Invalid email format";
- }
- }
- if (empty($_POST["website"])) {
- $website = "";
- } else {
- $website = test_input($_POST["website"]);
- // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
- if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
- $websiteErr = "Invalid URL";
- }
- }
- if (empty($_POST["comment"])) {
- $comment = "";
- } else {
- $comment = test_input($_POST["comment"]);
- }
- if (empty($_POST["gender"])) {
- $genderErr = "Gender is required";
- } else {
- $gender = test_input($_POST["gender"]);
- }
- }
- function test_input($data) {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
- ?>
- <h2>PHP Form Validation Example</h2>
- <p><span class="error">* required field.</span></p>
- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- Name: <input type="text" name="name" value="<?php echo $name;?>">
- <span class="error">* <?php echo $nameErr;?></span>
- <br><br>
- E-mail: <input type="text" name="email" value="<?php echo $email;?>">
- <span class="error">* <?php echo $emailErr;?></span>
- <br><br>
- Website: <input type="text" name="website" value="<?php echo $website;?>">
- <span class="error"><?php echo $websiteErr;?></span>
- <br><br>
- Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
- <br><br>
- Gender:
- <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
- <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
- <span class="error">* <?php echo $genderErr;?></span>
- <br><br>
- <input type="submit" name="submit" value="Submit">
- </form>
- <?php
- echo "<h2>Your Input:</h2>";
- echo $name;
- echo "<br>";
- echo $email;
- echo "<br>";
- echo $website;
- echo "<br>";
- echo $comment;
- echo "<br>";
- echo $gender;
- ?>
- </body>
- </html>
- ---------------------------------------------------------
- //FILES
- file_uploads = On
- HTML FILE
- <!DOCTYPE html>
- <html>
- <body>
- <form action="upload.php" method="post" enctype="multipart/form-data">
- Select image to upload:
- <input type="file" name="fileToUpload" id="fileToUpload">
- <input type="submit" value="Upload Image" name="submit">
- </form>
- </body>
- </html>
- COMPLETE PHP FILE
- <?php
- $target_dir = "uploads/";
- $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
- $uploadOk = 1;
- $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
- // Check if image file is a actual image or fake image
- if(isset($_POST["submit"])) {
- $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
- if($check !== false) {
- echo "File is an image - " . $check["mime"] . ".";
- $uploadOk = 1;
- } else {
- echo "File is not an image.";
- $uploadOk = 0;
- }
- }
- // Check if file already exists
- if (file_exists($target_file)) {
- echo "Sorry, file already exists.";
- $uploadOk = 0;
- }
- // Check file size
- if ($_FILES["fileToUpload"]["size"] > 500000) {
- echo "Sorry, your file is too large.";
- $uploadOk = 0;
- }
- // Allow certain file formats
- if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
- && $imageFileType != "gif" ) {
- echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
- $uploadOk = 0;
- }
- // Check if $uploadOk is set to 0 by an error
- if ($uploadOk == 0) {
- echo "Sorry, your file was not uploaded.";
- // if everything is ok, try to upload file
- } else {
- if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
- echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
- } else {
- echo "Sorry, there was an error uploading your file.";
- }
- }
- ?>
- --------------------------------------------------
- SQL operations
- open connection
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- // Create connection
- $conn = new mysqli($servername, $username, $password);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- echo "Connected successfully";
- ?>
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- // Create connection
- $conn = new mysqli($servername, $username, $password);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- Create database
- $sql = "CREATE DATABASE myDB";
- if ($conn->query($sql) === TRUE) {
- echo "Database created successfully";
- } else {
- echo "Error creating database: " . $conn->error;
- }
- $conn->close();
- ?>
- CREATE TABLE
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $dbname = "myDB";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- //sql to create table
- $sql = "CREATE TABLE MyGuests (
- id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
- firstname VARCHAR(30) NOT NULL,
- lastname VARCHAR(30) NOT NULL,
- email VARCHAR(50),
- reg_date TIMESTAMP
- )";
- if ($conn->query($sql) === TRUE) {
- echo "Table MyGuests created successfully";
- } else {
- echo "Error creating table: " . $conn->error;
- }
- $conn->close();
- ?>
- INSERT DATA
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $dbname = "myDB";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $sql = "INSERT INTO MyGuests (firstname, lastname, email)
- VALUES ('John', 'Doe', 'john@example.com')";
- if ($conn->query($sql) === TRUE) {
- echo "New record created successfully";
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- $conn->close();
- ?>
- INSERT MULTIPLE
- $sql = "INSERT INTO MyGuests (firstname, lastname, email)
- VALUES ('John', 'Doe', 'john@example.com');";
- $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
- VALUES ('Mary', 'Moe', 'mary@example.com');";
- $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
- VALUES ('Julie', 'Dooley', 'julie@example.com')";
- LAST ID:
- $conn->insert_id;
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $dbname = "myDB";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- // prepare and bind
- $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
- $stmt->bind_param("sss", $firstname, $lastname, $email);
- // set parameters and execute
- $firstname = "John";
- $lastname = "Doe";
- $email = "john@example.com";
- $stmt->execute();
- $firstname = "Mary";
- $lastname = "Moe";
- $email = "mary@example.com";
- $stmt->execute();
- $firstname = "Julie";
- $lastname = "Dooley";
- $email = "julie@example.com";
- $stmt->execute();
- echo "New records created successfully";
- $stmt->close();
- $conn->close();
- ?>
- SELECT DATA
- $sql = "SELECT id, firstname, lastname FROM MyGuests";
- DELETE DATA
- $sql = "DELETE FROM MyGuests WHERE id=3";
- UPDATE DATA
- $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
- LIMIT DATA
- $sql = "SELECT * FROM Orders LIMIT 15, 10";
- SELECT DATA
- $sql = "SELECT id, firstname, lastname FROM MyGuests";
- DELETE DATA
- $sql = "DELETE FROM MyGuests WHERE id=3";
- UPDATE DATA
- $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
- LIMIT DATA
- $sql = "SELECT * FROM Orders LIMIT 15, 10";
- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- Name: <input type="text" name="name" value="<?php echo $name;?>">
- <span class="error">* <?php echo $nameErr;?></span>
- <br><br>
- E-mail: <input type="text" name="email" value="<?php echo $email;?>">
- <span class="error">* <?php echo $emailErr;?></span>
- <br><br>
- Website: <input type="text" name="website" value="<?php echo $website;?>">
- <span class="error"><?php echo $websiteErr;?></span>
- <br><br>
- Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
- <br><br>
- Gender:
- <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
- <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
- <span class="error">* <?php echo $genderErr;?></span>
- <br><br>
- <input type="submit" name="submit" value="Submit">
- </form>
- <?php
- echo "<h2>Your Input:</h2>";
- echo $name;
- echo "<br>";
- echo $email;
- echo "<br>";
- echo $website;
- echo "<br>";
- echo $comment;
- echo "<br>";
- echo $gender;
- ?>
- </body>
- </html>
- ---------------------------------------------------------
- //FILES
- file_uploads = On
- HTML FILE
- <!DOCTYPE html>
- <html>
- <body>
- <form action="upload.php" method="post" enctype="multipart/form-data">
- Select image to upload:
- <input type="file" name="fileToUpload" id="fileToUpload">
- <input type="submit" value="Upload Image" name="submit">
- </form>
- </body>
- </html>
- COMPLETE PHP FILE
- <?php
- $target_dir = "uploads/";
- $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
- $uploadOk = 1;
- $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
- // Check if image file is a actual image or fake image
- if(isset($_POST["submit"])) {
- $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
- if($check !== false) {
- echo "File is an image - " . $check["mime"] . ".";
- $uploadOk = 1;
- } else {
- echo "File is not an image.";
- $uploadOk = 0;
- }
- }
- // Check if file already exists
- if (file_exists($target_file)) {
- echo "Sorry, file already exists.";
- $uploadOk = 0;
- }
- // Check file size
- if ($_FILES["fileToUpload"]["size"] > 500000) {
- echo "Sorry, your file is too large.";
- $uploadOk = 0;
- }
- // Allow certain file formats
- if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
- && $imageFileType != "gif" ) {
- echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
- $uploadOk = 0;
- }
- // Check if $uploadOk is set to 0 by an error
- if ($uploadOk == 0) {
- echo "Sorry, your file was not uploaded.";
- // if everything is ok, try to upload file
- } else {
- if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
- echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
- } else {
- echo "Sorry, there was an error uploading your file.";
- }
- }
- ?>
- --------------------------------------------------
- SQL operations
- open connection
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- // Create connection
- $conn = new mysqli($servername, $username, $password);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- echo "Connected successfully";
- ?>
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- // Create connection
- $conn = new mysqli($servername, $username, $password);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- Create database
- $sql = "CREATE DATABASE myDB";
- if ($conn->query($sql) === TRUE) {
- echo "Database created successfully";
- } else {
- echo "Error creating database: " . $conn->error;
- }
- $conn->close();
- ?>
- CREATE TABLE
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $dbname = "myDB";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- //sql to create table
- $sql = "CREATE TABLE MyGuests (
- id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
- firstname VARCHAR(30) NOT NULL,
- lastname VARCHAR(30) NOT NULL,
- email VARCHAR(50),
- reg_date TIMESTAMP
- )";
- if ($conn->query($sql) === TRUE) {
- echo "Table MyGuests created successfully";
- } else {
- echo "Error creating table: " . $conn->error;
- }
- $conn->close();
- ?>
- INSERT DATA
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $dbname = "myDB";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $sql = "INSERT INTO MyGuests (firstname, lastname, email)
- VALUES ('John', 'Doe', 'john@example.com')";
- if ($conn->query($sql) === TRUE) {
- echo "New record created successfully";
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- $conn->close();
- ?>
- INSERT MULTIPLE
- $sql = "INSERT INTO MyGuests (firstname, lastname, email)
- VALUES ('John', 'Doe', 'john@example.com');";
- $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
- VALUES ('Mary', 'Moe', 'mary@example.com');";
- $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
- VALUES ('Julie', 'Dooley', 'julie@example.com')";
- LAST ID:
- $conn->insert_id;
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $dbname = "myDB";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- // prepare and bind
- $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
- $stmt->bind_param("sss", $firstname, $lastname, $email);
- // set parameters and execute
- $firstname = "John";
- $lastname = "Doe";
- $email = "john@example.com";
- $stmt->execute();
- $firstname = "Mary";
- $lastname = "Moe";
- $email = "mary@example.com";
- $stmt->execute();
- $firstname = "Julie";
- $lastname = "Dooley";
- $email = "julie@example.com";
- $stmt->execute();
- echo "New records created successfully";
- $stmt->close();
- $conn->close();
- ?>
- SELECT DATA
- $sql = "SELECT id, firstname, lastname FROM MyGuests";
- DELETE DATA
- $sql = "DELETE FROM MyGuests WHERE id=3";
- UPDATE DATA
- $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
- LIMIT DATA
- $sql = "SELECT * FROM Orders LIMIT 15, 10";
- ---------------------------------------------
- Validation of number
- <!DOCTYPE html>
- <html>
- <body>
- <p>Enter a number and click OK:</p>
- <input id="id1" type="number" min="100" max="300" required>
- <button onclick="myFunction()">OK</button>
- <p>If the number is less than 100 or greater than 300, an error message will be displayed.</p>
- <p id="demo"></p>
- <script>
- function myFunction() {
- var inpObj = document.getElementById("id1");
- if (inpObj.checkValidity() == false) {
- document.getElementById("demo").innerHTML = inpObj.validationMessage;
- } else {
- document.getElementById("demo").innerHTML = "Input OK";
- }
- }
- </script>
- </body>
- </html>
- <!DOCTYPE html>
- <html>
- <body>
- <p>Enter a number and click OK:</p>
- <input id="id1" type="number" max="100">
- <button onclick="myFunction()">OK</button>
- <p>If the number is greater than 100 (the input's max attribute), an error message will be displayed.</p>
- <p id="demo"></p>
- <script>
- function myFunction() {
- var txt = "";
- if (document.getElementById("id1").validity.rangeOverflow) {
- txt = "Value too large";
- } else {
- txt = "Input OK";
- }
- document.getElementById("demo").innerHTML = txt;
- }
- </script>
- </body>
- </html>
- <input id="id1" type="number" min="100">
- <button onclick="myFunction()">OK</button>
- <p id="demo"></p>
- <script>
- function myFunction() {
- var txt = "";
- if (document.getElementById("id1").validity.rangeUnderflow) {
- txt = "Value too small";
- }
- document.getElementById("demo").innerHTML = txt;
- }
- </script>
- javascript events
- ONBLUR EVENT
- <!DOCTYPE html>
- <html>
- <head>
- <script>
- function myFunction() {
- var x = document.getElementById("fname");
- x.value = x.value.toUpperCase();
- }
- </script>
- </head>
- <body>
- Enter your name: <input type="text" id="fname" onblur="myFunction()">
- <p>When you leave the input field, a function is triggered which transforms the input text to upper case.</p>
- </body>
- </html>
- ON MOUSE OVER
- <!DOCTYPE html>
- <html>
- <body>
- <h1 onmouseover="style.color='red'" onmouseout="style.color='black'">Mouse over this text</h1>
- </body>
- </html>
- ONMOUSEUP
- <!DOCTYPE html>
- <html>
- <head>
- <script>
- function myFunction(elmnt, clr) {
- elmnt.style.color = clr;
- }
- </script>
- </head>
- <body>
- javascript events
- ONBLUR EVENT
- <!DOCTYPE html>
- <html>
- <head>
- <script>
- function myFunction() {
- var x = document.getElementById("fname");
- x.value = x.value.toUpperCase();
- }
- </script>
- </head>
- <body>
- Enter your name: <input type="text" id="fname" onblur="myFunction()">
- <p>When you leave the input field, a function is triggered which transforms the input text to upper case.</p>
- </body>
- </html>
- ON MOUSE OVER
- <!DOCTYPE html>
- <html>
- <body>
- <h1 onmouseover="style.color='red'" onmouseout="style.color='black'">Mouse over this text</h1>
- </body>
- </html>
- ONMOUSEUP
- <!DOCTYPE html>
- <html>
- <head>
- <script>
- function myFunction(elmnt, clr) {
- elmnt.style.color = clr;
- }
- </script>
- </head>
- <body>
- <p onmousedown="myFunction(this,'red')" onmouseup="myFunction(this,'green')">
- Click the text to change the color. A function, with parameters, is triggered when the mouse button is pressed down, and again, with other parameters, when the mouse button is released.
- </p>
- </body>
- </html>
- ONMOUSEDOWN
- <!DOCTYPE html>
- <html>
- <head>
- <script>
- function whichElement(e) {
- var targ;
- if (!e) {
- var e = window.event;
- }
- if (e.target) {
- targ=e.target;
- } else if (e.srcElement) {
- targ=e.srcElement;
- }
- var tname;
- tname = targ.tagName;
- alert("You clicked on a " + tname + " element.");
- }
- </script>
- </head>
- <body onmousedown="whichElement(event)">
- <p>Click somewhere in the document. An alert box will alert the name of the element you clicked on.</p>
- <h3>This is a heading</h3>
- <img border="0" src="smiley.gif" alt="Smiley" width="32" height="32">
- <p>This is a paragraph.</p>
- </body>
- </html>
- ONLOAD
- <!DOCTYPE html>
- <html>
- <head>
- <script>
- function myFunction() {
- alert("Page is loaded");
- }
- </script>
- </head>
- <body onload="myFunction()">
- <h2>Hello World!</h2>
- </body>
- </html>
- ---------------------------------------------------------------------------------
- CLOCK
- <!DOCTYPE html>
- <html>
- <head>
- <script>
- function startTime() {
- var today = new Date();
- var h = today.getHours();
- var m = today.getMinutes();
- var s = today.getSeconds();
- m = checkTime(m);
- s = checkTime(s);
- document.getElementById('txt').innerHTML =
- h + ":" + m + ":" + s;
- var t = setTimeout(startTime, 500);
- }
- function checkTime(i) {
- if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
- return i;
- }
- </script>
- </head>
- <body onload="startTime()">
- <div id="txt"></div>
- </body>
- </html>
- ------------------------------------------
- CALCULATOR
- <html>
- <head></head>
- <body>
- <h3>Simple Calculator</h3>
- <br/>
- <style>
- #calc{width:300px;height:250px;}
- #btn{width:100%;height:40px;font-size:20px;}
- </style>
- <form Name="calc">
- <table id="calc" border=2>
- <tr>
- <td colspan=5><input id="btn" name="display" onkeypress="return event.charCode >= 48 && event.charCode <= 57" type="text"></td>
- <td style="display:none"><input name="M" type="number"></td>
- </tr>
- <tr>
- <td><input id="btn" type=button value="MC" OnClick="calc.M.value=''"></td>
- <td><input id="btn" type=button value="0" OnClick="calc.display.value+='0'"></td>
- <td><input id="btn" type=button value="1" OnClick="calc.display.value+='1'"></td>
- <td><input id="btn" type=button value="2" OnClick="calc.display.value+='2'"></td>
- <td><input id="btn" type=button value="+" OnClick="calc.display.value+='+'"></td>
- </tr>
- <tr>
- <td><input id="btn" type=button value="MS" OnClick="calc.M.value=calc.display.value"></td>
- <td><input id="btn" type=button value="3" OnClick="calc.display.value+='3'"></td>
- <td><input id="btn" type=button value="4" OnClick="calc.display.value+='4'"></td>
- <td><input id="btn" type=button value="5" OnClick="calc.display.value+='5'"></td>
- <td><input id="btn" type=button value="-" OnClick="calc.display.value+='-'"></td>
- </tr>
- <tr>
- <td><input id="btn" type=button value="MR" OnClick="calc.display.value=calc.M.value"></td>
- <td><input id="btn" type=button value="6" OnClick="calc.display.value+='6'"></td>
- <td><input id="btn" type=button value="7" OnClick="calc.display.value+='7'"></td>
- <td><input id="btn" type=button value="8" OnClick="calc.display.value+='8'"></td>
- <td><input id="btn" type=button value="x" OnClick="calc.display.value+='*'"></td>
- </tr>
- <tr>
- <td><input id="btn" type=button value="M+" OnClick="calc.M.value=(Number(calc.M.value))+(Number(calc.display.value))"></td>
- <td><input id="btn" type=button value="9" OnClick="calc.display.value+='9'"></td>
- <td><input id="btn" type=button value="±"
- OnClick="calc.display.value=(calc.display.value==Math.abs(calc.display.value)?-(calc.display.value):Math.abs(calc.display.value))">
- </td>
- <td><input id="btn" type=button value="=" OnClick="calc.display.value=eval(calc.display.value)"></td>
- <td><input id="btn" type=button value="/" OnClick="calc.display.value+='/'"></td>
- </tr>
- <tr>
- <td><input id="btn" type=button value="1/x" OnClick="calc.display.value=1/calc.display.value"></td>
- <td><input id="btn" type=button value="." OnClick="calc.display.value+='.'"></td>
- <td><input id="btn" type=button value="x2" OnClick="calc.display.value=Math.pow(calc.display.value,2)"></td>
- <td><input id="btn" type=button value="√" OnClick="calc.display.value=Math.sqrt(calc.display.value)"></td>
- <td><input id="btn" type=button value="C" OnClick="calc.display.value=''"></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
- PHP BY NISHANT
- <!DOCTYPE HTML>
- <html>
- <head>
- <style>
- .error {color: #FF0000;}
- </style>
- </head>
- <body>
- <?php
- session_start();
- $servername = "localhost";
- $username = "root";
- $password = "";
- $db="Login details";
- // Create connection
- $conn = new mysqli($servername, $username, $password,$db);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $sql1 = "Select 1 from registration";
- $res = $conn->query($sql1);
- if($res == false){
- echo "Connected successfully";
- $sql="create table registration (Slno int(3) AUTO_INCREMENT PRIMARY KEY, name varchar(30),
- email varchar(30), website varchar(30));";
- if($conn->query($sql)==TRUE)
- {
- echo "table Myguest created";
- }
- else
- echo"Error creating table:".$conn->error;
- }
- else {
- echo "The table is already created";
- }
- // define variables and set to empty values
- $test1 = 1;
- $nameErr = $emailErr = $genderErr = $websiteErr = "";
- $name = $email = $gender = $comment = $website = "";
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- if (empty($_POST["name"])) {
- $nameErr = "Name is required";
- $test1 =0;
- } else {
- $name = test_input($_POST["name"]);
- // check if name only contains letters and whitespace
- if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
- $nameErr = "Only letters and white space allowed";
- $test1 =0;
- }
- }
- if (empty($_POST["email"])) {
- $emailErr = "Email is required";
- $test1 =0;
- } else {
- $email = test_input($_POST["email"]);
- // check if e-mail address is well-formed
- if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $emailErr = "Invalid email format";
- $test1 =0;
- }
- }
- if (empty($_POST["website"])) {
- $website = "";
- } else {
- $website = test_input($_POST["website"]);
- // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
- if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
- $websiteErr = "Invalid URL";
- $test1 =0;
- }
- }
- if (empty($_POST["comment"])) {
- $comment = "";
- } else {
- $comment = test_input($_POST["comment"]);
- }
- if (empty($_POST["gender"])) {
- $genderErr = "Gender is required";
- $test1 =0;
- } else {
- $gender = test_input($_POST["gender"]);
- }
- }
- if(isset($_POST["submit"]) && $test1 == 1){
- $sql2 = "insert into registration(`name`,`email`,`website`) values('$name','$email','$website')";
- $go = 0;
- if($conn->query($sql2)){
- $go=1;
- }
- else {
- echo "Some fucking error occured! ".$conn->error;
- }
- if($go == 1){
- setcookie("Name",$name,time()+60*5);
- $_SESSION["name"] = $name;
- header("Location: newPage.php");
- }
- }
- function test_input($data) {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
- ?>
- <h2>PHP Form Validation Example</h2>
- <p><span class="error">* required field.</span></p>
- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- Name: <input type="text" name="name" value="<?php echo $name;?>">
- <span class="error">* <?php echo $nameErr;?></span>
- <br><br>
- E-mail: <input type="text" name="email" value="<?php echo $email;?>">
- <span class="error">* <?php echo $emailErr;?></span>
- <br><br>
- Website: <input type="text" name="website" value="<?php echo $website;?>">
- <span class="error"><?php echo $websiteErr;?></span>
- <br><br>
- Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
- <br><br>
- <input type="text" id="check" value = "hey" placeholder="Enter your age">
- <p id="result">Hello</p>
- <br><input type="button" value= "Verify" onclick="checker()">
- <br><br>
- Gender:
- <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
- <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
- <span class="error">* <?php echo $genderErr;?></span>
- <br><br>
- <input type="submit" name="submit" value="Submit">
- </form>
- <?php
- echo "<h2>Your Input:</h2>";
- echo $name;
- echo "<br>";
- echo $email;
- echo "<br>";
- echo $website;
- echo "<br>";
- echo $comment;
- echo "<br>";
- echo $gender;
- ?>
- </body>
- <script>
- function checker(){
- var reg = "^[a-z]{3,}$";
- document.getElementById("result").innerHTML= "Not Matched";
- var str = document.getElementById("check").value;
- if(str.match(reg)){
- document.getElementById("result").innerHTML= "matched";
- }
- /*
- if(data.match(reg)){
- res.innerHTML = "Matched";
- }
- else {
- res.innerHTML = "Not matched";
- }
- */
- }
- </script>
- </html>
- <p onmousedown="myFunction(this,'red')" onmouseup="myFunction(this,'green')">
- Click the text to change the color. A function, with parameters, is triggered when the mouse button is pressed down, and again, with other parameters, when the mouse button is released.
- </p>
- </body>
- </html>
- ONMOUSEDOWN
- <!DOCTYPE html>
- <html>
- <head>
- <script>
- function whichElement(e) {
- var targ;
- if (!e) {
- var e = window.event;
- }
- if (e.target) {
- targ=e.target;
- } else if (e.srcElement) {
- targ=e.srcElement;
- }
- var tname;
- tname = targ.tagName;
- alert("You clicked on a " + tname + " element.");
- }
- </script>
- </head>
- <body onmousedown="whichElement(event)">
- <p>Click somewhere in the document. An alert box will alert the name of the element you clicked on.</p>
- <h3>This is a heading</h3>
- <img border="0" src="smiley.gif" alt="Smiley" width="32" height="32">
- <p>This is a paragraph.</p>
- </body>
- </html>
- ONLOAD
- <!DOCTYPE html>
- <html>
- <head>
- <script>
- function myFunction() {
- alert("Page is loaded");
- }
- </script>
- </head>
- <body onload="myFunction()">
- <h2>Hello World!</h2>
- </body>
- </html>
- ---------------------------------------------------------------------------------
- CLOCK
- <!DOCTYPE html>
- <html>
- <head>
- <script>
- function startTime() {
- var today = new Date();
- var h = today.getHours();
- var m = today.getMinutes();
- var s = today.getSeconds();
- m = checkTime(m);
- s = checkTime(s);
- document.getElementById('txt').innerHTML =
- h + ":" + m + ":" + s;
- var t = setTimeout(startTime, 500);
- }
- function checkTime(i) {
- if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
- return i;
- }
- </script>
- </head>
- <body onload="startTime()">
- <div id="txt"></div>
- </body>
- </html>
- ------------------------------------------
- CALCULATOR
- <html>
- <head></head>
- <body>
- <h3>Simple Calculator</h3>
- <br/>
- <style>
- #calc{width:300px;height:250px;}
- #btn{width:100%;height:40px;font-size:20px;}
- </style>
- <form Name="calc">
- <table id="calc" border=2>
- <tr>
- <td colspan=5><input id="btn" name="display" onkeypress="return event.charCode >= 48 && event.charCode <= 57" type="text"></td>
- <td style="display:none"><input name="M" type="number"></td>
- </tr>
- <tr>
- <td><input id="btn" type=button value="MC" OnClick="calc.M.value=''"></td>
- <td><input id="btn" type=button value="0" OnClick="calc.display.value+='0'"></td>
- <td><input id="btn" type=button value="1" OnClick="calc.display.value+='1'"></td>
- <td><input id="btn" type=button value="2" OnClick="calc.display.value+='2'"></td>
- <td><input id="btn" type=button value="+" OnClick="calc.display.value+='+'"></td>
- </tr>
- <tr>
- <td><input id="btn" type=button value="MS" OnClick="calc.M.value=calc.display.value"></td>
- <td><input id="btn" type=button value="3" OnClick="calc.display.value+='3'"></td>
- <td><input id="btn" type=button value="4" OnClick="calc.display.value+='4'"></td>
- <td><input id="btn" type=button value="5" OnClick="calc.display.value+='5'"></td>
- <td><input id="btn" type=button value="-" OnClick="calc.display.value+='-'"></td>
- </tr>
- <tr>
- <td><input id="btn" type=button value="MR" OnClick="calc.display.value=calc.M.value"></td>
- <td><input id="btn" type=button value="6" OnClick="calc.display.value+='6'"></td>
- <td><input id="btn" type=button value="7" OnClick="calc.display.value+='7'"></td>
- <td><input id="btn" type=button value="8" OnClick="calc.display.value+='8'"></td>
- <td><input id="btn" type=button value="x" OnClick="calc.display.value+='*'"></td>
- </tr>
- <tr>
- <td><input id="btn" type=button value="M+" OnClick="calc.M.value=(Number(calc.M.value))+(Number(calc.display.value))"></td>
- <td><input id="btn" type=button value="9" OnClick="calc.display.value+='9'"></td>
- <td><input id="btn" type=button value="±"
- OnClick="calc.display.value=(calc.display.value==Math.abs(calc.display.value)?-(calc.display.value):Math.abs(calc.display.value))">
- </td>
- <td><input id="btn" type=button value="=" OnClick="calc.display.value=eval(calc.display.value)"></td>
- <td><input id="btn" type=button value="/" OnClick="calc.display.value+='/'"></td>
- </tr>
- <tr>
- <td><input id="btn" type=button value="1/x" OnClick="calc.display.value=1/calc.display.value"></td>
- <td><input id="btn" type=button value="." OnClick="calc.display.value+='.'"></td>
- <td><input id="btn" type=button value="x2" OnClick="calc.display.value=Math.pow(calc.display.value,2)"></td>
- <td><input id="btn" type=button value="√" OnClick="calc.display.value=Math.sqrt(calc.display.value)"></td>
- <td><input id="btn" type=button value="C" OnClick="calc.display.value=''"></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
- PHP BY NISHANT
- <!DOCTYPE HTML>
- <html>
- <head>
- <style>
- .error {color: #FF0000;}
- </style>
- </head>
- <body>
- <?php
- session_start();
- $servername = "localhost";
- $username = "root";
- $password = "";
- $db="Login details";
- // Create connection
- $conn = new mysqli($servername, $username, $password,$db);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $sql1 = "Select 1 from registration";
- $res = $conn->query($sql1);
- if($res == false){
- echo "Connected successfully";
- $sql="create table registration (Slno int(3) AUTO_INCREMENT PRIMARY KEY, name varchar(30),
- email varchar(30), website varchar(30));";
- if($conn->query($sql)==TRUE)
- {
- echo "table Myguest created";
- }
- else
- echo"Error creating table:".$conn->error;
- }
- else {
- echo "The table is already created";
- }
- // define variables and set to empty values
- $test1 = 1;
- $nameErr = $emailErr = $genderErr = $websiteErr = "";
- $name = $email = $gender = $comment = $website = "";
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- if (empty($_POST["name"])) {
- $nameErr = "Name is required";
- $test1 =0;
- } else {
- $name = test_input($_POST["name"]);
- // check if name only contains letters and whitespace
- if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
- $nameErr = "Only letters and white space allowed";
- $test1 =0;
- }
- }
- if (empty($_POST["email"])) {
- $emailErr = "Email is required";
- $test1 =0;
- } else {
- $email = test_input($_POST["email"]);
- // check if e-mail address is well-formed
- if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $emailErr = "Invalid email format";
- $test1 =0;
- }
- }
- if (empty($_POST["website"])) {
- $website = "";
- } else {
- $website = test_input($_POST["website"]);
- // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
- if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
- $websiteErr = "Invalid URL";
- $test1 =0;
- }
- }
- if (empty($_POST["comment"])) {
- $comment = "";
- } else {
- $comment = test_input($_POST["comment"]);
- }
- if (empty($_POST["gender"])) {
- $genderErr = "Gender is required";
- $test1 =0;
- } else {
- $gender = test_input($_POST["gender"]);
- }
- }
- if(isset($_POST["submit"]) && $test1 == 1){
- $sql2 = "insert into registration(`name`,`email`,`website`) values('$name','$email','$website')";
- $go = 0;
- if($conn->query($sql2)){
- $go=1;
- }
- else {
- echo "Some fucking error occured! ".$conn->error;
- }
- if($go == 1){
- setcookie("Name",$name,time()+60*5);
- $_SESSION["name"] = $name;
- header("Location: newPage.php");
- }
- }
- function test_input($data) {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
- ?>
- <h2>PHP Form Validation Example</h2>
- <p><span class="error">* required field.</span></p>
- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- Name: <input type="text" name="name" value="<?php echo $name;?>">
- <span class="error">* <?php echo $nameErr;?></span>
- <br><br>
- E-mail: <input type="text" name="email" value="<?php echo $email;?>">
- <span class="error">* <?php echo $emailErr;?></span>
- <br><br>
- Website: <input type="text" name="website" value="<?php echo $website;?>">
- <span class="error"><?php echo $websiteErr;?></span>
- <br><br>
- Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
- <br><br>
- <input type="text" id="check" value = "hey" placeholder="Enter your age">
- <p id="result">Hello</p>
- <br><input type="button" value= "Verify" onclick="checker()">
- <br><br>
- Gender:
- <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
- <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
- <span class="error">* <?php echo $genderErr;?></span>
- <br><br>
- <input type="submit" name="submit" value="Submit">
- </form>
- <?php
- echo "<h2>Your Input:</h2>";
- echo $name;
- echo "<br>";
- echo $email;
- echo "<br>";
- echo $website;
- echo "<br>";
- echo $comment;
- echo "<br>";
- echo $gender;
- ?>
- </body>
- <script>
- function checker(){
- var reg = "^[a-z]{3,}$";
- document.getElementById("result").innerHTML= "Not Matched";
- var str = document.getElementById("check").value;
- if(str.match(reg)){
- document.getElementById("result").innerHTML= "matched";
- }
- /*
- if(data.match(reg)){
- res.innerHTML = "Matched";
- }
- else {
- res.innerHTML = "Not matched";
- }
- */
- }
- </script>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement