Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Put get parameters into post request

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.
      }
    });
  });
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading