Advertisement
Guest User

all

a guest
May 3rd, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 47.39 KB | None | 0 0
  1.  
  2. //Form Validation
  3. <!DOCTYPE HTML>
  4. <html>
  5. <head>
  6. <style>
  7. .error {color: #FF0000;}
  8. </style>
  9. </head>
  10. <body>
  11.  
  12. <?php
  13. // define variables and set to empty values
  14. $nameErr = $emailErr = $genderErr = $websiteErr = "";
  15. $name = $email = $gender = $comment = $website = "";
  16.  
  17. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  18. if (empty($_POST["name"])) {
  19. $nameErr = "Name is required";
  20. } else {
  21. $name = test_input($_POST["name"]);
  22. // check if name only contains letters and whitespace
  23. if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  24. $nameErr = "Only letters and white space allowed";
  25. }
  26. }
  27.  
  28. if (empty($_POST["email"])) {
  29. $emailErr = "Email is required";
  30. } else {
  31. $email = test_input($_POST["email"]);
  32. // check if e-mail address is well-formed
  33. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  34. $emailErr = "Invalid email format";
  35. }
  36. }
  37.  
  38. if (empty($_POST["website"])) {
  39. $website = "";
  40. } else {
  41. $website = test_input($_POST["website"]);
  42. // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
  43. if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
  44. $websiteErr = "Invalid URL";
  45. }
  46. }
  47.  
  48. if (empty($_POST["comment"])) {
  49. $comment = "";
  50. } else {
  51. $comment = test_input($_POST["comment"]);
  52. }
  53.  
  54. if (empty($_POST["gender"])) {
  55. $genderErr = "Gender is required";
  56. } else {
  57. $gender = test_input($_POST["gender"]);
  58. }
  59. }
  60.  
  61. function test_input($data) {
  62. $data = trim($data);
  63. $data = stripslashes($data);
  64. $data = htmlspecialchars($data);
  65. return $data;
  66. }
  67. ?>
  68.  
  69. <h2>PHP Form Validation Example</h2>
  70. <p><span class="error">* required field.</span></p>//Form Validation
  71. <!DOCTYPE HTML>
  72. <html>
  73. <head>
  74. <style>
  75. .error {color: #FF0000;}
  76. </style>
  77. </head>
  78. <body>
  79.  
  80. <?php
  81. // define variables and set to empty values
  82. $nameErr = $emailErr = $genderErr = $websiteErr = "";
  83. $name = $email = $gender = $comment = $website = "";
  84.  
  85. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  86. if (empty($_POST["name"])) {
  87. $nameErr = "Name is required";
  88. } else {
  89. $name = test_input($_POST["name"]);
  90. // check if name only contains letters and whitespace
  91. if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  92. $nameErr = "Only letters and white space allowed";
  93. }
  94. }
  95.  
  96. if (empty($_POST["email"])) {
  97. $emailErr = "Email is required";
  98. } else {
  99. $email = test_input($_POST["email"]);
  100. // check if e-mail address is well-formed
  101. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  102. $emailErr = "Invalid email format";
  103. }
  104. }
  105.  
  106. if (empty($_POST["website"])) {
  107. $website = "";
  108. } else {
  109. $website = test_input($_POST["website"]);
  110. // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
  111. if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
  112. $websiteErr = "Invalid URL";
  113. }
  114. }
  115.  
  116. if (empty($_POST["comment"])) {
  117. $comment = "";
  118. } else {
  119. $comment = test_input($_POST["comment"]);
  120. }
  121.  
  122. if (empty($_POST["gender"])) {
  123. $genderErr = "Gender is required";
  124. } else {
  125. $gender = test_input($_POST["gender"]);
  126. }
  127. }
  128.  
  129. function test_input($data) {
  130. $data = trim($data);
  131. $data = stripslashes($data);
  132. $data = htmlspecialchars($data);
  133. return $data;
  134. }
  135. ?>
  136.  
  137. <h2>PHP Form Validation Example</h2>
  138. <p><span class="error">* required field.</span></p>
  139. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  140. Name: <input type="text" name="name" value="<?php echo $name;?>">
  141. <span class="error">* <?php echo $nameErr;?></span>
  142. <br><br>
  143. E-mail: <input type="text" name="email" value="<?php echo $email;?>">
  144. <span class="error">* <?php echo $emailErr;?></span>
  145. <br><br>
  146. Website: <input type="text" name="website" value="<?php echo $website;?>">
  147. <span class="error"><?php echo $websiteErr;?></span>
  148. <br><br>
  149. Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
  150. <br><br>
  151. Gender:
  152. <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
  153. <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
  154. <span class="error">* <?php echo $genderErr;?></span>
  155. <br><br>
  156. <input type="submit" name="submit" value="Submit">
  157. </form>
  158.  
  159. <?php
  160. echo "<h2>Your Input:</h2>";
  161. echo $name;
  162. echo "<br>";
  163. echo $email;
  164. echo "<br>";
  165. echo $website;
  166. echo "<br>";
  167. echo $comment;
  168. echo "<br>";
  169. echo $gender;
  170. ?>
  171.  
  172. </body>
  173. </html>
  174.  
  175.  
  176.  
  177.  
  178.  
  179. ---------------------------------------------------------
  180. //FILES
  181.  
  182.  
  183. file_uploads = On
  184.  
  185. HTML FILE
  186. <!DOCTYPE html>
  187. <html>
  188. <body>
  189.  
  190. <form action="upload.php" method="post" enctype="multipart/form-data">
  191. Select image to upload:
  192. <input type="file" name="fileToUpload" id="fileToUpload">
  193. <input type="submit" value="Upload Image" name="submit">
  194. </form>
  195.  
  196. </body>
  197. </html>
  198.  
  199.  
  200. COMPLETE PHP FILE
  201.  
  202. <?php
  203. $target_dir = "uploads/";
  204. $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  205. $uploadOk = 1;
  206. $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  207. // Check if image file is a actual image or fake image
  208. if(isset($_POST["submit"])) {
  209. $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  210. if($check !== false) {
  211. echo "File is an image - " . $check["mime"] . ".";
  212. $uploadOk = 1;
  213. } else {
  214. echo "File is not an image.";
  215. $uploadOk = 0;
  216. }
  217. }
  218. // Check if file already exists
  219. if (file_exists($target_file)) {
  220. echo "Sorry, file already exists.";
  221. $uploadOk = 0;
  222. }
  223. // Check file size
  224. if ($_FILES["fileToUpload"]["size"] > 500000) {
  225. echo "Sorry, your file is too large.";
  226. $uploadOk = 0;
  227. }
  228. // Allow certain file formats
  229. if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
  230. && $imageFileType != "gif" ) {
  231. echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  232. $uploadOk = 0;
  233. }
  234. // Check if $uploadOk is set to 0 by an error
  235. if ($uploadOk == 0) {
  236. echo "Sorry, your file was not uploaded.";
  237. // if everything is ok, try to upload file
  238. } else {
  239. if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
  240. echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
  241. } else {
  242. echo "Sorry, there was an error uploading your file.";
  243. }
  244. }
  245. ?>
  246.  
  247. --------------------------------------------------
  248. SQL operations
  249.  
  250. open connection
  251.  
  252. <?php
  253. $servername = "localhost";
  254. $username = "username";
  255. $password = "password";
  256.  
  257. // Create connection
  258. $conn = new mysqli($servername, $username, $password);
  259.  
  260. // Check connection
  261. if ($conn->connect_error) {
  262. die("Connection failed: " . $conn->connect_error);
  263. }
  264. echo "Connected successfully";
  265. ?>
  266.  
  267. <?php
  268. $servername = "localhost";
  269. $username = "username";
  270. $password = "password";
  271.  
  272. // Create connection
  273. $conn = new mysqli($servername, $username, $password);
  274. // Check connection
  275. if ($conn->connect_error) {
  276. die("Connection failed: " . $conn->connect_error);
  277. }
  278.  
  279.  
  280.  
  281. Create database
  282. $sql = "CREATE DATABASE myDB";
  283. if ($conn->query($sql) === TRUE) {
  284. echo "Database created successfully";
  285. } else {
  286. echo "Error creating database: " . $conn->error;
  287. }
  288.  
  289. $conn->close();
  290. ?>
  291.  
  292.  
  293. CREATE TABLE
  294.  
  295.  
  296. <?php
  297. $servername = "localhost";
  298. $username = "username";
  299. $password = "password";
  300. $dbname = "myDB";
  301.  
  302. // Create connection
  303. $conn = new mysqli($servername, $username, $password, $dbname);
  304. // Check connection
  305. if ($conn->connect_error) {
  306. die("Connection failed: " . $conn->connect_error);
  307. }
  308.  
  309. //sql to create table
  310. $sql = "CREATE TABLE MyGuests (
  311. id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  312. firstname VARCHAR(30) NOT NULL,
  313. lastname VARCHAR(30) NOT NULL,
  314. email VARCHAR(50),
  315. reg_date TIMESTAMP
  316. )";
  317.  
  318. if ($conn->query($sql) === TRUE) {
  319. echo "Table MyGuests created successfully";
  320. } else {
  321. echo "Error creating table: " . $conn->error;
  322. }
  323.  
  324. $conn->close();
  325. ?>
  326.  
  327.  
  328.  
  329.  
  330. INSERT DATA
  331.  
  332.  
  333.  
  334. <?php
  335. $servername = "localhost";
  336. $username = "username";
  337. $password = "password";
  338. $dbname = "myDB";
  339.  
  340. // Create connection
  341. $conn = new mysqli($servername, $username, $password, $dbname);
  342. // Check connection
  343. if ($conn->connect_error) {
  344. die("Connection failed: " . $conn->connect_error);
  345. }
  346.  
  347. $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  348. VALUES ('John', 'Doe', 'john@example.com')";
  349.  
  350. if ($conn->query($sql) === TRUE) {
  351. echo "New record created successfully";
  352. } else {
  353. echo "Error: " . $sql . "<br>" . $conn->error;
  354. }
  355.  
  356. $conn->close();
  357. ?>
  358.  
  359.  
  360.  
  361. INSERT MULTIPLE
  362.  
  363.  
  364.  
  365.  
  366. $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  367. VALUES ('John', 'Doe', 'john@example.com');";
  368. $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
  369. VALUES ('Mary', 'Moe', 'mary@example.com');";
  370. $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
  371. VALUES ('Julie', 'Dooley', 'julie@example.com')";
  372.  
  373.  
  374.  
  375.  
  376. LAST ID:
  377.  
  378.  
  379. $conn->insert_id;
  380.  
  381.  
  382. <?php
  383. $servername = "localhost";
  384. $username = "username";
  385. $password = "password";
  386. $dbname = "myDB";
  387.  
  388. // Create connection
  389. $conn = new mysqli($servername, $username, $password, $dbname);
  390.  
  391. // Check connection
  392. if ($conn->connect_error) {
  393. die("Connection failed: " . $conn->connect_error);
  394. }
  395.  
  396. // prepare and bind
  397. $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
  398. $stmt->bind_param("sss", $firstname, $lastname, $email);
  399.  
  400. // set parameters and execute
  401. $firstname = "John";
  402. $lastname = "Doe";
  403. $email = "john@example.com";
  404. $stmt->execute();
  405.  
  406. $firstname = "Mary";
  407. $lastname = "Moe";
  408. $email = "mary@example.com";
  409. $stmt->execute();
  410.  
  411. $firstname = "Julie";
  412. $lastname = "Dooley";
  413. $email = "julie@example.com";
  414. $stmt->execute();
  415.  
  416. echo "New records created successfully";
  417.  
  418. $stmt->close();
  419. $conn->close();
  420. ?>
  421.  
  422. //Form Validation
  423. <!DOCTYPE HTML>
  424. <html>
  425. <head>
  426. <style>
  427. .error {color: #FF0000;}
  428. </style>
  429. </head>
  430. <body>
  431.  
  432. <?php
  433. // define variables and set to empty values
  434. $nameErr = $emailErr = $genderErr = $websiteErr = "";
  435. $name = $email = $gender = $comment = $website = "";
  436.  
  437. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  438. if (empty($_POST["name"])) {
  439. $nameErr = "Name is required";
  440. } else {
  441. $name = test_input($_POST["name"]);
  442. // check if name only contains letters and whitespace
  443. if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  444. $nameErr = "Only letters and white space allowed";
  445. }
  446. }
  447.  
  448. if (empty($_POST["email"])) {
  449. $emailErr = "Email is required";
  450. } else {
  451. $email = test_input($_POST["email"]);
  452. // check if e-mail address is well-formed
  453. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  454. $emailErr = "Invalid email format";
  455. }
  456. }
  457.  
  458. if (empty($_POST["website"])) {
  459. $website = "";
  460. } else {
  461. $website = test_input($_POST["website"]);
  462. // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
  463. if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
  464. $websiteErr = "Invalid URL";
  465. }
  466. }
  467.  
  468. if (empty($_POST["comment"])) {
  469. $comment = "";
  470. } else {
  471. $comment = test_input($_POST["comment"]);
  472. }
  473.  
  474. if (empty($_POST["gender"])) {
  475. $genderErr = "Gender is required";
  476. } else {
  477. $gender = test_input($_POST["gender"]);
  478. }
  479. }
  480.  
  481. function test_input($data) {
  482. $data = trim($data);
  483. $data = stripslashes($data);
  484. $data = htmlspecialchars($data);
  485. return $data;
  486. }
  487. ?>
  488.  
  489. <h2>PHP Form Validation Example</h2>
  490. <p><span class="error">* required field.</span></p>
  491. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  492. Name: <input type="text" name="name" value="<?php echo $name;?>">
  493. <span class="error">* <?php echo $nameErr;?></span>
  494. <br><br>
  495. E-mail: <input type="text" name="email" value="<?php echo $email;?>">
  496. <span class="error">* <?php echo $emailErr;?></span>
  497. <br><br>
  498. Website: <input type="text" name="website" value="<?php echo $website;?>">
  499. <span class="error"><?php echo $websiteErr;?></span>
  500. <br><br>
  501. Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
  502. <br><br>
  503. Gender:
  504. <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
  505. <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
  506. <span class="error">* <?php echo $genderErr;?></span>
  507. <br><br>
  508. <input type="submit" name="submit" value="Submit">
  509. </form>
  510.  
  511. <?php
  512. echo "<h2>Your Input:</h2>";
  513. echo $name;
  514. echo "<br>";
  515. echo $email;
  516. echo "<br>";
  517. echo $website;
  518. echo "<br>";
  519. echo $comment;
  520. echo "<br>";
  521. echo $gender;
  522. ?>
  523.  
  524. </body>
  525. </html>
  526.  
  527.  
  528.  
  529.  
  530.  
  531. ---------------------------------------------------------
  532. //FILES
  533.  
  534.  
  535. file_uploads = On
  536.  
  537. HTML FILE
  538. <!DOCTYPE html>
  539. <html>
  540. <body>
  541.  
  542. <form action="upload.php" method="post" enctype="multipart/form-data">
  543. Select image to upload:
  544. <input type="file" name="fileToUpload" id="fileToUpload">
  545. <input type="submit" value="Upload Image" name="submit">
  546. </form>
  547.  
  548. </body>
  549. </html>
  550.  
  551.  
  552. COMPLETE PHP FILE
  553.  
  554. <?php
  555. $target_dir = "uploads/";
  556. $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  557. $uploadOk = 1;
  558. $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  559. // Check if image file is a actual image or fake image
  560. if(isset($_POST["submit"])) {
  561. $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  562. if($check !== false) {
  563. echo "File is an image - " . $check["mime"] . ".";
  564. $uploadOk = 1;
  565. } else {
  566. echo "File is not an image.";
  567. $uploadOk = 0;
  568. }
  569. }
  570. // Check if file already exists
  571. if (file_exists($target_file)) {
  572. echo "Sorry, file already exists.";
  573. $uploadOk = 0;
  574. }
  575. // Check file size
  576. if ($_FILES["fileToUpload"]["size"] > 500000) {
  577. echo "Sorry, your file is too large.";
  578. $uploadOk = 0;
  579. }
  580. // Allow certain file formats
  581. if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
  582. && $imageFileType != "gif" ) {
  583. echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  584. $uploadOk = 0;
  585. }
  586. // Check if $uploadOk is set to 0 by an error
  587. if ($uploadOk == 0) {
  588. echo "Sorry, your file was not uploaded.";
  589. // if everything is ok, try to upload file
  590. } else {
  591. if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
  592. echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
  593. } else {
  594. echo "Sorry, there was an error uploading your file.";
  595. }
  596. }
  597. ?>
  598.  
  599. --------------------------------------------------
  600. SQL operations
  601.  
  602. open connection
  603.  
  604. <?php
  605. $servername = "localhost";
  606. $username = "username";
  607. $password = "password";
  608.  
  609. // Create connection
  610. $conn = new mysqli($servername, $username, $password);
  611.  
  612. // Check connection
  613. if ($conn->connect_error) {
  614. die("Connection failed: " . $conn->connect_error);
  615. }
  616. echo "Connected successfully";
  617. ?>
  618.  
  619. <?php
  620. $servername = "localhost";
  621. $username = "username";
  622. $password = "password";
  623.  
  624. // Create connection
  625. $conn = new mysqli($servername, $username, $password);
  626. // Check connection
  627. if ($conn->connect_error) {
  628. die("Connection failed: " . $conn->connect_error);
  629. }
  630.  
  631.  
  632.  
  633. Create database
  634. $sql = "CREATE DATABASE myDB";
  635. if ($conn->query($sql) === TRUE) {
  636. echo "Database created successfully";
  637. } else {
  638. echo "Error creating database: " . $conn->error;
  639. }
  640.  
  641. $conn->close();
  642. ?>
  643.  
  644.  
  645. CREATE TABLE
  646.  
  647.  
  648. <?php
  649. $servername = "localhost";
  650. $username = "username";
  651. $password = "password";
  652. $dbname = "myDB";
  653.  
  654. // Create connection
  655. $conn = new mysqli($servername, $username, $password, $dbname);
  656. // Check connection
  657. if ($conn->connect_error) {
  658. die("Connection failed: " . $conn->connect_error);
  659. }
  660.  
  661. //sql to create table
  662. $sql = "CREATE TABLE MyGuests (
  663. id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  664. firstname VARCHAR(30) NOT NULL,
  665. lastname VARCHAR(30) NOT NULL,
  666. email VARCHAR(50),
  667. reg_date TIMESTAMP
  668. )";
  669.  
  670. if ($conn->query($sql) === TRUE) {
  671. echo "Table MyGuests created successfully";
  672. } else {
  673. echo "Error creating table: " . $conn->error;
  674. }
  675.  
  676. $conn->close();
  677. ?>
  678.  
  679.  
  680.  
  681.  
  682. INSERT DATA
  683.  
  684.  
  685.  
  686. <?php
  687. $servername = "localhost";
  688. $username = "username";
  689. $password = "password";
  690. $dbname = "myDB";
  691.  
  692. // Create connection
  693. $conn = new mysqli($servername, $username, $password, $dbname);
  694. // Check connection
  695. if ($conn->connect_error) {
  696. die("Connection failed: " . $conn->connect_error);
  697. }
  698.  
  699. $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  700. VALUES ('John', 'Doe', 'john@example.com')";
  701.  
  702. if ($conn->query($sql) === TRUE) {
  703. echo "New record created successfully";
  704. } else {
  705. echo "Error: " . $sql . "<br>" . $conn->error;
  706. }
  707.  
  708. $conn->close();
  709. ?>
  710.  
  711.  
  712.  
  713. INSERT MULTIPLE
  714.  
  715.  
  716.  
  717.  
  718. $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  719. VALUES ('John', 'Doe', 'john@example.com');";
  720. $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
  721. VALUES ('Mary', 'Moe', 'mary@example.com');";
  722. $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
  723. VALUES ('Julie', 'Dooley', 'julie@example.com')";
  724.  
  725.  
  726.  
  727.  
  728. LAST ID:
  729.  
  730.  
  731. $conn->insert_id;
  732.  
  733.  
  734. <?php
  735. $servername = "localhost";
  736. $username = "username";
  737. $password = "password";
  738. $dbname = "myDB";
  739.  
  740. // Create connection
  741. $conn = new mysqli($servername, $username, $password, $dbname);
  742.  
  743. // Check connection
  744. if ($conn->connect_error) {
  745. die("Connection failed: " . $conn->connect_error);
  746. }
  747.  
  748. // prepare and bind
  749. $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
  750. $stmt->bind_param("sss", $firstname, $lastname, $email);
  751.  
  752. // set parameters and execute
  753. $firstname = "John";
  754. $lastname = "Doe";
  755. $email = "john@example.com";
  756. $stmt->execute();
  757.  
  758. $firstname = "Mary";
  759. $lastname = "Moe";
  760. $email = "mary@example.com";
  761. $stmt->execute();
  762.  
  763. $firstname = "Julie";
  764. $lastname = "Dooley";
  765. $email = "julie@example.com";
  766. $stmt->execute();
  767.  
  768. echo "New records created successfully";
  769.  
  770. $stmt->close();
  771. $conn->close();
  772. ?>
  773.  
  774.  
  775.  
  776. SELECT DATA
  777.  
  778. $sql = "SELECT id, firstname, lastname FROM MyGuests";
  779.  
  780.  
  781. DELETE DATA
  782.  
  783.  
  784. $sql = "DELETE FROM MyGuests WHERE id=3";
  785.  
  786.  
  787.  
  788. UPDATE DATA
  789.  
  790. $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
  791.  
  792.  
  793. LIMIT DATA
  794.  
  795.  
  796. $sql = "SELECT * FROM Orders LIMIT 15, 10";
  797.  
  798. SELECT DATA
  799.  
  800. $sql = "SELECT id, firstname, lastname FROM MyGuests";
  801.  
  802.  
  803. DELETE DATA
  804.  
  805.  
  806. $sql = "DELETE FROM MyGuests WHERE id=3";
  807.  
  808.  
  809.  
  810. UPDATE DATA
  811.  
  812. $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
  813.  
  814.  
  815. LIMIT DATA
  816.  
  817.  
  818. $sql = "SELECT * FROM Orders LIMIT 15, 10";
  819. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  820. Name: <input type="text" name="name" value="<?php echo $name;?>">
  821. <span class="error">* <?php echo $nameErr;?></span>
  822. <br><br>
  823. E-mail: <input type="text" name="email" value="<?php echo $email;?>">
  824. <span class="error">* <?php echo $emailErr;?></span>
  825. <br><br>
  826. Website: <input type="text" name="website" value="<?php echo $website;?>">
  827. <span class="error"><?php echo $websiteErr;?></span>
  828. <br><br>
  829. Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
  830. <br><br>
  831. Gender:
  832. <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
  833. <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
  834. <span class="error">* <?php echo $genderErr;?></span>
  835. <br><br>
  836. <input type="submit" name="submit" value="Submit">
  837. </form>
  838.  
  839. <?php
  840. echo "<h2>Your Input:</h2>";
  841. echo $name;
  842. echo "<br>";
  843. echo $email;
  844. echo "<br>";
  845. echo $website;
  846. echo "<br>";
  847. echo $comment;
  848. echo "<br>";
  849. echo $gender;
  850. ?>
  851.  
  852. </body>
  853. </html>
  854.  
  855.  
  856.  
  857.  
  858.  
  859. ---------------------------------------------------------
  860. //FILES
  861.  
  862.  
  863. file_uploads = On
  864.  
  865. HTML FILE
  866. <!DOCTYPE html>
  867. <html>
  868. <body>
  869.  
  870. <form action="upload.php" method="post" enctype="multipart/form-data">
  871. Select image to upload:
  872. <input type="file" name="fileToUpload" id="fileToUpload">
  873. <input type="submit" value="Upload Image" name="submit">
  874. </form>
  875.  
  876. </body>
  877. </html>
  878.  
  879.  
  880. COMPLETE PHP FILE
  881.  
  882. <?php
  883. $target_dir = "uploads/";
  884. $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  885. $uploadOk = 1;
  886. $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  887. // Check if image file is a actual image or fake image
  888. if(isset($_POST["submit"])) {
  889. $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  890. if($check !== false) {
  891. echo "File is an image - " . $check["mime"] . ".";
  892. $uploadOk = 1;
  893. } else {
  894. echo "File is not an image.";
  895. $uploadOk = 0;
  896. }
  897. }
  898. // Check if file already exists
  899. if (file_exists($target_file)) {
  900. echo "Sorry, file already exists.";
  901. $uploadOk = 0;
  902. }
  903. // Check file size
  904. if ($_FILES["fileToUpload"]["size"] > 500000) {
  905. echo "Sorry, your file is too large.";
  906. $uploadOk = 0;
  907. }
  908. // Allow certain file formats
  909. if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
  910. && $imageFileType != "gif" ) {
  911. echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  912. $uploadOk = 0;
  913. }
  914. // Check if $uploadOk is set to 0 by an error
  915. if ($uploadOk == 0) {
  916. echo "Sorry, your file was not uploaded.";
  917. // if everything is ok, try to upload file
  918. } else {
  919. if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
  920. echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
  921. } else {
  922. echo "Sorry, there was an error uploading your file.";
  923. }
  924. }
  925. ?>
  926.  
  927. --------------------------------------------------
  928. SQL operations
  929.  
  930. open connection
  931.  
  932. <?php
  933. $servername = "localhost";
  934. $username = "username";
  935. $password = "password";
  936.  
  937. // Create connection
  938. $conn = new mysqli($servername, $username, $password);
  939.  
  940. // Check connection
  941. if ($conn->connect_error) {
  942. die("Connection failed: " . $conn->connect_error);
  943. }
  944. echo "Connected successfully";
  945. ?>
  946.  
  947. <?php
  948. $servername = "localhost";
  949. $username = "username";
  950. $password = "password";
  951.  
  952. // Create connection
  953. $conn = new mysqli($servername, $username, $password);
  954. // Check connection
  955. if ($conn->connect_error) {
  956. die("Connection failed: " . $conn->connect_error);
  957. }
  958.  
  959.  
  960.  
  961. Create database
  962. $sql = "CREATE DATABASE myDB";
  963. if ($conn->query($sql) === TRUE) {
  964. echo "Database created successfully";
  965. } else {
  966. echo "Error creating database: " . $conn->error;
  967. }
  968.  
  969. $conn->close();
  970. ?>
  971.  
  972.  
  973. CREATE TABLE
  974.  
  975.  
  976. <?php
  977. $servername = "localhost";
  978. $username = "username";
  979. $password = "password";
  980. $dbname = "myDB";
  981.  
  982. // Create connection
  983. $conn = new mysqli($servername, $username, $password, $dbname);
  984. // Check connection
  985. if ($conn->connect_error) {
  986. die("Connection failed: " . $conn->connect_error);
  987. }
  988.  
  989. //sql to create table
  990. $sql = "CREATE TABLE MyGuests (
  991. id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  992. firstname VARCHAR(30) NOT NULL,
  993. lastname VARCHAR(30) NOT NULL,
  994. email VARCHAR(50),
  995. reg_date TIMESTAMP
  996. )";
  997.  
  998. if ($conn->query($sql) === TRUE) {
  999. echo "Table MyGuests created successfully";
  1000. } else {
  1001. echo "Error creating table: " . $conn->error;
  1002. }
  1003.  
  1004. $conn->close();
  1005. ?>
  1006.  
  1007.  
  1008.  
  1009.  
  1010. INSERT DATA
  1011.  
  1012.  
  1013.  
  1014. <?php
  1015. $servername = "localhost";
  1016. $username = "username";
  1017. $password = "password";
  1018. $dbname = "myDB";
  1019.  
  1020. // Create connection
  1021. $conn = new mysqli($servername, $username, $password, $dbname);
  1022. // Check connection
  1023. if ($conn->connect_error) {
  1024. die("Connection failed: " . $conn->connect_error);
  1025. }
  1026.  
  1027. $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  1028. VALUES ('John', 'Doe', 'john@example.com')";
  1029.  
  1030. if ($conn->query($sql) === TRUE) {
  1031. echo "New record created successfully";
  1032. } else {
  1033. echo "Error: " . $sql . "<br>" . $conn->error;
  1034. }
  1035.  
  1036. $conn->close();
  1037. ?>
  1038.  
  1039.  
  1040.  
  1041. INSERT MULTIPLE
  1042.  
  1043.  
  1044.  
  1045.  
  1046. $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  1047. VALUES ('John', 'Doe', 'john@example.com');";
  1048. $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
  1049. VALUES ('Mary', 'Moe', 'mary@example.com');";
  1050. $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
  1051. VALUES ('Julie', 'Dooley', 'julie@example.com')";
  1052.  
  1053.  
  1054.  
  1055.  
  1056. LAST ID:
  1057.  
  1058.  
  1059. $conn->insert_id;
  1060.  
  1061.  
  1062. <?php
  1063. $servername = "localhost";
  1064. $username = "username";
  1065. $password = "password";
  1066. $dbname = "myDB";
  1067.  
  1068. // Create connection
  1069. $conn = new mysqli($servername, $username, $password, $dbname);
  1070.  
  1071. // Check connection
  1072. if ($conn->connect_error) {
  1073. die("Connection failed: " . $conn->connect_error);
  1074. }
  1075.  
  1076. // prepare and bind
  1077. $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
  1078. $stmt->bind_param("sss", $firstname, $lastname, $email);
  1079.  
  1080. // set parameters and execute
  1081. $firstname = "John";
  1082. $lastname = "Doe";
  1083. $email = "john@example.com";
  1084. $stmt->execute();
  1085.  
  1086. $firstname = "Mary";
  1087. $lastname = "Moe";
  1088. $email = "mary@example.com";
  1089. $stmt->execute();
  1090.  
  1091. $firstname = "Julie";
  1092. $lastname = "Dooley";
  1093. $email = "julie@example.com";
  1094. $stmt->execute();
  1095.  
  1096. echo "New records created successfully";
  1097.  
  1098. $stmt->close();
  1099. $conn->close();
  1100. ?>
  1101.  
  1102.  
  1103.  
  1104. SELECT DATA
  1105.  
  1106. $sql = "SELECT id, firstname, lastname FROM MyGuests";
  1107.  
  1108.  
  1109. DELETE DATA
  1110.  
  1111.  
  1112. $sql = "DELETE FROM MyGuests WHERE id=3";
  1113.  
  1114.  
  1115.  
  1116. UPDATE DATA
  1117.  
  1118. $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
  1119.  
  1120.  
  1121. LIMIT DATA
  1122.  
  1123.  
  1124. $sql = "SELECT * FROM Orders LIMIT 15, 10";
  1125. ---------------------------------------------
  1126. Validation of number
  1127.  
  1128.  
  1129. <!DOCTYPE html>
  1130. <html>
  1131. <body>
  1132.  
  1133. <p>Enter a number and click OK:</p>
  1134.  
  1135. <input id="id1" type="number" min="100" max="300" required>
  1136. <button onclick="myFunction()">OK</button>
  1137.  
  1138. <p>If the number is less than 100 or greater than 300, an error message will be displayed.</p>
  1139.  
  1140. <p id="demo"></p>
  1141.  
  1142. <script>
  1143. function myFunction() {
  1144. var inpObj = document.getElementById("id1");
  1145. if (inpObj.checkValidity() == false) {
  1146. document.getElementById("demo").innerHTML = inpObj.validationMessage;
  1147. } else {
  1148. document.getElementById("demo").innerHTML = "Input OK";
  1149. }
  1150. }
  1151. </script>
  1152.  
  1153. </body>
  1154. </html>
  1155.  
  1156.  
  1157.  
  1158.  
  1159. <!DOCTYPE html>
  1160. <html>
  1161. <body>
  1162.  
  1163. <p>Enter a number and click OK:</p>
  1164.  
  1165. <input id="id1" type="number" max="100">
  1166. <button onclick="myFunction()">OK</button>
  1167.  
  1168. <p>If the number is greater than 100 (the input's max attribute), an error message will be displayed.</p>
  1169.  
  1170. <p id="demo"></p>
  1171.  
  1172. <script>
  1173. function myFunction() {
  1174. var txt = "";
  1175. if (document.getElementById("id1").validity.rangeOverflow) {
  1176. txt = "Value too large";
  1177. } else {
  1178. txt = "Input OK";
  1179. }
  1180. document.getElementById("demo").innerHTML = txt;
  1181. }
  1182. </script>
  1183.  
  1184. </body>
  1185. </html>
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191. <input id="id1" type="number" min="100">
  1192. <button onclick="myFunction()">OK</button>
  1193.  
  1194. <p id="demo"></p>
  1195.  
  1196. <script>
  1197. function myFunction() {
  1198. var txt = "";
  1199. if (document.getElementById("id1").validity.rangeUnderflow) {
  1200. txt = "Value too small";
  1201. }
  1202. document.getElementById("demo").innerHTML = txt;
  1203. }
  1204. </script>
  1205.  
  1206.  
  1207. javascript events
  1208.  
  1209. ONBLUR EVENT
  1210. <!DOCTYPE html>
  1211. <html>
  1212. <head>
  1213. <script>
  1214. function myFunction() {
  1215. var x = document.getElementById("fname");
  1216. x.value = x.value.toUpperCase();
  1217. }
  1218. </script>
  1219. </head>
  1220. <body>
  1221.  
  1222. Enter your name: <input type="text" id="fname" onblur="myFunction()">
  1223.  
  1224. <p>When you leave the input field, a function is triggered which transforms the input text to upper case.</p>
  1225.  
  1226. </body>
  1227. </html>
  1228.  
  1229. ON MOUSE OVER
  1230.  
  1231.  
  1232.  
  1233. <!DOCTYPE html>
  1234. <html>
  1235. <body>
  1236.  
  1237. <h1 onmouseover="style.color='red'" onmouseout="style.color='black'">Mouse over this text</h1>
  1238.  
  1239. </body>
  1240. </html>
  1241. ONMOUSEUP
  1242.  
  1243.  
  1244. <!DOCTYPE html>
  1245. <html>
  1246. <head>
  1247. <script>
  1248. function myFunction(elmnt, clr) {
  1249. elmnt.style.color = clr;
  1250. }
  1251. </script>
  1252. </head>
  1253. <body>
  1254. javascript events
  1255.  
  1256. ONBLUR EVENT
  1257. <!DOCTYPE html>
  1258. <html>
  1259. <head>
  1260. <script>
  1261. function myFunction() {
  1262. var x = document.getElementById("fname");
  1263. x.value = x.value.toUpperCase();
  1264. }
  1265. </script>
  1266. </head>
  1267. <body>
  1268.  
  1269. Enter your name: <input type="text" id="fname" onblur="myFunction()">
  1270.  
  1271. <p>When you leave the input field, a function is triggered which transforms the input text to upper case.</p>
  1272.  
  1273. </body>
  1274. </html>
  1275.  
  1276. ON MOUSE OVER
  1277.  
  1278.  
  1279.  
  1280. <!DOCTYPE html>
  1281. <html>
  1282. <body>
  1283.  
  1284. <h1 onmouseover="style.color='red'" onmouseout="style.color='black'">Mouse over this text</h1>
  1285.  
  1286. </body>
  1287. </html>
  1288. ONMOUSEUP
  1289.  
  1290.  
  1291. <!DOCTYPE html>
  1292. <html>
  1293. <head>
  1294. <script>
  1295. function myFunction(elmnt, clr) {
  1296. elmnt.style.color = clr;
  1297. }
  1298. </script>
  1299. </head>
  1300. <body>
  1301.  
  1302. <p onmousedown="myFunction(this,'red')" onmouseup="myFunction(this,'green')">
  1303. 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.
  1304. </p>
  1305.  
  1306. </body>
  1307. </html>
  1308.  
  1309.  
  1310.  
  1311. ONMOUSEDOWN
  1312.  
  1313.  
  1314.  
  1315.  
  1316. <!DOCTYPE html>
  1317. <html>
  1318. <head>
  1319. <script>
  1320. function whichElement(e) {
  1321. var targ;
  1322. if (!e) {
  1323. var e = window.event;
  1324. }
  1325. if (e.target) {
  1326. targ=e.target;
  1327. } else if (e.srcElement) {
  1328. targ=e.srcElement;
  1329. }
  1330. var tname;
  1331. tname = targ.tagName;
  1332. alert("You clicked on a " + tname + " element.");
  1333. }
  1334. </script>
  1335. </head>
  1336. <body onmousedown="whichElement(event)">
  1337.  
  1338. <p>Click somewhere in the document. An alert box will alert the name of the element you clicked on.</p>
  1339. <h3>This is a heading</h3>
  1340. <img border="0" src="smiley.gif" alt="Smiley" width="32" height="32">
  1341. <p>This is a paragraph.</p>
  1342.  
  1343. </body>
  1344. </html>
  1345.  
  1346. ONLOAD
  1347.  
  1348.  
  1349.  
  1350. <!DOCTYPE html>
  1351. <html>
  1352. <head>
  1353. <script>
  1354. function myFunction() {
  1355. alert("Page is loaded");
  1356. }
  1357. </script>
  1358. </head>
  1359.  
  1360. <body onload="myFunction()">
  1361. <h2>Hello World!</h2>
  1362. </body>
  1363.  
  1364. </html>
  1365. ---------------------------------------------------------------------------------
  1366.  
  1367. CLOCK
  1368.  
  1369. <!DOCTYPE html>
  1370. <html>
  1371. <head>
  1372. <script>
  1373. function startTime() {
  1374. var today = new Date();
  1375. var h = today.getHours();
  1376. var m = today.getMinutes();
  1377. var s = today.getSeconds();
  1378. m = checkTime(m);
  1379. s = checkTime(s);
  1380. document.getElementById('txt').innerHTML =
  1381. h + ":" + m + ":" + s;
  1382. var t = setTimeout(startTime, 500);
  1383. }
  1384. function checkTime(i) {
  1385. if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
  1386. return i;
  1387. }
  1388. </script>
  1389. </head>
  1390.  
  1391. <body onload="startTime()">
  1392.  
  1393. <div id="txt"></div>
  1394.  
  1395. </body>
  1396. </html>
  1397.  
  1398.  
  1399. ------------------------------------------
  1400.  
  1401. CALCULATOR
  1402.  
  1403.  
  1404. <html>
  1405. <head></head>
  1406. <body>
  1407. <h3>Simple Calculator</h3>
  1408. <br/>
  1409. <style>
  1410. #calc{width:300px;height:250px;}
  1411. #btn{width:100%;height:40px;font-size:20px;}
  1412. </style>
  1413. <form Name="calc">
  1414. <table id="calc" border=2>
  1415. <tr>
  1416. <td colspan=5><input id="btn" name="display" onkeypress="return event.charCode >= 48 && event.charCode <= 57" type="text"></td>
  1417. <td style="display:none"><input name="M" type="number"></td>
  1418. </tr>
  1419. <tr>
  1420. <td><input id="btn" type=button value="MC" OnClick="calc.M.value=''"></td>
  1421. <td><input id="btn" type=button value="0" OnClick="calc.display.value+='0'"></td>
  1422. <td><input id="btn" type=button value="1" OnClick="calc.display.value+='1'"></td>
  1423. <td><input id="btn" type=button value="2" OnClick="calc.display.value+='2'"></td>
  1424. <td><input id="btn" type=button value="+" OnClick="calc.display.value+='+'"></td>
  1425. </tr>
  1426. <tr>
  1427. <td><input id="btn" type=button value="MS" OnClick="calc.M.value=calc.display.value"></td>
  1428. <td><input id="btn" type=button value="3" OnClick="calc.display.value+='3'"></td>
  1429. <td><input id="btn" type=button value="4" OnClick="calc.display.value+='4'"></td>
  1430. <td><input id="btn" type=button value="5" OnClick="calc.display.value+='5'"></td>
  1431. <td><input id="btn" type=button value="-" OnClick="calc.display.value+='-'"></td>
  1432. </tr>
  1433. <tr>
  1434. <td><input id="btn" type=button value="MR" OnClick="calc.display.value=calc.M.value"></td>
  1435. <td><input id="btn" type=button value="6" OnClick="calc.display.value+='6'"></td>
  1436. <td><input id="btn" type=button value="7" OnClick="calc.display.value+='7'"></td>
  1437. <td><input id="btn" type=button value="8" OnClick="calc.display.value+='8'"></td>
  1438. <td><input id="btn" type=button value="x" OnClick="calc.display.value+='*'"></td>
  1439. </tr>
  1440. <tr>
  1441. <td><input id="btn" type=button value="M+" OnClick="calc.M.value=(Number(calc.M.value))+(Number(calc.display.value))"></td>
  1442. <td><input id="btn" type=button value="9" OnClick="calc.display.value+='9'"></td>
  1443. <td><input id="btn" type=button value="±"
  1444.  
  1445. OnClick="calc.display.value=(calc.display.value==Math.abs(calc.display.value)?-(calc.display.value):Math.abs(calc.display.value))">
  1446.  
  1447. </td>
  1448. <td><input id="btn" type=button value="=" OnClick="calc.display.value=eval(calc.display.value)"></td>
  1449. <td><input id="btn" type=button value="/" OnClick="calc.display.value+='/'"></td>
  1450. </tr>
  1451. <tr>
  1452. <td><input id="btn" type=button value="1/x" OnClick="calc.display.value=1/calc.display.value"></td>
  1453. <td><input id="btn" type=button value="." OnClick="calc.display.value+='.'"></td>
  1454. <td><input id="btn" type=button value="x2" OnClick="calc.display.value=Math.pow(calc.display.value,2)"></td>
  1455. <td><input id="btn" type=button value="√" OnClick="calc.display.value=Math.sqrt(calc.display.value)"></td>
  1456. <td><input id="btn" type=button value="C" OnClick="calc.display.value=''"></td>
  1457. </tr>
  1458. </table>
  1459. </form>
  1460. </body>
  1461. </html>
  1462.  
  1463.  
  1464.  
  1465. PHP BY NISHANT
  1466.  
  1467. <!DOCTYPE HTML>
  1468. <html>
  1469. <head>
  1470. <style>
  1471. .error {color: #FF0000;}
  1472. </style>
  1473. </head>
  1474. <body>
  1475.  
  1476. <?php
  1477. session_start();
  1478. $servername = "localhost";
  1479. $username = "root";
  1480. $password = "";
  1481. $db="Login details";
  1482.  
  1483. // Create connection
  1484. $conn = new mysqli($servername, $username, $password,$db);
  1485.  
  1486. // Check connection
  1487. if ($conn->connect_error) {
  1488. die("Connection failed: " . $conn->connect_error);
  1489. }
  1490. $sql1 = "Select 1 from registration";
  1491. $res = $conn->query($sql1);
  1492. if($res == false){
  1493. echo "Connected successfully";
  1494. $sql="create table registration (Slno int(3) AUTO_INCREMENT PRIMARY KEY, name varchar(30),
  1495. email varchar(30), website varchar(30));";
  1496. if($conn->query($sql)==TRUE)
  1497. {
  1498. echo "table Myguest created";
  1499. }
  1500. else
  1501. echo"Error creating table:".$conn->error;
  1502. }
  1503. else {
  1504. echo "The table is already created";
  1505. }
  1506. // define variables and set to empty values
  1507. $test1 = 1;
  1508. $nameErr = $emailErr = $genderErr = $websiteErr = "";
  1509. $name = $email = $gender = $comment = $website = "";
  1510.  
  1511. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  1512. if (empty($_POST["name"])) {
  1513. $nameErr = "Name is required";
  1514. $test1 =0;
  1515. } else {
  1516. $name = test_input($_POST["name"]);
  1517. // check if name only contains letters and whitespace
  1518. if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  1519. $nameErr = "Only letters and white space allowed";
  1520. $test1 =0;
  1521.  
  1522. }
  1523. }
  1524.  
  1525. if (empty($_POST["email"])) {
  1526. $emailErr = "Email is required";
  1527. $test1 =0;
  1528.  
  1529. } else {
  1530. $email = test_input($_POST["email"]);
  1531. // check if e-mail address is well-formed
  1532. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  1533. $emailErr = "Invalid email format";
  1534. $test1 =0;
  1535.  
  1536. }
  1537. }
  1538.  
  1539. if (empty($_POST["website"])) {
  1540. $website = "";
  1541. } else {
  1542. $website = test_input($_POST["website"]);
  1543. // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
  1544. if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
  1545. $websiteErr = "Invalid URL";
  1546. $test1 =0;
  1547.  
  1548. }
  1549. }
  1550.  
  1551. if (empty($_POST["comment"])) {
  1552. $comment = "";
  1553. } else {
  1554. $comment = test_input($_POST["comment"]);
  1555. }
  1556.  
  1557. if (empty($_POST["gender"])) {
  1558. $genderErr = "Gender is required";
  1559. $test1 =0;
  1560.  
  1561. } else {
  1562. $gender = test_input($_POST["gender"]);
  1563. }
  1564. }
  1565.  
  1566. if(isset($_POST["submit"]) && $test1 == 1){
  1567.  
  1568. $sql2 = "insert into registration(`name`,`email`,`website`) values('$name','$email','$website')";
  1569. $go = 0;
  1570. if($conn->query($sql2)){
  1571. $go=1;
  1572. }
  1573. else {
  1574.  
  1575. echo "Some fucking error occured! ".$conn->error;
  1576. }
  1577.  
  1578.  
  1579. if($go == 1){
  1580. setcookie("Name",$name,time()+60*5);
  1581. $_SESSION["name"] = $name;
  1582. header("Location: newPage.php");
  1583. }
  1584. }
  1585.  
  1586. function test_input($data) {
  1587. $data = trim($data);
  1588. $data = stripslashes($data);
  1589. $data = htmlspecialchars($data);
  1590. return $data;
  1591. }
  1592. ?>
  1593.  
  1594. <h2>PHP Form Validation Example</h2>
  1595. <p><span class="error">* required field.</span></p>
  1596. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  1597. Name: <input type="text" name="name" value="<?php echo $name;?>">
  1598. <span class="error">* <?php echo $nameErr;?></span>
  1599. <br><br>
  1600. E-mail: <input type="text" name="email" value="<?php echo $email;?>">
  1601. <span class="error">* <?php echo $emailErr;?></span>
  1602. <br><br>
  1603. Website: <input type="text" name="website" value="<?php echo $website;?>">
  1604. <span class="error"><?php echo $websiteErr;?></span>
  1605. <br><br>
  1606. Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
  1607. <br><br>
  1608. <input type="text" id="check" value = "hey" placeholder="Enter your age">
  1609. <p id="result">Hello</p>
  1610. <br><input type="button" value= "Verify" onclick="checker()">
  1611. <br><br>
  1612. Gender:
  1613. <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
  1614. <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
  1615. <span class="error">* <?php echo $genderErr;?></span>
  1616. <br><br>
  1617. <input type="submit" name="submit" value="Submit">
  1618. </form>
  1619.  
  1620. <?php
  1621. echo "<h2>Your Input:</h2>";
  1622. echo $name;
  1623. echo "<br>";
  1624. echo $email;
  1625. echo "<br>";
  1626. echo $website;
  1627. echo "<br>";
  1628. echo $comment;
  1629. echo "<br>";
  1630. echo $gender;
  1631. ?>
  1632.  
  1633. </body>
  1634. <script>
  1635. function checker(){
  1636. var reg = "^[a-z]{3,}$";
  1637. document.getElementById("result").innerHTML= "Not Matched";
  1638. var str = document.getElementById("check").value;
  1639. if(str.match(reg)){
  1640. document.getElementById("result").innerHTML= "matched";
  1641. }
  1642.  
  1643. /*
  1644. if(data.match(reg)){
  1645. res.innerHTML = "Matched";
  1646. }
  1647. else {
  1648. res.innerHTML = "Not matched";
  1649. }
  1650. */
  1651. }
  1652. </script>
  1653.  
  1654. </html>
  1655. <p onmousedown="myFunction(this,'red')" onmouseup="myFunction(this,'green')">
  1656. 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.
  1657. </p>
  1658.  
  1659. </body>
  1660. </html>
  1661.  
  1662.  
  1663.  
  1664. ONMOUSEDOWN
  1665.  
  1666.  
  1667.  
  1668.  
  1669. <!DOCTYPE html>
  1670. <html>
  1671. <head>
  1672. <script>
  1673. function whichElement(e) {
  1674. var targ;
  1675. if (!e) {
  1676. var e = window.event;
  1677. }
  1678. if (e.target) {
  1679. targ=e.target;
  1680. } else if (e.srcElement) {
  1681. targ=e.srcElement;
  1682. }
  1683. var tname;
  1684. tname = targ.tagName;
  1685. alert("You clicked on a " + tname + " element.");
  1686. }
  1687. </script>
  1688. </head>
  1689. <body onmousedown="whichElement(event)">
  1690.  
  1691. <p>Click somewhere in the document. An alert box will alert the name of the element you clicked on.</p>
  1692. <h3>This is a heading</h3>
  1693. <img border="0" src="smiley.gif" alt="Smiley" width="32" height="32">
  1694. <p>This is a paragraph.</p>
  1695.  
  1696. </body>
  1697. </html>
  1698.  
  1699. ONLOAD
  1700.  
  1701.  
  1702.  
  1703. <!DOCTYPE html>
  1704. <html>
  1705. <head>
  1706. <script>
  1707. function myFunction() {
  1708. alert("Page is loaded");
  1709. }
  1710. </script>
  1711. </head>
  1712.  
  1713. <body onload="myFunction()">
  1714. <h2>Hello World!</h2>
  1715. </body>
  1716.  
  1717. </html>
  1718. ---------------------------------------------------------------------------------
  1719.  
  1720. CLOCK
  1721.  
  1722. <!DOCTYPE html>
  1723. <html>
  1724. <head>
  1725. <script>
  1726. function startTime() {
  1727. var today = new Date();
  1728. var h = today.getHours();
  1729. var m = today.getMinutes();
  1730. var s = today.getSeconds();
  1731. m = checkTime(m);
  1732. s = checkTime(s);
  1733. document.getElementById('txt').innerHTML =
  1734. h + ":" + m + ":" + s;
  1735. var t = setTimeout(startTime, 500);
  1736. }
  1737. function checkTime(i) {
  1738. if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
  1739. return i;
  1740. }
  1741. </script>
  1742. </head>
  1743.  
  1744. <body onload="startTime()">
  1745.  
  1746. <div id="txt"></div>
  1747.  
  1748. </body>
  1749. </html>
  1750.  
  1751.  
  1752. ------------------------------------------
  1753.  
  1754. CALCULATOR
  1755.  
  1756.  
  1757. <html>
  1758. <head></head>
  1759. <body>
  1760. <h3>Simple Calculator</h3>
  1761. <br/>
  1762. <style>
  1763. #calc{width:300px;height:250px;}
  1764. #btn{width:100%;height:40px;font-size:20px;}
  1765. </style>
  1766. <form Name="calc">
  1767. <table id="calc" border=2>
  1768. <tr>
  1769. <td colspan=5><input id="btn" name="display" onkeypress="return event.charCode >= 48 && event.charCode <= 57" type="text"></td>
  1770. <td style="display:none"><input name="M" type="number"></td>
  1771. </tr>
  1772. <tr>
  1773. <td><input id="btn" type=button value="MC" OnClick="calc.M.value=''"></td>
  1774. <td><input id="btn" type=button value="0" OnClick="calc.display.value+='0'"></td>
  1775. <td><input id="btn" type=button value="1" OnClick="calc.display.value+='1'"></td>
  1776. <td><input id="btn" type=button value="2" OnClick="calc.display.value+='2'"></td>
  1777. <td><input id="btn" type=button value="+" OnClick="calc.display.value+='+'"></td>
  1778. </tr>
  1779. <tr>
  1780. <td><input id="btn" type=button value="MS" OnClick="calc.M.value=calc.display.value"></td>
  1781. <td><input id="btn" type=button value="3" OnClick="calc.display.value+='3'"></td>
  1782. <td><input id="btn" type=button value="4" OnClick="calc.display.value+='4'"></td>
  1783. <td><input id="btn" type=button value="5" OnClick="calc.display.value+='5'"></td>
  1784. <td><input id="btn" type=button value="-" OnClick="calc.display.value+='-'"></td>
  1785. </tr>
  1786. <tr>
  1787. <td><input id="btn" type=button value="MR" OnClick="calc.display.value=calc.M.value"></td>
  1788. <td><input id="btn" type=button value="6" OnClick="calc.display.value+='6'"></td>
  1789. <td><input id="btn" type=button value="7" OnClick="calc.display.value+='7'"></td>
  1790. <td><input id="btn" type=button value="8" OnClick="calc.display.value+='8'"></td>
  1791. <td><input id="btn" type=button value="x" OnClick="calc.display.value+='*'"></td>
  1792. </tr>
  1793. <tr>
  1794. <td><input id="btn" type=button value="M+" OnClick="calc.M.value=(Number(calc.M.value))+(Number(calc.display.value))"></td>
  1795. <td><input id="btn" type=button value="9" OnClick="calc.display.value+='9'"></td>
  1796. <td><input id="btn" type=button value="±"
  1797.  
  1798. OnClick="calc.display.value=(calc.display.value==Math.abs(calc.display.value)?-(calc.display.value):Math.abs(calc.display.value))">
  1799.  
  1800. </td>
  1801. <td><input id="btn" type=button value="=" OnClick="calc.display.value=eval(calc.display.value)"></td>
  1802. <td><input id="btn" type=button value="/" OnClick="calc.display.value+='/'"></td>
  1803. </tr>
  1804. <tr>
  1805. <td><input id="btn" type=button value="1/x" OnClick="calc.display.value=1/calc.display.value"></td>
  1806. <td><input id="btn" type=button value="." OnClick="calc.display.value+='.'"></td>
  1807. <td><input id="btn" type=button value="x2" OnClick="calc.display.value=Math.pow(calc.display.value,2)"></td>
  1808. <td><input id="btn" type=button value="√" OnClick="calc.display.value=Math.sqrt(calc.display.value)"></td>
  1809. <td><input id="btn" type=button value="C" OnClick="calc.display.value=''"></td>
  1810. </tr>
  1811. </table>
  1812. </form>
  1813. </body>
  1814. </html>
  1815.  
  1816.  
  1817.  
  1818. PHP BY NISHANT
  1819.  
  1820. <!DOCTYPE HTML>
  1821. <html>
  1822. <head>
  1823. <style>
  1824. .error {color: #FF0000;}
  1825. </style>
  1826. </head>
  1827. <body>
  1828.  
  1829. <?php
  1830. session_start();
  1831. $servername = "localhost";
  1832. $username = "root";
  1833. $password = "";
  1834. $db="Login details";
  1835.  
  1836. // Create connection
  1837. $conn = new mysqli($servername, $username, $password,$db);
  1838.  
  1839. // Check connection
  1840. if ($conn->connect_error) {
  1841. die("Connection failed: " . $conn->connect_error);
  1842. }
  1843. $sql1 = "Select 1 from registration";
  1844. $res = $conn->query($sql1);
  1845. if($res == false){
  1846. echo "Connected successfully";
  1847. $sql="create table registration (Slno int(3) AUTO_INCREMENT PRIMARY KEY, name varchar(30),
  1848. email varchar(30), website varchar(30));";
  1849. if($conn->query($sql)==TRUE)
  1850. {
  1851. echo "table Myguest created";
  1852. }
  1853. else
  1854. echo"Error creating table:".$conn->error;
  1855. }
  1856. else {
  1857. echo "The table is already created";
  1858. }
  1859. // define variables and set to empty values
  1860. $test1 = 1;
  1861. $nameErr = $emailErr = $genderErr = $websiteErr = "";
  1862. $name = $email = $gender = $comment = $website = "";
  1863.  
  1864. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  1865. if (empty($_POST["name"])) {
  1866. $nameErr = "Name is required";
  1867. $test1 =0;
  1868. } else {
  1869. $name = test_input($_POST["name"]);
  1870. // check if name only contains letters and whitespace
  1871. if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  1872. $nameErr = "Only letters and white space allowed";
  1873. $test1 =0;
  1874.  
  1875. }
  1876. }
  1877.  
  1878. if (empty($_POST["email"])) {
  1879. $emailErr = "Email is required";
  1880. $test1 =0;
  1881.  
  1882. } else {
  1883. $email = test_input($_POST["email"]);
  1884. // check if e-mail address is well-formed
  1885. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  1886. $emailErr = "Invalid email format";
  1887. $test1 =0;
  1888.  
  1889. }
  1890. }
  1891.  
  1892. if (empty($_POST["website"])) {
  1893. $website = "";
  1894. } else {
  1895. $website = test_input($_POST["website"]);
  1896. // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
  1897. if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
  1898. $websiteErr = "Invalid URL";
  1899. $test1 =0;
  1900.  
  1901. }
  1902. }
  1903.  
  1904. if (empty($_POST["comment"])) {
  1905. $comment = "";
  1906. } else {
  1907. $comment = test_input($_POST["comment"]);
  1908. }
  1909.  
  1910. if (empty($_POST["gender"])) {
  1911. $genderErr = "Gender is required";
  1912. $test1 =0;
  1913.  
  1914. } else {
  1915. $gender = test_input($_POST["gender"]);
  1916. }
  1917. }
  1918.  
  1919. if(isset($_POST["submit"]) && $test1 == 1){
  1920.  
  1921. $sql2 = "insert into registration(`name`,`email`,`website`) values('$name','$email','$website')";
  1922. $go = 0;
  1923. if($conn->query($sql2)){
  1924. $go=1;
  1925. }
  1926. else {
  1927.  
  1928. echo "Some fucking error occured! ".$conn->error;
  1929. }
  1930.  
  1931.  
  1932. if($go == 1){
  1933. setcookie("Name",$name,time()+60*5);
  1934. $_SESSION["name"] = $name;
  1935. header("Location: newPage.php");
  1936. }
  1937. }
  1938.  
  1939. function test_input($data) {
  1940. $data = trim($data);
  1941. $data = stripslashes($data);
  1942. $data = htmlspecialchars($data);
  1943. return $data;
  1944. }
  1945. ?>
  1946.  
  1947. <h2>PHP Form Validation Example</h2>
  1948. <p><span class="error">* required field.</span></p>
  1949. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  1950. Name: <input type="text" name="name" value="<?php echo $name;?>">
  1951. <span class="error">* <?php echo $nameErr;?></span>
  1952. <br><br>
  1953. E-mail: <input type="text" name="email" value="<?php echo $email;?>">
  1954. <span class="error">* <?php echo $emailErr;?></span>
  1955. <br><br>
  1956. Website: <input type="text" name="website" value="<?php echo $website;?>">
  1957. <span class="error"><?php echo $websiteErr;?></span>
  1958. <br><br>
  1959. Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
  1960. <br><br>
  1961. <input type="text" id="check" value = "hey" placeholder="Enter your age">
  1962. <p id="result">Hello</p>
  1963. <br><input type="button" value= "Verify" onclick="checker()">
  1964. <br><br>
  1965. Gender:
  1966. <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
  1967. <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
  1968. <span class="error">* <?php echo $genderErr;?></span>
  1969. <br><br>
  1970. <input type="submit" name="submit" value="Submit">
  1971. </form>
  1972.  
  1973. <?php
  1974. echo "<h2>Your Input:</h2>";
  1975. echo $name;
  1976. echo "<br>";
  1977. echo $email;
  1978. echo "<br>";
  1979. echo $website;
  1980. echo "<br>";
  1981. echo $comment;
  1982. echo "<br>";
  1983. echo $gender;
  1984. ?>
  1985.  
  1986. </body>
  1987. <script>
  1988. function checker(){
  1989. var reg = "^[a-z]{3,}$";
  1990. document.getElementById("result").innerHTML= "Not Matched";
  1991. var str = document.getElementById("check").value;
  1992. if(str.match(reg)){
  1993. document.getElementById("result").innerHTML= "matched";
  1994. }
  1995.  
  1996. /*
  1997. if(data.match(reg)){
  1998. res.innerHTML = "Matched";
  1999. }
  2000. else {
  2001. res.innerHTML = "Not matched";
  2002. }
  2003. */
  2004. }
  2005. </script>
  2006.  
  2007. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement