Send both POST and GET in a form

前端 未结 5 1419
-上瘾入骨i
-上瘾入骨i 2020-12-15 10:08

I need to make a form send both POST and GET requests (due to some bugs in IE and iframes), how can you do so?

The data being sent is nothing mega secure so it doesn

相关标签:
5条回答
  • 2020-12-15 10:38

    If you need a dynamicaly created URL. You can use this HTML example. The GET fields are in a seprated Form. Before submit of the POST Form the URL is generated from the GET Form.

    <form id="formGET">
        email: <input name="email" value="email@domain.nl"/>
    </form>
    <form id="formPOST" method="post" onsubmit="this.action='/api/Account?'+Array.prototype.slice.call(formGET.elements).map(function(val){return val.name + '=' + val.value}).join('&');">
        mobile: <input name="mobile" value="9999999999" /><br />
        <button>POST</button>
    </form>
    
    0 讨论(0)
  • 2020-12-15 10:39

    Not sure what bug you're trying to get around but you can use jQuery to easily modify the form's action to contain the posted values:

    script:

    function setAction() {
        $("#myform").attr("action", "/path/to/script/?" + $("#myform").serialize());
    }
    

    html:

    <form id="myform" action="/path/to/script/" method="post" onsubmit="setAction()">
    
    0 讨论(0)
  • 2020-12-15 10:42

    Make the form do a usual POST and use JavaScript to replicate the values in the query string as well:

    HTML:

    <form id="myform" method="post" action="..." onsubmit="process()">
      ...
    </form>
    

    JavaScript:

    function process() {
      var form = document.getElementById('myform');
      var elements = form.elements;
      var values = [];
    
      for (var i = 0; i < elements.length; i++)
        values.push(encodeURIComponent(elements[i].name) + '=' + encodeURIComponent(elements[i].value));
    
      form.action += '?' + values.join('&');
    }
    
    0 讨论(0)
  • 2020-12-15 10:45

    the form should set post do the get in the url

    <form method="post" action="http://www.yourpage.php?firstparam=1&sec=2">
    .
    .
    </form>
    
    0 讨论(0)
  • 2020-12-15 10:59

    Easy: Just specify the GET data in the form URL.

    <form method="POST" action="form.php?a=1&b=2&c=3">
    

    however, very carefully check how the data is used in the receiving script. Don't use $_REQUEST- rather parse $_GET and $_POST according to your exact needs and in the priority order you need them.

    0 讨论(0)
提交回复
热议问题