Custom forms + Polylang

I am new to Wordpress but I have some knowledge of PHP. I am creating a bilingual site (with Polylang) which has many forms. Some of them are handled by Contact Form 7 but some others have to be custom made.

So, the first form I made was quite a disaster - it works but under certain circumstances.

In the archives page of a custom post I insert

form method="post" action="?php echo esc_url( home_url( '/' ) ); ?"
input type="hidden" name="post_type" value="myvalue"
select class="form-control"  name="k" id="k"
?php  
for($i=0;$i2;$i++){?
option value="?php echo $array3[$i]?"?php echo $array3[$i];?/option
?php }?   
/select
span class="input-group-btn"
button class="btn btn-search" type="submit"i class="fa fa-search"  /i/button
/span
/form 

I created a template file named mysearchresults.php In there I inserted the essential (between them)

 echo $_POST['k'];
 echo "Hello world";   

I created a page having as template mysearchresults.php in one language (named mysearchresults) and another in the other language (same template) named mysearchresults-2.

Loading the two pages gives a page with the Hello world printed.

Adding

form method="post" action="?php echo esc_url( home_url( '/' ) ); ?mysearchresults-2"

gives 404 (when I press the submit button - having made a selsction) - the same as instead of -2 I put mysearchresults. Putting mysearchresults.php gives the same error.

So I tried another approach, instead of

select class="form-control"  name="k" id="k" 

I put

select class="form-control"  name="s" id="s"

and for action I left

action="?php echo esc_url( home_url( '/' ) ); ?"  

In the search page I put echo $_POST['s'] and it loads the search results page printing the value of my selection.

What am I doing wrong? How can I have a custom form, pass the values in my action file and make operations on them? Why leaving like this the action label it loads the search results page?

Topic forms Wordpress

Category Web


You can also look at the polylang extension for contact form 7. It allows you to manage multiple forms in different languages using the familiar polylang translator management tools.


for the action label, there are some reserved terms, that wordpress uses internally. so you should avoid this terms (specially true in http_post ot get requests). 's' stands for search, that's why the output is a search result page. here you can find reserved terms: https://codex.wordpress.org/Reserved_Terms

If you need very simple forms (so it seems in this case), i would suggest you to learn how to append variables to an url with add_query_arg. Here's a good place to start, it's quite simple and ready to use : https://developer.wordpress.org/reference/functions/add_query_arg/

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.