Download Source Code -PHP – Store Form Data With Modal Using PDO

Loading...

PHP – Store Form Data With Modal Using PDO

Submitted by: 
razormist
Language: 
PHP
Visitors have accessed this post 5 times.

In this tutorial we will create a Store Form Data With Modal using PDO. This code can store a form data to the database server using PDO when submitting. The code use PDO INSERT query to store the form inputs to database server that secure data process to prevent injection tools. This is a free program feel free to modify it or use it to your application.

We will be using PDO as a query scripting it an acronym for PHP Data Objects. It is a lean, clean, and consistent way to access databases. This means developers can write portable code much easier.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here’s the link for XAMPP server https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_pdo_store, after that click Import then locate the database file inside the folder of the application then click ok.
tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

  1. <?php
  2. $conn=new PDO( 'mysql:host=localhost;dbname=db_pdo_store', 'root', '');
  3. if(!$conn){
  4. die("Error: Failed to connect to database!");
  5. }
  6. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1">
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="cotaniner-fluid">
  10. <a class="navbar-brand">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Store Form Data With Modal Using PDO</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-2"></div>
  18. <div class="col-md-8">
  19.  
  20. <button class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add user</button>
  21. <br /><br />
  22. <table class="table table-bordered">
  23. <thead class="alert-info">
  24. <tr>
  25. <th>Firstname</th>
  26. <th>Lastname</th>
  27. <th>Age</th>
  28. </tr>
  29. </thead>
  30. <tbody>
  31. <?php
  32. require_once 'conn.php';
  33.  
  34. $sql = "SELECT * FROM `user`";
  35. $query = $conn->prepare($sql);
  36. $query->execute();
  37.  
  38. while($fetch = $query->fetch()){
  39. ?>
  40.  
  41. <tr>
  42. <td><?php echo $fetch['firstname']?></td>
  43. <td><?php echo $fetch['lastname']?></td>
  44. <td><?php echo $fetch['age']?></td>
  45. </tr>
  46.  
  47. <?php
  48. }
  49. ?>
  50. </tbody>
  51. </table>
  52. </div>
  53. </div>
  54.  
  55. <div class="modal fade" id="form_modal" aria-hidden="true">
  56. <div class="modal-dialog">
  57. <div class="modal-content">
  58. <form method="POST" action="save_user.php">
  59. <div class="modal-header">
  60. <h3 class="modal-title">Add User</h3>
  61. </div>
  62. <div class="modal-body">
  63. <div class="col-md-2"></div>
  64. <div class="col-md-8">
  65. <div class="form-group">
  66. <label>Firstname</label>
  67. <input type="text" name="firstname" class="form-control" required="required"/>
  68. </div>
  69. <div class="form-group">
  70. <label>Lastname</label>
  71. <input type="text" name="lastname" class="form-control" required="required" />
  72. </div>
  73. <div class="form-group">
  74. <label>Age</label>
  75. <input type="number" name="age" class="form-control" min="0" max="200" required="required" />
  76. </div>
  77. </div>
  78. </div>
  79. <div style="clear:both;"></div>
  80. <div class="modal-footer">
  81. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  82. <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  83. </div>
  84. </div>
  85. </form>
  86. </div>
  87. </div>
  88. </div>
  89.  
  90.  
  91. <script src="http://www.sourcecodester.com/js/jquery-3.2.1.min.js"></script>
  92. <script src="http://www.sourcecodester.com/js/bootstrap.js"></script>
  93. </body>
  94. </html>

Creating the Main Function

This code contains the main function of the application. This code will store the form data to the database server when the button is clicked. To do this just kindly write these block of codes inside the text editor, then save it as save_user.php.

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5.  
  6. $firstname = $_POST['firstname'];
  7. $lastname = $_POST['lastname'];
  8. $age = $_POST['age'];
  9.  
  10.  
  11. try{
  12. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  13. $sql = "INSERT INTO `user`(firstname, lastname, age) VALUES ('$firstname', '$lastname', '$age')";
  14. $conn->exec($sql);
  15. }catch(PDOException $e){
  16. echo $e->getMessage();
  17. }
  18.  
  19. $conn = null;
  20.  
  21. header("location: index.php");
  22.  
  23. }
  24. ?>

There you have it we successfully created a Store Form Data With Modal using PDO. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Tags: 
php
Bootstrap
jQuery
PDO

Add new comment

(Visited 1 times, 1 visits today)
Loading...