fawzan
fawzan

Reputation: 214

bootstrap-table : passing parameter to $.ajax()

I am following the example https://examples.bootstrap-table.com/index.html#options/table-ajax.html#view-source

<table
  id="table"
  data-toggle="table"
  data-height="460"
  data-ajax="ajaxRequest"
  data-search="true"
  data-side-pagination="server"
  data-pagination="true">
</table>

I would need to pass a parameter in data-ajax="ajaxRequest" let say switch = 1 to my ajaxRequest script below:

function ajaxRequest(params) {        
        $.ajax({
            type: "post",
            url: "process.php",
           dataType: 'json',
data:{
//I want to pass *switch* variable here to process.php
//how?
} 
           success: function(items) {
                params.success({
                    rows: items

                }, null);
            },
            error: function(er) {
                console.log(params.error(er))
                console.log("error", er);
            }
        })
    }

Please help.

-Edit---- 2021/09/29 Thanks @Eitanmg, I read that doc.

So sorry if my question was not clear.

So, from view.php :

<table
  id="table"
  data-toggle="table"
  data-height="460"
  data-ajax="ajaxRequest"
  data-query-params="queryParams"
  data-search="true"
  data-side-pagination="server"
  data-pagination="true">
</table>

I want to pass switch = 42 to ajaxRequest so then later in ajaxRequest the data:{ "switch": params.data.switch } is processed in process.php which contains:

if (switch == 42){ 
    task1()
 }else if (switch == 42){
    task2()
 }else { and so on}

So the "42" is sent from the view.php dinamically. and switch variable will process various function on process.php based on the value.

I am looking for advice to send switch parameter from php file to ajaxRequest.

Upvotes: 4

Views: 2440

Answers (2)

fawzan
fawzan

Reputation: 214

Thanks @eitanmg

I modified as follow

in the view,

<input type=hidden id=switch value=42>  //     <<<<--- add this
<table
  id="table"
  data-toggle="table"
  data-height="460"
  data-ajax="ajaxRequest"
  data-query-params="queryParams"
  data-search="true"
  data-side-pagination="server"
  data-pagination="true">
</table>

and catch the variable

  function queryParams(params) {
    params.switch = document.getElementById(switch)  //   <<<----------catch like this
    return params
  }

function ajaxRequest(params) {        
        $.ajax({
            type: "post",
            url: "process.php",
           dataType: 'json',
           data:{ "switch": params.data.switch },
           success: function(items) {
                params.success({
                    rows: items

                }, null);
            },
            error: function(er) {
                console.log(params.error(er))
                console.log("error", er);
            }
        })
    }

Then process it in process.php

if ($_POST('switch')==42) {}

However, should you have any smarter method than I coded above please welcome.

Upvotes: 0

Eitanmg
Eitanmg

Reputation: 586

You should add data-query-params="queryParams" to you table as:

<table
  id="table"
  data-toggle="table"
  data-height="460"
  data-ajax="ajaxRequest"
  data-query-params="queryParams"
  data-search="true"
  data-side-pagination="server"
  data-pagination="true">
</table>

and add new function with switch querystring parameter

  function queryParams(params) {
    params.switch = 42
    return params
  }

and in you ajaxRequest function you should access it as:

function ajaxRequest(params) {        
        $.ajax({
            type: "post",
            url: "process.php",
           dataType: 'json',
           data:{ "switch": params.data.switch },
           success: function(items) {
                params.success({
                    rows: items

                }, null);
            },
            error: function(er) {
                console.log(params.error(er))
                console.log("error", er);
            }
        })
    }

Upvotes: 1

Related Questions