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?

6
  • Just append hidden input with that name (SELECTED_ID) into your form
    – hindmost
    Jul 22, 2015 at 11:16
  • You can only achieve that with JavaScript or by redirecting on the server side.
    – Quentin
    Jul 22, 2015 at 11:16
  • I cannot use hidden input as I don't know what value will be selected by the user. I have no problem using JavaScript here but how can I append something to the URL dynamically?
    – mmvsbg
    Jul 22, 2015 at 11:17
  • I think you should be able to add a selection listener to your options and use JavaScript to modify the form's action value. Don't have time to test it, though.
    – Marvin
    Jul 22, 2015 at 11:22
  • Is this a normal submit or with ajax? Jul 22, 2015 at 11:25

2 Answers 2

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());
    }
2
  • 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, 2015 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! Oct 4, 2017 at 19:33
1

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

3
  • As noted in the question, I'm required to use POST on submission.
    – mmvsbg
    Jul 22, 2015 at 11:23
  • using method = "GET" you cannot store values in server side! Jul 22, 2015 at 11:23
  • you can use javascript to achieve this, for example the select outside the form, and onchange() append to the url, and the rest of the form to be used as POST normally Jul 22, 2015 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.