-1

I have a simple form and when the select field is changed it submits the form reloading the current page passing the select value to the page.

<form>
  <select id='query' name='query' onchange='this.form.submit()'>
    <option value=1>1<option>
    <option value=2>2<option>
  </select>
</form>

This works fine, but the URL then contains pagename.php?query=2 Is there anyway to do this without the parameter and value being seen in the URL ?

Thanks

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Tom
  • 1,436
  • 24
  • 50
  • 4
    Add a `method="post"` attribute to the `form`. This will send the form data in the body of the request instead of the URL. Note that you will need to update how you receive the values, though. – Rory McCrossan Mar 10 '22 at 09:14
  • Adding `method="post"` hasn't helped, sorry I should have mentioned I had tried that. The form submits, the page reloads and the URL contains the param and value – Tom Mar 10 '22 at 09:48
  • 1
    @AlexScott — https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit — That's native DOM not a custom function. – Quentin Mar 10 '22 at 10:02
  • @AlexScott — Well, yes. The value of the `onchange` attribute is a user provided function … which is entirely shown in the question and which calls the native DOM `submit` method on the form element. – Quentin Mar 10 '22 at 10:04
  • @Tom: This smells of being an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). While there are ways to submit form data without showing it in the URL, that is more of a side effect of what those techniques are intended for. Most people asking how to hide the data in the URL are either trying to do so for aesthetic reasons (which should take a back seat to the more practical aspects of the different methods) or for security reasons (in which case moving the data out of the URL won't create any security benefits). – Quentin Mar 10 '22 at 10:07
  • @RoryMcCrossan it helps if I add `method="post"` to the form, NOT the select ! If you would like to add this as an answer I will accept it. – Tom Mar 10 '22 at 10:16

1 Answers1

1

When you use form, it has a paramatter that if you dont put nothing, it is METHOD="GET", this makes the petition and sends data from URL, so you should put METHOD="POST":

<form method="post">
  <select id='query' name='query' onchange='this.form.submit()'>
    <option value=1>1<option>
    <option value=2>2<option>
  </select>
</form>

You will have to make some more changes in your php, but i don't have enought information about it to help you.

Maida
  • 235
  • 1
  • 4
  • 12