Dynamically adding GET parameter to URL on form submit

折月煮酒 提交于 2021-02-08 05:56:08

问题


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?


回答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:


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



来源:https://stackoverflow.com/questions/31561495/dynamically-adding-get-parameter-to-url-on-form-submit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!