Home > Mobile >  Put get parameters into post request
Put get parameters into post request

Time:12-20

I am submitting a form via an ajax post request but there is a possibility that there is also one get parameter and if the get parameter is set it must be included in the post request. I currently have this:

jQuery(document).ready(function($) {
   $("#plugin_check").submit(function(e) {
    e.preventDefault(); // avoid to execute the actual submit of the form.

    var form = $(this);
    var actionUrl = form.attr('action');
    
    $.ajax({
        type: "POST",
        cache: false,
        url: actionUrl,
        data: form.serialize(), // serializes the form's elements.
        success: function(data)
        {
          $("#result").html(data); // show response from the php script.
        }
    });
  });  
});

I need to figure out how to put $_GET['datesort'] into the post data.

CodePudding user response:

you can try this, create variable to save datesort and add this to ajax data

jQuery(document).ready(function($) {
  $("#plugin_check").submit(function(e) {
    e.preventDefault(); // avoid to execute the actual submit of the form.

    var form = $(this);
    var actionUrl = form.attr('action');
    var queryString = window.location.search;
    var urlParams = new URLSearchParams(queryString);
    var datesort = urlParams.get('datesort')

    $.ajax({
      type: "POST",
      cache: false,
      url: actionUrl,
      data: form.serialize()   `&datesort=${datesort}`, // serializes the form's elements.
      success: function(data) {
        $("#result").html(data); // show response from the php script.
      }
    });
  });
});
  • Related