Search code examples
phpslimidiorm

From view to model (Slim PHP framework / Idiorm / Paris)


The issue:

I have a timesheet application. It has an SQlite database. I am trying to find a way to present a GUI so that if the user clicks a square(a ) i need to paas the data from the template to the model (paris) so i can save it in theb SQlite database.

There are three tables one for users one for the timesheet and one for the department.

It is a timesheet like application.

The setup:

Slim php Idiorm/Paris SQlite3

Does anyone know a good way to make the user click a so that the data is passed on to the model from the view?

Thank you in advance!


Solution

  • Imagine this is your view

    <html> 
       <head>
       $(document).ready(function() {
                $("#add").on("click", function(e) {
                    var user_id = $('#user_id').val();
                    var time = date();
                    var department = $('#department').val();
                    var data = {uid:user_id, time:time, dept:department};
                    $.ajax({
                        url: "add_into_db.php",
                        type: 'POST',
                        data: data,
                        success: function(msg) {
                           if(msg=="true"){
                                alert("Your dats is inserted successfully");
                                document.location.reload(true);
                            }
                            else{
                                 alert("Your data insertion failed");
                                  return false;
                            }
                        }
                    });
                    e.preventDefault();      
                });
            });
       </head>
    
       <body>
         // Some html here
         // input field for user
         // input field for time
         // input field for department
         // whatever data you want os send on click, include it here
         <input id ='add' type= 'submit' value 'Add'/> //Your submit button to add data via AJAX
       </body>
    

    Yout php function which will add data into database

    function add_into_db(){
      $user id = $_POST['user_id'];
      $time = $_POST['time'];
      $dept = $_POST['department'];
    
      // connect to your db
      // run your insert query
    
      If (insertion is successfull) {
         $msg = 'true';
         echo $msg;
      }
      else{
         $msg = 'false';
        echo $msg
      }
    }
    

    Hope it ill help you