user756659
user756659

Reputation: 3512

passing data to jquery modal aside from adding parameters to url?

On click of a #dtDelete button I am opening an ajax modal in bootstrap 3. I am also passing a parameter, selected, along with it and using $_GET on the php page to retrieve it.

As the value of selected may or may not be extremely large I think I should avoid passing the value this way / using $_GET.

How the heck can I pass values other than this method? Due to the nature of opening the modal (loading it then showing it) I am stuck on any other ways.

$('#dtDelete').on('click', function () {
    //open the modal with selected parameter attached
    $('#modal-ajax').load('/modals/m_keystroke_delete.php?selected='+selected);
    $('#modal-ajax').modal('show');                         
});

Upvotes: 0

Views: 1746

Answers (1)

Stefan
Stefan

Reputation: 5672

Pass a data object as a second param to load for it to make a POST request.

$('#dtDelete').on('click', function () {

    var data = { 'propertyA': 'extremely large data' };

    //open the modal with selected parameter attached
    $('#modal-ajax').load(
      '/modals/m_keystroke_delete.php?selected=' + selected, // url
      data,                                                  // data
      function(responseText, textStatus, XMLHttpRequest) { } // complete callback
    );

    $('#modal-ajax').modal('show');                         
});

You can even pass your "selected" param through the POST request and use $_POST or even $_REQUEST to retrieve the data. Also notice that the modal now shows once the request has completed.

$('#dtDelete').on('click', function () {

    var data = {
      'selected': selected
      'largeData': '...'
    };

    $('#modal-ajax').load(
      '/modals/m_keystroke_delete.php',
      data,
      function() {

        // Invoke the delete-function
        deleteComp();

        // Show the modal
        $(this).modal('show');
      }
    );
});

Upvotes: 1

Related Questions