Guest User

Untitled

a guest
Nov 1st, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.09 KB | None | 0 0
  1. FILES in PHP:
  2.  
  3. form.html:
  4. ------------------------------------------------------------------------------
  5. <!DOCTYPE html>
  6. <!--
  7. To change this license header, choose License Headers in Project Properties.
  8. To change this template file, choose Tools | Templates
  9. and open the template in the editor.
  10. -->
  11. <html>
  12. <head>
  13. <title>TODO supply a title</title>
  14. <meta charset="UTF-8">
  15. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  16. <script>
  17. function validate()
  18. {
  19. var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
  20. var passformat = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/;
  21. var nameformat = /^[A-Za-z]+$/;
  22. var name = document.forms["form1"]["nam"].value;
  23. var email = document.forms["form1"]["mail"].value;
  24. if(email.match(mailformat))
  25. {
  26. if(name.match(nameformat))
  27. {
  28. return true;
  29. }
  30. else
  31. {
  32. alert("You have entered an invalid Name");
  33. return false;
  34. }
  35. }
  36. else
  37. {
  38. alert("You have entered an invalid mail format");
  39. return false;
  40. }
  41. }
  42. </script>
  43. </head>
  44. <body>
  45. <div style = "background: yellow; position: absolute; left: 40%;">
  46. <h2 style=" text-align: center;">The World of Fruit</h2>
  47. <div style = "background: lightyellow; text-align: center;">
  48. <p style=" text-align: center;">Fruit Survey</p>
  49. <form name = "form1" action = "process.php" onsubmit ="return validate()" method="POST">
  50. <label>Name :</label>
  51. <input type="text" id = "nam" name="nam" required>
  52. <br>
  53. <label>Address :</label>
  54. <input type="text" id = "add" name="add" required>
  55. <br>
  56. <label>Email :</label>
  57. <input type="email" id = "mail" name="mail" required>
  58. <br>
  59. <label>How many Pieces of fruit do you eat per day?</label><br>
  60. <input type="radio" id = "fru" name="fru" value ="0" required><span>0</span>
  61. <input type="radio" id = "fru" name="fru" value ="1" required><span>1</span>
  62. <input type="radio" id = "fru" name="fru" value ="2" required><span>2</span>
  63. <input type="radio" id = "fru" name="fru" value ="More than 3" required><span>More than 2</span><br>
  64. <label>My favorite fruit: </label>
  65. <select id="fav" name="fav">
  66. <option value="Apple">Apple</option>
  67. <option value="Banana">Banana</option>
  68. <option value="Plum">Plum</option>
  69. <option value="Pomegranate">Pomegranate</option>
  70. </select>
  71. <br>
  72. <label>Would you like a brochure?</label>
  73. <input type ="checkbox" name="brochure" id = "brouchre">
  74. <br>
  75. <input type="submit">
  76. </form>
  77. </div>
  78. </div>
  79. </body>
  80. </html>
  81. -----------------------------------------------------------------------
  82.  
  83. process.php
  84. -----------------------------------------------------------------------
  85. <?php
  86. $name = $_POST["nam"];
  87. $address =$_POST["add"];
  88. $mail = $_POST["mail"];
  89. $fru = $_POST["fru"];
  90. $fav = $_POST["fav"];
  91. $bro = $_POST["brochure"];
  92. $str = $name.','.$address.','.$mail.','.$fru.','.$fav.','.$bro[0]."\n";
  93.  
  94. $file1 = fopen("registration.txt", "a");
  95. fwrite($file1, $str);
  96. echo "<table>";
  97. fclose($file1);
  98. $file1 = fopen("registration.txt", "rb");
  99. while(($row = fgets($file1)) != false)
  100. {
  101. echo "<tr>";
  102. $col = explode(',',$row);
  103. foreach($col as $data)
  104. {
  105. echo "<td>". trim($data)."</td>";
  106. }
  107. echo "</tr>";
  108. }
  109. echo "</table>";
  110. fclose($file1);
  111. ?>
  112.  
  113. ---------------------------------------------------------------------------------
  114. MySQL with PHP:
  115. ----------------------------------------------------------------------------------
  116. <?php
  117. $servername = "localhost";
  118. $username = "root";
  119. $password = "";
  120. $dbname = "test";
  121. $con = mysqli_connect($servername,$username,$password,$dbname);
  122. $query = "CREATE TABLE IF NOT EXISTS info(
  123. name varchar(25),
  124. pass varchar(25),
  125. email varchar(25),
  126. gender varchar(10)
  127. /*intr varchar(15),
  128. terms varchar(10)*/
  129. );";
  130. if(mysqli_query($con,$query))
  131. {
  132. $nam = $_POST['nam'];
  133. $pass = $_POST['pas'];
  134. $mail = $_POST['mail'];
  135. $sex = $_POST['sex'];/*
  136. $intr = $_POST['intr'];
  137. $agree = $_POST['agree'];*/
  138. $append = "INSERT INTO info (name,pass,email,gender) values('$nam','$pass','$mail','$sex');";
  139. if(mysqli_query($con,$append))
  140. {
  141. echo "<table><th><td>Name</td><td>Password</td><td>Email</td><td>Sex</td></th>";
  142. $result = mysqli_query($con,"select * from info;") or die("Select Query failed".mysqli_error($con));
  143. if (mysqli_num_rows($result) > 0)
  144. {
  145. while($row = mysqli_fetch_assoc($result))
  146. {
  147. echo "<tr><td>".$row["name"]."</td><td>".$row["pass"]."</td><td>".$row["email"]."</td><td>".$row["gender"]."</td>/tr>";
  148. }
  149. echo "</table>";
  150. }
  151. else
  152. {
  153. echo "</table><br><br><p>THERE ARE NO ENTRIES</p>";
  154. }
  155. }
  156. else
  157. {
  158. echo "Insertion Failed";
  159. }
  160. }
  161. else
  162. {
  163. echo "Table not created.";
  164. }
  165. ?>
  166.  
  167. ------------------------------------------------------------------------
  168. Servlet code:
  169. -------------------------------------------------------------------------
  170. response.setContentType("text/html");
  171. PrintWriter out = response.getWriter();
  172. Connection conn = null;
  173.  
  174. try
  175. {
  176. String user = request.getParameter("user");
  177. String pwd = request.getParameter("pass");
  178. String userName = "root";
  179. String password = "";
  180. String url = "jdbc:mysql://localhost:3306/test";
  181. Class.forName("com.mysql.jdbc.Driver").newInstance();
  182. conn = DriverManager.getConnection(url, userName, password);
  183. Statement s = conn.createStatement();
  184. int i=s.executeUpdate("insert into users values ('"+user+"','"+pwd+"')");
  185. s.executeQuery("SELECT * FROM users");
  186. ResultSet rs = s.getResultSet();
  187. while (rs.next())
  188. {
  189. String username=rs.getString("user");
  190. String pass=rs.getString("pwd");
  191. out.println("Username: "+username+" Password: "+pass+"<hr>");
  192. }
  193. rs.close();
  194. s.close();
  195. } catch (Exception e)
  196. {
  197. out.println(e.toString());
  198. }
  199. finally
  200. {
  201. out.close();
  202. if (conn != null) {
  203. try {
  204. conn.close();
  205. out.println("Database connection terminated");
  206. } catch (Exception e) {
  207. }
  208. }
  209.  
  210. }
  211. ---------------------------------------------------------------------------------------
  212. JSP code:
  213. ---------------------------------------------------------------------------------------
  214. <%@ page import ="java.sql.*" %>
  215. <%@ page import ="javax.sql.*" %>
  216. <%
  217. String user=request.getParameter("userid");
  218.  
  219. String pwd=request.getParameter("pwd");
  220.  
  221. Class.forName("com.mysql.jdbc.Driver");
  222. java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","","");
  223. Statement st= con.createStatement();
  224. ResultSet rs;
  225. //int i=st.executeUpdate("insert into users values ('"+user+"','"+pwd+"')");
  226. rs=st.executeQuery("select * from users");
  227.  
  228. while(rs.next()){
  229. String username=rs.getString("user");
  230. String password=rs.getString("pwd");
  231. out.println("Username "+username+" Pass "+password+"<hr>");
  232. }
  233. %>
Add Comment
Please, Sign In to add comment