2

I have a simple form in a PHP application that I have to submit via POST method. Something like:

<form action="URL?page_id=10" method="POST">
    <select name="some_name" id="some_id">
        <option value='1'>...</option>
        <option value='2'>...</option>
        ...
    </select>
    ...
    //submit button here
</form>

The goal is to go to the following URL on submit:

URL?page_id=10&selected_id=SELECTED_ID

where SELECTED_ID is the value chosen by the user from the select drop down menu in the form. I've done it by converting the whole form to post the parameters as GET as I need to have this SELECTED_ID visible in the URL. However, another requirement turned up saying that I need to pass everything through POST and still have the SELECTED_ID visible in the URL and now I'm looking for alternatives.

So the question gets down to: how can I add dynamically another GET parameter to the URL upon POST form submission with one of the values submitted with the form?

mmvsbg
  • 3,570
  • 17
  • 52
  • 73

2 Answers2

1

first you have to add a id to your form as below

<form id='form1' action="URL?page_id=10" method="POST">

then add call below function on your button click

 function test(){
    $('#form1').attr('action', $(this).attr('formaction')+'&selected_id='+$('#some_id').val());
    }
joker
  • 982
  • 9
  • 23
  • I had to change the "$(this)" with "$('#form1')" to make it work but it does the job as advertised. Thank you very much! – mmvsbg Jul 22 '15 at 12:16
  • I had to change the `attr('formaction')` to just `attr('action')` maybe this is something between version of jquery, but I got it to work. Thanks! – Preston Hager Oct 04 '17 at 19:33
1

use <form method="GET"> .. all the field values will be appended to the url automatically

Ehab Eldeeb
  • 722
  • 4
  • 12