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;$i<2;$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?