Form Action not working as expected for my search box

[ad_1]

I want to search post by specific keyword. first people write whatever they seek in the search box then the form must be sent to `get_search_result.php` file but nothing happens when I submit the form it doesn’t even print my echo messages. I tried action=’get\_search\_result.php’ nothing again

I will be very grateful if someone helps me I m lost right now

searchform.php

`<?php /**`
`* Template Name: Search Page`
`*/`
`?>`
`<form method=”post” action=”<?php echo get_site_url(); ?>/get_search_result.php”>`
`<input class=”search_box” type=”search” name=”search_query” placeholder=”Search something here”>`
`<!–<input class=”send-btn” type=”submit”>–>`
`</form>`

get\_search\_result.php

`<?php`

`$search_query = $_POST[“search_query”];`

`if (isset($search_query)) {`

`echo “The word is” . $search_query;`

`$the_query = new WP_Query(array(‘s’ => $search_query));`

`while ($the_query->have_posts()) {`

`echo “Posts found”;`

`echo $the_query->the_post();`

`echo get_the_title();`

`}`

`}`

&#x200B;

`else {`

`echo “Nothing to search”;`

`}`

[ad_2]
1 Comment
  1. Where is that file located? You can’t include a file like that in the root, since it won’t load the necessary WP libraries – you would needed to call `require( ‘../wp-load.php’ );` first. If it’s included in the theme, then you have not got the correct URL in the ‘action’ attribute.

    You will probably want to include get_header() and get_footer() in the template too.

    I’m sure there would be tutorials online on how to create a custom search form + template

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer