How to use a form input to filter a DataTable

How to use a form input to filter a DataTable

schwaluckschwaluck Posts: 103Questions: 27Answers: 1

Hello everyone,

I use the Codeigniter Framework for my web project, which is based on the MVC (Model, View, Controller) pattern. Now I have the following problem where I can't get any further:

On one page a user should be able to fill out a form. After submitting the form, an entry is created in the database and the user is forwarded to another page with a datatable. This works fine so far. But now a certain value from the submitted form should be used to filter the content of this datatable.

Example:
The user enters a license plate (e.g. "S 1234") in the form and submits the form. He is then forwarded to the next page where he sees the datatable filtered for the license plate "S 1234".

My question now is how I can use the value from the form (here: "S 1234") to filter the DataTable. I already tried to save the value in a variable and pass it to the model to include it in a where clause, but that didn't quite work (code follows at the end).

Does anyone have an idea why this doesn't work that way or if there is a better way to filter the table based on the form input?

Controller-Code excerpt:

$car_plate;
$GLOBALS['car_plate'] = $this->request->getPost('car_plate');
public function ajax_add_edit(){ $this->Check_In_Dus_Schaeden_Model->getDatatablesRows($this->request->getPost(), $GLOBALS['car_plate']); }

Modell-Code excerpt:
public function getDatatablesRows($post, $car_plate_local) {
Editor::inst( $this->editorDb, $this->table, 'id' ) ->fields(
Field::inst( 'car_plate' )
//Other Fields...
)
->where( function ( $q ) use ( $car_plate_local ) { $q->where( 'kennzeichen', $car_plate_local ); } ) ->process( $post ) ->json();

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    ...but that didn't quite work

    Why not? What happens? Do you see error messages? What debugging have you done?

  • schwaluckschwaluck Posts: 103Questions: 27Answers: 1

    I get the following error messages:
    1. DataTables warning: table id=table_test - Ajax error. For more information about this error, please see http://datatables.net/tn/7
    2. XHR Internal server error while trying to run the controller method ajax_add_edit

    Does this help you?

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    Answer ✓

    Diagnostic steps are explained in the link provided in the error message.

  • schwaluckschwaluck Posts: 103Questions: 27Answers: 1

    I already checked the server logs, since it's a 500 error.
    Server logs the following:

    [29/Aug/2020:18:09:21 +0200] "POST /Test_Controller/ajax_add_edit HTTP/2.0" 500 122 "https://url.com/test" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0"

    Does this help you out? Maybe I am missing something.

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923

    You need to look into the server logs to see why its responding with a 500 Internal Server error.

    Kevin

  • schwaluckschwaluck Posts: 103Questions: 27Answers: 1

    Okay I have solved the problem. I just used a SESSION variable instead of a global variable. However thanks for your support @tangerine. :)

This discussion has been closed.