Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FILES in PHP:
- form.html:
- ------------------------------------------------------------------------------
- <!DOCTYPE html>
- <!--
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
- -->
- <html>
- <head>
- <title>TODO supply a title</title>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <script>
- function validate()
- {
- var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
- var passformat = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/;
- var nameformat = /^[A-Za-z]+$/;
- var name = document.forms["form1"]["nam"].value;
- var email = document.forms["form1"]["mail"].value;
- if(email.match(mailformat))
- {
- if(name.match(nameformat))
- {
- return true;
- }
- else
- {
- alert("You have entered an invalid Name");
- return false;
- }
- }
- else
- {
- alert("You have entered an invalid mail format");
- return false;
- }
- }
- </script>
- </head>
- <body>
- <div style = "background: yellow; position: absolute; left: 40%;">
- <h2 style=" text-align: center;">The World of Fruit</h2>
- <div style = "background: lightyellow; text-align: center;">
- <p style=" text-align: center;">Fruit Survey</p>
- <form name = "form1" action = "process.php" onsubmit ="return validate()" method="POST">
- <label>Name :</label>
- <input type="text" id = "nam" name="nam" required>
- <br>
- <label>Address :</label>
- <input type="text" id = "add" name="add" required>
- <br>
- <label>Email :</label>
- <input type="email" id = "mail" name="mail" required>
- <br>
- <label>How many Pieces of fruit do you eat per day?</label><br>
- <input type="radio" id = "fru" name="fru" value ="0" required><span>0</span>
- <input type="radio" id = "fru" name="fru" value ="1" required><span>1</span>
- <input type="radio" id = "fru" name="fru" value ="2" required><span>2</span>
- <input type="radio" id = "fru" name="fru" value ="More than 3" required><span>More than 2</span><br>
- <label>My favorite fruit: </label>
- <select id="fav" name="fav">
- <option value="Apple">Apple</option>
- <option value="Banana">Banana</option>
- <option value="Plum">Plum</option>
- <option value="Pomegranate">Pomegranate</option>
- </select>
- <br>
- <label>Would you like a brochure?</label>
- <input type ="checkbox" name="brochure" id = "brouchre">
- <br>
- <input type="submit">
- </form>
- </div>
- </div>
- </body>
- </html>
- -----------------------------------------------------------------------
- process.php
- -----------------------------------------------------------------------
- <?php
- $name = $_POST["nam"];
- $address =$_POST["add"];
- $mail = $_POST["mail"];
- $fru = $_POST["fru"];
- $fav = $_POST["fav"];
- $bro = $_POST["brochure"];
- $str = $name.','.$address.','.$mail.','.$fru.','.$fav.','.$bro[0]."\n";
- $file1 = fopen("registration.txt", "a");
- fwrite($file1, $str);
- echo "<table>";
- fclose($file1);
- $file1 = fopen("registration.txt", "rb");
- while(($row = fgets($file1)) != false)
- {
- echo "<tr>";
- $col = explode(',',$row);
- foreach($col as $data)
- {
- echo "<td>". trim($data)."</td>";
- }
- echo "</tr>";
- }
- echo "</table>";
- fclose($file1);
- ?>
- ---------------------------------------------------------------------------------
- MySQL with PHP:
- ----------------------------------------------------------------------------------
- <?php
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "test";
- $con = mysqli_connect($servername,$username,$password,$dbname);
- $query = "CREATE TABLE IF NOT EXISTS info(
- name varchar(25),
- pass varchar(25),
- email varchar(25),
- gender varchar(10)
- /*intr varchar(15),
- terms varchar(10)*/
- );";
- if(mysqli_query($con,$query))
- {
- $nam = $_POST['nam'];
- $pass = $_POST['pas'];
- $mail = $_POST['mail'];
- $sex = $_POST['sex'];/*
- $intr = $_POST['intr'];
- $agree = $_POST['agree'];*/
- $append = "INSERT INTO info (name,pass,email,gender) values('$nam','$pass','$mail','$sex');";
- if(mysqli_query($con,$append))
- {
- echo "<table><th><td>Name</td><td>Password</td><td>Email</td><td>Sex</td></th>";
- $result = mysqli_query($con,"select * from info;") or die("Select Query failed".mysqli_error($con));
- if (mysqli_num_rows($result) > 0)
- {
- while($row = mysqli_fetch_assoc($result))
- {
- echo "<tr><td>".$row["name"]."</td><td>".$row["pass"]."</td><td>".$row["email"]."</td><td>".$row["gender"]."</td>/tr>";
- }
- echo "</table>";
- }
- else
- {
- echo "</table><br><br><p>THERE ARE NO ENTRIES</p>";
- }
- }
- else
- {
- echo "Insertion Failed";
- }
- }
- else
- {
- echo "Table not created.";
- }
- ?>
- ------------------------------------------------------------------------
- Servlet code:
- -------------------------------------------------------------------------
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- Connection conn = null;
- try
- {
- String user = request.getParameter("user");
- String pwd = request.getParameter("pass");
- String userName = "root";
- String password = "";
- String url = "jdbc:mysql://localhost:3306/test";
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- conn = DriverManager.getConnection(url, userName, password);
- Statement s = conn.createStatement();
- int i=s.executeUpdate("insert into users values ('"+user+"','"+pwd+"')");
- s.executeQuery("SELECT * FROM users");
- ResultSet rs = s.getResultSet();
- while (rs.next())
- {
- String username=rs.getString("user");
- String pass=rs.getString("pwd");
- out.println("Username: "+username+" Password: "+pass+"<hr>");
- }
- rs.close();
- s.close();
- } catch (Exception e)
- {
- out.println(e.toString());
- }
- finally
- {
- out.close();
- if (conn != null) {
- try {
- conn.close();
- out.println("Database connection terminated");
- } catch (Exception e) {
- }
- }
- }
- ---------------------------------------------------------------------------------------
- JSP code:
- ---------------------------------------------------------------------------------------
- <%@ page import ="java.sql.*" %>
- <%@ page import ="javax.sql.*" %>
- <%
- String user=request.getParameter("userid");
- String pwd=request.getParameter("pwd");
- Class.forName("com.mysql.jdbc.Driver");
- java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","","");
- Statement st= con.createStatement();
- ResultSet rs;
- //int i=st.executeUpdate("insert into users values ('"+user+"','"+pwd+"')");
- rs=st.executeQuery("select * from users");
- while(rs.next()){
- String username=rs.getString("user");
- String password=rs.getString("pwd");
- out.println("Username "+username+" Pass "+password+"<hr>");
- }
- %>
Add Comment
Please, Sign In to add comment