Search page does not show the search results in WordPress - php

I have a searchform.php file which contains the following code:
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<div><label class="screen-reader-text" for="s">Search for</label>
<input type="text" value="" name="s" id="s" />
<input type="submit" id="searchsumit" value="Search" />
</div>
Also in my index.php I've inserted this code:
<div class="search">
<?php get_search_form(); ?>
</div>
Now When I search for something I get no results. Therefore I added a search.php file to show the results in it but I still don't get the results. What kind of changes should be made? Or what piece of code is lacking?
Note that I want to show the results in a separate page which must be search.php in Wordpress.

You need to add search.php file inside the theme and in that you will need to add the view for the search result page and the result can be obtained by looping for e.g
while(have_posts() ) : the_post();
//here is your data
endwhile;

Related

Wordpress: Custom search result page throws 404

I have followed the link here in codex and created the following
This is my searchform.php
<form action="/search" method="get" role="search" id="searchform">
<div class="input-group">
<input type="text" name="s" id="search" class="form-control search-bar" placeholder="Search Healthkart Blog" value="<?php the_search_query(); ?>">
<div class="input-group-append button-icon">
<button class="btn" type="button" id="searchBtn">
<img src="<?php echo get_stylesheet_directory_uri(); ?>/assets/images/search-icon.png" alt="search" class="search-icon-white">
<img src="<?php echo get_stylesheet_directory_uri(); ?>/assets/images/search-icon-grey.png" alt="search" class="search-icon-grey">
</button>
</div>
</div>
</form>
I have included it in header.php as
get_search_form();
And the JS for submitting the form in this way
$("#searchBtn").click(function(){
$("#searchform").submit();
})
I have also created a custom search result page called search.php and it contains the normal search page code. Then I have created a page in wordpress pages called search (URL as /search) and assigned the custom search result template to it.
The issue is when I submit the form, it gets redirected to /search/?s=test123 and returns a 404 page. But if I hit the urls /search/test123, it works.
How do I make the search form submit to the URL /search/test123 ?
How do I make the search form submit to the URL /search/test123 ?
You can add :
$("#searchform").on('submit', function(e) {
e.preventDefault();
window.location = $("#searchform").attr('action') +'/'+ $("#searchform #search").val();
});
But with this solution, browser without JavaScript won't work.
To fix it, you can add the following to the search template:
if (isset($_GET['s']) && !empty($_GET['s'])) {
wp_redirect(get_permalink() . $_GET['s'], 301);
die();
}

WordPress Search Error: ?s= in URL

So I'm doing a few stuff for this humanitarian association and they asked me to add a search button on their navbar.
So far I came up with two solutions but none of them work. They both add "?s=" into the URL, thus redirecting to an empty URL.
Example: if you type "programme" in the search field, the URL will be: http://pisad.org/?s=programme. This is an empty URL and it should ultimately redirect to http://pisad.org/nos-programmes-inedits/ instead.
The first solution tried: search on navigation menu plugin:
https://wordpress.org/plugins/search-box-on-navigation-menu/
The second solution tried: adding this code to the navbar
<form class="navbar-form navbar-right search-form searchform" id="searchform" role="search" method="GET" action="<?php echo esc_url( home_url( '/' )); ?>" id="searchform">
<div class="form-group">
<input type="text" class="form-control" placeholder="Rechercher" name="s" id="s">
</div>
<button type="submit" class="btn btn-default">Rechercher</button>
</form>
Any idea why it's like that?

CodeIgniter URL repetition

I just started CodeIgnitor, that's the first time I use MVC structure though, and I have a problem that I've never seen before... It's mainly in the "form" part, but also in the database display.Also I use Xampp.
I've got a form to create an item to insert in the database, but whenever i click the submit button, things gets wrong in the url section.
My base URL is : localhost/CodeIgniter-3.1.1/ (CodeIgniter-3.1.1 is the directory that contain every php folder).
So the form page URL is : localhost/CodeIgniter-3.1.1/index.php/news/create
And when i submit, it is : localhost/CodeIgniter-3.1.1/index.php/news/localhost/CodeIgniter-3.1.1/index.php/news/create
It just repeat the entire URL after the controller (news).
I don't think it has to be with config.php, my base URL seems good, I just don't know.
Make your base url http://localhost/Codeigniter-3.1.1/index.php/ then in your <form> tag set the url like this <form method="post" action="<?= base_url('news/create') ?>">
In /application/config/config.php set $config['base_url'] like this
$config['base_url'] = http://localhost/Codeigniter-3.1.1/
In your view do either one of the following to create the <form> tag
<form method="post" action="<?= base_url('news/create'); ?>">
of if you have loaded the "Form Helper" (documented here) use this line in the view
<?php echo form_open('news/create'); ?>
It's handle by the framework, as so:
<h2><?php echo $title; ?></h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/create'); ?>
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" />
</form>
Also, the problem occure when I put a link to a view, like :
<a href="<?php echo 'news/'.$news_item['slug']; ?>">
Instead of building the right URL it copy itself along the bar.

how to use $_POST in wordpress

Hey i am trying to retrive data from a basic form .. but when i am using $_POST['field name'] then it gives me nothing .
here is my basic code
form page is:
<?php
/**
Template Name: galaxy
*/
get_header(); ?>
<div id="main-content" class="main-content">
<form action="<?php echo site_url();?>?page_id=8" method="post">
<input type="text" name="name" /> <input type="submit" value="Send" />
</form>
</div><!-- #main-content -->
<?php
get_footer();
when i click submit it redirects to next page but display nothing with this code
<?php
/**
Template Name: get_value_galaxy
*/
$name=$_POST['name'];
echo $name;
print_r($_POST);
?>
Try using a different name for the variable. I know that Wordpress uses "name" as a public query var, and perhaps that's why it's not working. So rather than using name="name", try this:
Form:
<input type="text" name="unique_name" />
Post Page:
$name=$_POST['unique_name'];
echo $name;
See this list for all query vars:
http://codex.wordpress.org/WordPress_Query_Vars#Query_variables

Creating a iframe search page for my site

It is very difficult for me to put in words my query. But I will try.
I have a site xyz.com which has search facility for listed products. The search page url is generated like this :www.wyz.com/search/search_term
I want to create a iframe page in a third party site with a search facility which can directly communicated with my site xyz.com.
I have tried to create a search box with a submit button. I want to append the search query in as a variable to my form action url string.
So the search string should look like this :www.wyz.com/search/my_string_variable
The code I have written is:
<?php
$url='http://www.xyz.com/search/';
?>
<?php
if (isset($_POST['submit']))
{
$r1=$_POST['num1'];
}
?>
<?php
$result=$url.$r1
?>
<html><body>
<form action="<?php echo $result; ?>" method="post">
Num1:<input name="num1"><br>
<input type="submit" name="submit">
</form>
</body></html>
==================================================================
But output what I get, is only "http://www.xyz.com/search/". It removes my variable from the url. I am not able to find what is the reason? I have also tried to print result via to check the actual output and it shows that it has added the value at the end of url. But when I want to achieve the same thing via form action it does not work. please help?
<?php
$url='http://www.xyz.com/search/';
?>
<?php
if (isset($_POST['submit']))
{
$r1=$_POST['num1'];
$result=$url.$r1;
header("location:$result");
}
?>
<html><body>
<form action="" method="post">
Num1:<input name="num1"><br>
<input type="submit" name="submit">
</form>
</body></html>
Please try the above code. I have made some modifications. The main reason your code is not working is whenever you press the submit button it is going to the the url "http://www.xyz.com/search/" directly .The if condition is never executed. In the above mentioned code it will work properly
action="" - you are submitting to the wrong url. Here is alternate version -
<?php $url='http://www.xyz.com/search/';
if (isset($_POST['submit'])) {
$r1=$_POST['num1']; header("Location: ".$r1); // 302 redirection
}
?>
<html><body> <form target="_SELF" method="post"> Num1:<input name="num1" type="text" /><br /> <input type="submit" name="submit" /> </form> </body></html>

Categories