Search code examples
phphtmlxmlhttprequestpostmanhttp-post

How to assign value1 to value2 in post request?


How do you assign value1 to value2 in post request?

For example:

www.site.com/index.php?value1=10&value2=value1&value3=value2+1

How do you make value2 = value1 in the URL?


Solution

  • URL is just raw-text, so you need to either:

    • Use JavaScript to get what value1 is currently, then set value2 to be same, with jQuery it's something like:
    $("#myFormId").submit(function(event) {
      event.preventDefault();
      var $form = $(this);
    
      // Finds URL using form's action attribute.
      var url = $form.attr('action');
    
      // You may change this part.
      var value1 = $('#myFirstField').val();
      var value2 = value1;
      var data = {
        value1: value1,
        value2: value2,
      };
    
      // Sends the request.
      var listener = $.post(url, data);
      // Handles the response.
      listener.done(function(data) {
        console.log('success', data);
      });
      listener.fail(function() {
        console.log('failed');
      });
    });
    
    • Or, simply remove value2 from URL, if it's always equal to value1, then use PHP to do set value2 parameter (something like $value2 = $value1; or whatever your API to change URL-param needs).