Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. <div class="row">
  2.  
  3. <form method="post" id="add-lecture-form" class="col-xs-6 ajax-form" action="Controller/CourseController.php">
  4. <div class="form-group">
  5. <input type="text" name="title" class="form-control">
  6. </div>
  7. <input type="hidden" id="action" name="action" value="create">
  8. <div class="form-group">
  9. <input type="submit" id="submit" class="btn btn-primary" value="add a lecture">
  10. </div>
  11. </form>
  12.  
  13. <div class="col-xs-6">
  14.  
  15.  
  16. <div id="lecture-result">
  17. </div>
  18.  
  19.  
  20. </div>
  21.  
  22. </div>
  23.  
  24.  
  25.  
  26. ---JQUERY-
  27.  
  28. $(document).ready(function () {
  29.  
  30.  
  31. $('.ajax-form').on('submit', function (e) {
  32. e.preventDefault();
  33. var postData = $(this).serialize();
  34. var url = $(this).attr('action');
  35.  
  36. $.ajax({
  37.  
  38. url: url,
  39. data: postData,
  40. type: $(this).val('method'),
  41. success: function (data) {
  42. if (data.first_name)
  43. {
  44. var html = '<ul>';
  45. html += '<li>' + data.title + '</li></ul>';
  46.  
  47. $('#result').append(html);
  48.  
  49. }
  50.  
  51.  
  52. if (!data.error) {
  53.  
  54. $('#result').html(data);
  55.  
  56. }
  57. });
  58. });
  59.  
  60. --PHP---
  61.  
  62. $action = $_POST['action'];
  63.  
  64.  
  65. if ($action == 'create') {
  66. createCourse($_POST['title']);
  67. getCourseListAsView();
  68. }
  69. if ($action == 'getList') {
  70. getCourseListAsView();
  71. }
  72.  
  73.  
  74. /*
  75. * Function Create Course
  76. * This will create a new Course object and persist it to the database.
  77. * */
  78.  
  79. function createCourse($title) {
  80. /* Create new Course Entity */
  81. $course = new Course;
  82. /* Fill course with data */
  83. $course->setTitle($title);
  84.  
  85. /* Persost Object to Database */
  86. $dbConfigObject = new DbConfig;
  87. $dbConnection = $dbConfigObject->getDBConnection();
  88.  
  89. $query = "INSERT INTO courses(title) VALUES('$title')";
  90. $query_lecture_name = mysqli_query($dbConnection, $query);
  91.  
  92. if (!$query_lecture_name) {
  93. die('QUERY FAILED');
  94. }
  95. }
  96.  
  97. function getCourseListAsView() {
  98.  
  99. $dbConfigObject = new DbConfig;
  100. $dbConnection = $dbConfigObject->getDBConnection();
  101.  
  102. $query = "SELECT * FROM courses";
  103. $search_query = mysqli_query($dbConnection, $query);
  104.  
  105. $listview = "<ul class='list-unstyled'>";
  106. while ($row = mysqli_fetch_array($search_query)) {
  107. //title column in db
  108. $listview .= "<li>" . $row['title'] . "</li>";
  109. }
  110. $listview .= "</ul>";
  111.  
  112. echo $listview;
  113. }
  114.  
  115. die();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement