Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use form button to submit GET value

Tags:

html

forms

php

I have the following code to submit GET values:

<form method="GET">
 <input type="hidden" name="price" value="ASC" />
  <input type="submit" value="Price Low to High" />
</form>

<form method="GET">
 <input type="hidden" name="price" value="DESC" />
  <input type="submit" value="Price High to Low" />
</form>

<form method="GET">
 <input type="hidden" name="price" value="" />
  <input type="submit" value="Default" />
</form>

Which is working fine. So I am not repeating myself with three forms, is there a way I could have a single form, and define what GET values are submitted by the buttons? They would still need to have the same information visible to the user e.g. the button that submits DESC needs to display as Price High to Low

like image 866
Mike Harrison Avatar asked Oct 28 '25 16:10

Mike Harrison


2 Answers

use this code:

<form method="GET">
  <input type="submit"  name="price" value="ASC">Price Low to High</input>
  <input type="submit" name="price" value="DESC"  >Price High to Low</input>

 <input type="submit" name="price" value="" >Default</input>
</form>
like image 72
Aref Anafgeh Avatar answered Oct 30 '25 08:10

Aref Anafgeh


If having three buttons is not obligatory by design, then I suggest, why not just use a select to let the user decide what he wants (as most of the sites already do)

<form method="GET">
 <select id="price" name="price">
   <option value="">Default</option>
   <option value="ASC">Price Low to High</option>
   <option value="DESC">Price High to Low</option>
 </select>
 <input type="submit" value="Price Low to High" />
</form>

Demo

And if having three buttons is necessary then, you could do either one of (though not limited to) the following..

  1. Use plain links (<a> anchor tags), yup you read that correct, since the form is just submitting using the $_GET method, eventually the data is gonna be inside the URLs, so why not just have it there in the first place using the href attributes of the "buttons" ?

  2. Use the solution that @Aref Anafgeh suggested, and have three submit buttons each with different values and just let the html handle which value is sent to the server.

  3. Use JavaScript and make ajax calls, which pretty much allows you to handle what is & isn't sent in the request.

like image 38
Mohd Abdul Mujib Avatar answered Oct 30 '25 07:10

Mohd Abdul Mujib