Advertisement
Guest User

JAVASCRIPT

a guest
May 3rd, 2017
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.17 KB | None | 0 0
  1. javascript events
  2.  
  3. ONBLUR EVENT
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <script>
  8. function myFunction() {
  9. var x = document.getElementById("fname");
  10. x.value = x.value.toUpperCase();
  11. }
  12. </script>
  13. </head>
  14. <body>
  15.  
  16. Enter your name: <input type="text" id="fname" onblur="myFunction()">
  17.  
  18. <p>When you leave the input field, a function is triggered which transforms the input text to upper case.</p>
  19.  
  20. </body>
  21. </html>
  22.  
  23. ON MOUSE OVER
  24.  
  25.  
  26.  
  27. <!DOCTYPE html>
  28. <html>
  29. <body>
  30.  
  31. <h1 onmouseover="style.color='red'" onmouseout="style.color='black'">Mouse over this text</h1>
  32.  
  33. </body>
  34. </html>
  35. ONMOUSEUP
  36.  
  37.  
  38. <!DOCTYPE html>
  39. <html>
  40. <head>
  41. <script>
  42. function myFunction(elmnt, clr) {
  43. elmnt.style.color = clr;
  44. }
  45. </script>
  46. </head>
  47. <body>
  48.  
  49. <p onmousedown="myFunction(this,'red')" onmouseup="myFunction(this,'green')">
  50. 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.
  51. </p>
  52.  
  53. </body>
  54. </html>
  55.  
  56.  
  57.  
  58. ONMOUSEDOWN
  59.  
  60.  
  61.  
  62.  
  63. <!DOCTYPE html>
  64. <html>
  65. <head>
  66. <script>
  67. function whichElement(e) {
  68. var targ;
  69. if (!e) {
  70. var e = window.event;
  71. }
  72. if (e.target) {
  73. targ=e.target;
  74. } else if (e.srcElement) {
  75. targ=e.srcElement;
  76. }
  77. var tname;
  78. tname = targ.tagName;
  79. alert("You clicked on a " + tname + " element.");
  80. }
  81. </script>
  82. </head>
  83. <body onmousedown="whichElement(event)">
  84.  
  85. <p>Click somewhere in the document. An alert box will alert the name of the element you clicked on.</p>
  86. <h3>This is a heading</h3>
  87. <img border="0" src="smiley.gif" alt="Smiley" width="32" height="32">
  88. <p>This is a paragraph.</p>
  89.  
  90. </body>
  91. </html>
  92.  
  93. ONLOAD
  94.  
  95.  
  96.  
  97. <!DOCTYPE html>
  98. <html>
  99. <head>
  100. <script>
  101. function myFunction() {
  102. alert("Page is loaded");
  103. }
  104. </script>
  105. </head>
  106.  
  107. <body onload="myFunction()">
  108. <h2>Hello World!</h2>
  109. </body>
  110.  
  111. </html>
  112. ---------------------------------------------------------------------------------
  113.  
  114. CLOCK
  115.  
  116. <!DOCTYPE html>
  117. <html>
  118. <head>
  119. <script>
  120. function startTime() {
  121. var today = new Date();
  122. var h = today.getHours();
  123. var m = today.getMinutes();
  124. var s = today.getSeconds();
  125. m = checkTime(m);
  126. s = checkTime(s);
  127. document.getElementById('txt').innerHTML =
  128. h + ":" + m + ":" + s;
  129. var t = setTimeout(startTime, 500);
  130. }
  131. function checkTime(i) {
  132. if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
  133. return i;
  134. }
  135. </script>
  136. </head>
  137.  
  138. <body onload="startTime()">
  139.  
  140. <div id="txt"></div>
  141.  
  142. </body>
  143. </html>
  144.  
  145.  
  146. ------------------------------------------
  147.  
  148. CALCULATOR
  149.  
  150.  
  151. <html>
  152. <head></head>
  153. <body>
  154. <h3>Simple Calculator</h3>
  155. <br/>
  156. <style>
  157. #calc{width:300px;height:250px;}
  158. #btn{width:100%;height:40px;font-size:20px;}
  159. </style>
  160. <form Name="calc">
  161. <table id="calc" border=2>
  162. <tr>
  163. <td colspan=5><input id="btn" name="display" onkeypress="return event.charCode >= 48 && event.charCode <= 57" type="text"></td>
  164. <td style="display:none"><input name="M" type="number"></td>
  165. </tr>
  166. <tr>
  167. <td><input id="btn" type=button value="MC" OnClick="calc.M.value=''"></td>
  168. <td><input id="btn" type=button value="0" OnClick="calc.display.value+='0'"></td>
  169. <td><input id="btn" type=button value="1" OnClick="calc.display.value+='1'"></td>
  170. <td><input id="btn" type=button value="2" OnClick="calc.display.value+='2'"></td>
  171. <td><input id="btn" type=button value="+" OnClick="calc.display.value+='+'"></td>
  172. </tr>
  173. <tr>
  174. <td><input id="btn" type=button value="MS" OnClick="calc.M.value=calc.display.value"></td>
  175. <td><input id="btn" type=button value="3" OnClick="calc.display.value+='3'"></td>
  176. <td><input id="btn" type=button value="4" OnClick="calc.display.value+='4'"></td>
  177. <td><input id="btn" type=button value="5" OnClick="calc.display.value+='5'"></td>
  178. <td><input id="btn" type=button value="-" OnClick="calc.display.value+='-'"></td>
  179. </tr>
  180. <tr>
  181. <td><input id="btn" type=button value="MR" OnClick="calc.display.value=calc.M.value"></td>
  182. <td><input id="btn" type=button value="6" OnClick="calc.display.value+='6'"></td>
  183. <td><input id="btn" type=button value="7" OnClick="calc.display.value+='7'"></td>
  184. <td><input id="btn" type=button value="8" OnClick="calc.display.value+='8'"></td>
  185. <td><input id="btn" type=button value="x" OnClick="calc.display.value+='*'"></td>
  186. </tr>
  187. <tr>
  188. <td><input id="btn" type=button value="M+" OnClick="calc.M.value=(Number(calc.M.value))+(Number(calc.display.value))"></td>
  189. <td><input id="btn" type=button value="9" OnClick="calc.display.value+='9'"></td>
  190. <td><input id="btn" type=button value="±"
  191.  
  192. OnClick="calc.display.value=(calc.display.value==Math.abs(calc.display.value)?-(calc.display.value):Math.abs(calc.display.value))">
  193.  
  194. </td>
  195. <td><input id="btn" type=button value="=" OnClick="calc.display.value=eval(calc.display.value)"></td>
  196. <td><input id="btn" type=button value="/" OnClick="calc.display.value+='/'"></td>
  197. </tr>
  198. <tr>
  199. <td><input id="btn" type=button value="1/x" OnClick="calc.display.value=1/calc.display.value"></td>
  200. <td><input id="btn" type=button value="." OnClick="calc.display.value+='.'"></td>
  201. <td><input id="btn" type=button value="x2" OnClick="calc.display.value=Math.pow(calc.display.value,2)"></td>
  202. <td><input id="btn" type=button value="√" OnClick="calc.display.value=Math.sqrt(calc.display.value)"></td>
  203. <td><input id="btn" type=button value="C" OnClick="calc.display.value=''"></td>
  204. </tr>
  205. </table>
  206. </form>
  207. </body>
  208. </html>
  209.  
  210.  
  211.  
  212. PHP BY NISHANT
  213.  
  214. <!DOCTYPE HTML>
  215. <html>
  216. <head>
  217. <style>
  218. .error {color: #FF0000;}
  219. </style>
  220. </head>
  221. <body>
  222.  
  223. <?php
  224. session_start();
  225. $servername = "localhost";
  226. $username = "root";
  227. $password = "";
  228. $db="Login details";
  229.  
  230. // Create connection
  231. $conn = new mysqli($servername, $username, $password,$db);
  232.  
  233. // Check connection
  234. if ($conn->connect_error) {
  235. die("Connection failed: " . $conn->connect_error);
  236. }
  237. $sql1 = "Select 1 from registration";
  238. $res = $conn->query($sql1);
  239. if($res == false){
  240. echo "Connected successfully";
  241. $sql="create table registration (Slno int(3) AUTO_INCREMENT PRIMARY KEY, name varchar(30),
  242. email varchar(30), website varchar(30));";
  243. if($conn->query($sql)==TRUE)
  244. {
  245. echo "table Myguest created";
  246. }
  247. else
  248. echo"Error creating table:".$conn->error;
  249. }
  250. else {
  251. echo "The table is already created";
  252. }
  253. // define variables and set to empty values
  254. $test1 = 1;
  255. $nameErr = $emailErr = $genderErr = $websiteErr = "";
  256. $name = $email = $gender = $comment = $website = "";
  257.  
  258. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  259. if (empty($_POST["name"])) {
  260. $nameErr = "Name is required";
  261. $test1 =0;
  262. } else {
  263. $name = test_input($_POST["name"]);
  264. // check if name only contains letters and whitespace
  265. if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  266. $nameErr = "Only letters and white space allowed";
  267. $test1 =0;
  268.  
  269. }
  270. }
  271.  
  272. if (empty($_POST["email"])) {
  273. $emailErr = "Email is required";
  274. $test1 =0;
  275.  
  276. } else {
  277. $email = test_input($_POST["email"]);
  278. // check if e-mail address is well-formed
  279. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  280. $emailErr = "Invalid email format";
  281. $test1 =0;
  282.  
  283. }
  284. }
  285.  
  286. if (empty($_POST["website"])) {
  287. $website = "";
  288. } else {
  289. $website = test_input($_POST["website"]);
  290. // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
  291. if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
  292. $websiteErr = "Invalid URL";
  293. $test1 =0;
  294.  
  295. }
  296. }
  297.  
  298. if (empty($_POST["comment"])) {
  299. $comment = "";
  300. } else {
  301. $comment = test_input($_POST["comment"]);
  302. }
  303.  
  304. if (empty($_POST["gender"])) {
  305. $genderErr = "Gender is required";
  306. $test1 =0;
  307.  
  308. } else {
  309. $gender = test_input($_POST["gender"]);
  310. }
  311. }
  312.  
  313. if(isset($_POST["submit"]) && $test1 == 1){
  314.  
  315. $sql2 = "insert into registration(`name`,`email`,`website`) values('$name','$email','$website')";
  316. $go = 0;
  317. if($conn->query($sql2)){
  318. $go=1;
  319. }
  320. else {
  321.  
  322. echo "Some fucking error occured! ".$conn->error;
  323. }
  324.  
  325.  
  326. if($go == 1){
  327. setcookie("Name",$name,time()+60*5);
  328. $_SESSION["name"] = $name;
  329. header("Location: newPage.php");
  330. }
  331. }
  332.  
  333. function test_input($data) {
  334. $data = trim($data);
  335. $data = stripslashes($data);
  336. $data = htmlspecialchars($data);
  337. return $data;
  338. }
  339. ?>
  340.  
  341. <h2>PHP Form Validation Example</h2>
  342. <p><span class="error">* required field.</span></p>
  343. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  344. Name: <input type="text" name="name" value="<?php echo $name;?>">
  345. <span class="error">* <?php echo $nameErr;?></span>
  346. <br><br>
  347. E-mail: <input type="text" name="email" value="<?php echo $email;?>">
  348. <span class="error">* <?php echo $emailErr;?></span>
  349. <br><br>
  350. Website: <input type="text" name="website" value="<?php echo $website;?>">
  351. <span class="error"><?php echo $websiteErr;?></span>
  352. <br><br>
  353. Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
  354. <br><br>
  355. <input type="text" id="check" value = "hey" placeholder="Enter your age">
  356. <p id="result">Hello</p>
  357. <br><input type="button" value= "Verify" onclick="checker()">
  358. <br><br>
  359. Gender:
  360. <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
  361. <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
  362. <span class="error">* <?php echo $genderErr;?></span>
  363. <br><br>
  364. <input type="submit" name="submit" value="Submit">
  365. </form>
  366.  
  367. <?php
  368. echo "<h2>Your Input:</h2>";
  369. echo $name;
  370. echo "<br>";
  371. echo $email;
  372. echo "<br>";
  373. echo $website;
  374. echo "<br>";
  375. echo $comment;
  376. echo "<br>";
  377. echo $gender;
  378. ?>
  379.  
  380. </body>
  381. <script>
  382. function checker(){
  383. var reg = "^[a-z]{3,}$";
  384. document.getElementById("result").innerHTML= "Not Matched";
  385. var str = document.getElementById("check").value;
  386. if(str.match(reg)){
  387. document.getElementById("result").innerHTML= "matched";
  388. }
  389.  
  390. /*
  391. if(data.match(reg)){
  392. res.innerHTML = "Matched";
  393. }
  394. else {
  395. res.innerHTML = "Not matched";
  396. }
  397. */
  398. }
  399. </script>
  400.  
  401. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement