There is a form

<form role="form" id="form"> <input id="log_2" type="text" name="test1" value="<?php $a; ?> "/> <input type="text" name="test2" value="<?php $b;?>" /> <button type="submit"> <a href="/2-catalog?q=Цена-₴-<?php echo $a;?> -<?php echo $b;?>">Подобрать </button> </form> 

How to correctly declare variables so that when values ​​are entered into an input, the values ​​are saved into a variable and then displayed in a link?

    3 answers 3

    The scheme is approximately the same.
    1. We show the empty form to the user.

     <form role="form" id="form" action="/2-catalog" method="get"> <input id="min-price" type="text" name="min-price" value="" /> <input id="max-price" type="text" name="max-price" value="" /> <button type="submit"></button> </form> 

    action - the address where the form handler is located

    1. The user presses the button

    2. We receive the entered data after sending the form

    $ a = $ _GET ['min-price'];
    $ b = $ _GET ['max-price'];
    // do something with the received data

    PS A little bit for clarification. Php code works on the server, after we sent the page to the browser to the user, the php code is no longer there. Then the user entered the data, clicked the button and we received the entered data on the server.

    • Thank you all, of course, but I write / 2-catalog? q = Price- ₴ - <? php% 20echo% 20 $ _GET [% 27test1% 27];% 20?> - <? php% 20echo% 20 $ _GET [% 27test2% 27];?> And I would like / 2-catalog? Q = Price- ₴ -100-200 - Kir
    • If you replace it with the above code, then when sending the form it will be / 2-catalog? Min-price = 100 & max-price = 200 - Oleg Plotnikov
    • You mix server and client code together, so it’s not what you’ve expected. - Oleg Plotnikov

    Added - method="get" . Something like this.

     <form role="form" id="form" method="get"> <input type="text" name="test1" value=""> <input type="text" name="test2" value=""> <button type="submit"> <a href="/2-catalog?q=Цена-₴-<?php echo $_GET['test1']; ?>-<?php echo $_GET['test2'];?>">Подобрать </button> </form> 

    PS The value of the method attribute is case insensitive. There are two methods - get and post .

    get This method is one of the most common and is designed to get the required information and transfer data in the address bar. In this case, the “name = value” pairs are joined to the address after the question mark and are separated by an ampersand (the & symbol). The convenience of using the get method is that the address with all parameters can be used repeatedly, saving it, for example, in the browser bookmarks, and also changing the parameter values ​​directly in the address bar.

    post The post method sends data to the server in a browser request. This allows you to send more data than the get method, since it has a limit of 4 KB. Large amounts of data are used in forums, postal services, filling the database, when sending files, etc.

    • Thank you all, of course, but I write / 2-catalog? q = Price- ₴ - <? php% 20echo% 20 $ _GET [% 27test1% 27];% 20?> - <? php% 20echo% 20 $ _GET [% 27test2% 27];?> And I would like / 2-catalog? Q = Price- ₴ -100-200 - Kir
    • @Kir, then try using JS. To remove the value from the field in input , then in the JS attribute href give the link you need. - user242433

    They are not necessary to declare using php. You first need to specify a method in the form Post or Get. In the form you must specify, for example: name = "value1", and then, so that you can get this value, write $ _GET ['value1'] or Post.

     <form role="form" id="form" method="get"> 
    • Thank you all, of course, but I write / 2-catalog? q = Price- ₴ - <? php% 20echo% 20 $ _GET [% 27test1% 27];% 20?> - <? php% 20echo% 20 $ _GET [% 27test2% 27];?> And I would like / 2-catalog? Q = Price- ₴ -100-200 - Kir