Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <div class="row">
- <form method="post" id="add-lecture-form" class="col-xs-6 ajax-form" action="Controller/CourseController.php">
- <div class="form-group">
- <input type="text" name="title" class="form-control">
- </div>
- <input type="hidden" id="action" name="action" value="create">
- <div class="form-group">
- <input type="submit" id="submit" class="btn btn-primary" value="add a lecture">
- </div>
- </form>
- <div class="col-xs-6">
- <div id="lecture-result">
- </div>
- </div>
- </div>
- ---JQUERY-
- $(document).ready(function () {
- $('.ajax-form').on('submit', function (e) {
- e.preventDefault();
- var postData = $(this).serialize();
- var url = $(this).attr('action');
- $.ajax({
- url: url,
- data: postData,
- type: $(this).val('method'),
- success: function (data) {
- if (data.first_name)
- {
- var html = '<ul>';
- html += '<li>' + data.title + '</li></ul>';
- $('#result').append(html);
- }
- if (!data.error) {
- $('#result').html(data);
- }
- });
- });
- --PHP---
- $action = $_POST['action'];
- if ($action == 'create') {
- createCourse($_POST['title']);
- getCourseListAsView();
- }
- if ($action == 'getList') {
- getCourseListAsView();
- }
- /*
- * Function Create Course
- * This will create a new Course object and persist it to the database.
- * */
- function createCourse($title) {
- /* Create new Course Entity */
- $course = new Course;
- /* Fill course with data */
- $course->setTitle($title);
- /* Persost Object to Database */
- $dbConfigObject = new DbConfig;
- $dbConnection = $dbConfigObject->getDBConnection();
- $query = "INSERT INTO courses(title) VALUES('$title')";
- $query_lecture_name = mysqli_query($dbConnection, $query);
- if (!$query_lecture_name) {
- die('QUERY FAILED');
- }
- }
- function getCourseListAsView() {
- $dbConfigObject = new DbConfig;
- $dbConnection = $dbConfigObject->getDBConnection();
- $query = "SELECT * FROM courses";
- $search_query = mysqli_query($dbConnection, $query);
- $listview = "<ul class='list-unstyled'>";
- while ($row = mysqli_fetch_array($search_query)) {
- //title column in db
- $listview .= "<li>" . $row['title'] . "</li>";
- }
- $listview .= "</ul>";
- echo $listview;
- }
- die();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement