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();
}
Related
I am working on a project where I need to pass an ID to controller when someone clicks on the link, and open response in new tab.
I googled for solution on this, but couldn't find proper result. I also tried by sending data through URL in anchor tag, but then it gave an error of config.php file, so I reversed the code.
Currently I am using form tag to pass data. Can ye do this using PHP, or will we have to take help of jquery?
Here's View code:
<form class="form-horizontal" method="post" action='<?php echo base_url() . "home/edit_policy"; ?>'>
<div class="form-group">
<input type="hidden" class="form-control" id="polid" name="polid" value="<?php echo $ajax_view_pol_response[0]->polid; ?>">
<div class="col-sm-offset-3 col-sm-3">
<button class="btn btn-primary" id="editpolsubmit">Edit this Policy</button>
</div>
</div>
</font>
This code is from Controller:
public function edit_policy() {
$polid = $this->input->post('polid');
}
I want to transfer the "polid" from view to controller function using anchor tag.
All positive suggestions are appreciated.
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?
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;
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.
HTML
<div class="cus_input">
<form id="category_form" method="GET" action="<?php echo base_url(); ?>welcome/find/" >
<input type="text" id="category_input" name="q" placeholder=" Find Category"/>
<a type="submit" class="ajax cboxElement" id="category_submit" src="<?php echo base_url() ?>img/board/icons/add.jpg" value="" />
</form>
</div>
Controller
function find()
{
$this->pageload_model->load_page();
$getquery = $this->input->get("q");
$data['find'] = $this->find_model->get_find_view($getquery);
$page['content'] = $this->load->view("template/findtemplate.php", $data);
echo json_encode($page);
}
This controller correctly generates my desired results when you manually go to the URI. It was slightly modified in the attempt to use jquery ui dialog. However, i'd much rather use colorbox or fancybox at this point.
find model:
public function get_find_view($q)
{
if (!$q) {
$html = "Search disrupted: <a href='". base_url()."'>Go back to main page </a>";
return $html;
} else {
$accountdata['found'] = $this->find_model->get_found_view($q);
$accountdata['create_category'] = $this->find_model->get_create_category_view($q);
return $this->load->view('find/find_view', $accountdata , TRUE);
}
}
the colobox ajax JS should be: $('ajax').colorbox()
I have the ajax class in 2 areas...a straight up link, which works with no problems, and the above mentioned submit button. What I am looking to do is send the input value to the find() controller, load the page, and open it via the colorbox window. I eventually would add a change to pushstate, so that the new URL / page can be crawled and shared.
I am having no luck. Thanks for the help! Will add any code you might need.
I haven't used colorbox before so I'm not 100% on this. I think what you need to do is serialize the data from your form and handle the ajax request yourself. So something like this:
HTML:
<div class="cus_input">
<form id="category_form" method="GET" action="<?php echo base_url(); ?>welcome/find/" onsubmit="return category_form_submit();" >
<input type="text" id="category_input" name="q" placeholder=" Find Category"/>
<a type="submit" id="category_submit" src="<?php echo base_url() ?>img/board/icons/add.jpg" value=""/>
</form>
</div>
JQUERY:
function category_form_submit() {
$.get('/welcome/find?' + $('#category_form').serialize(), function(response){
$.fn.colorbox({html:response});
//here is where you could put your pushstate code
});
return false;
}