Wordpress search form action - php

I need to do a custom search function in my wordpress page, some javascript codes needed to put into header and use wordpress search form to call that function.
Under the theme folder of wordpress, I have added some javascript codes between tag in header.php
<head>
<script>
var test123456 = function() {
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
}
</script>
</head>
The default theme searchform.php was:
<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="searchsubmit" value="Search" />
</div>
</form>
I would like to ask how do I call that javascript function by searchform.php ?
my form part:
<form action="javascript:test123456()">
<input type="text" name="firstname" value="" id="curlinput">
<input type="submit" value="Submit">
</form>
many thanks and appreciated !!

change your code to
<form onsubmit="test123456()">
<div><label class="screen-reader-text" for="s">Search for:</label>
<input type="text" value="" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" />
</div>

You can put form line this.
<form role="search" method="get" id="searchform" action="javascript:void(0);" onsubmit="test123456();">
<div><label class="screen-reader-text" for="s">Search for:</label>
<input type="text" value="" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
OR like below
<form role="search" method="get" id="searchform" action="javascript:test123456()">
<div><label class="screen-reader-text" for="s">Search for:</label>
<input type="text" value="" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
If you also want to set action file then try below code.
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>" onsubmit="test123456();">
<div><label class="screen-reader-text" for="s">Search for:</label>
<input type="text" value="" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>

Related

How to keep form data even after submiting not to dispear

In this
<form method="POST" action="">
<input type="text" class="field small-field" name="tex1" />
<input type="submit" value="search" name="search"/>
<input type="submit" value="print" name="print"/>
</form>
After I submit the form, the page refreshes and the data inside the input texts gets blank
Is it possible to keep the data even after submit?
Regards.
You can simply use ajax for submitting the form.
Or use following
<form method="POST" action=""><input type="text" class="field small-field" name="tex1" value="<?php (isset($_POST['text1]))? echo $_POST['text1] : '';" /><input type="submit" value="search" name="search"/><input type="submit" value="print" name="print"/></form>
try to echo, what ever is the variable named for your input.
<form method="POST" action="">
<input type="text" class="field small-field" name="tex1" value="<?php echo $_POST['tex1'];?>" />
<input type="submit" value="search" name="search"/>
<input type="submit" value="print" name="print"/>
</form>
With php for example:
<form method="POST" action="">
<input type="text" class="field small-field" name="tex1" value="<?php echo $_POST['tex1']; ?>"/>
<input type="submit" value="search" name="search"/>
<input type="submit" value="print" name="print"/>
</form>
If you are handling the post on the same page you could just do like this on the fields where you want the posted value to be shown:
<input type="submit" value="search" name="search" <?php if( isset( $_POST['search'] ) ){ echo "value=\"". $_POST['search'] ."\"; } ?>/>
Use this:
<form method="POST" action="">
<input type="text" class="field small-field" name="tex1" value="<?php if(isset($_POST['tex1'])) echo $_POST['tex1'] ?>" />
<input type="submit" value="search" name="search"/>
<input type="submit" value="print" name="print"/>
</form>
Bascially http is statelessprotocol , hence you need to save the data some where
The simplest way in this case would be to use a conditional operator
<input type="text" class="field small-field" name="tex1" value="<?php echo (isset($_POST['search'] || $_POST['search'] )?$_POST['tex1']:''); ?>" />

Add suffix to action url

I have this form
<form role="search" method="get" id="searchform" class="searchform" action="http://search.site.com/Archive/search/read" >
<div>
<input type="text" value="" placeholder="text" name="query" id="?" />
<input type="submit" id="searchsubmit" value="." />
</div>
</form>
I need to add this suffix at the end of the url: &title=&start_year=&end_year=
For example:
http://search.site.com/Archive/search/read?query=pertini&title=&start_year=&end_year=
Where the query is: pertini
Is it possible in JavaScript or jQuery or in PHP?
Site only support GET method.
I am working in WordPress.
Just add hidden fields for all the ones you need:
<form role="search" method="get" id="searchform" class="searchform" action="http://search.site.com/Archive/search/read">
<div>
<input type="text" value="" placeholder="text" name="query" id="?" /><input type="submit" id="searchsubmit" value="." />
</div>
<input name="title" type="hidden" value="" />
<input name="start_year" type="hidden" value="" />
<input name="end_year" type="hidden" value="" />
</form>

In a Wordpress theme (using get_search_form()), how can I add placeholder text to the search box?

I am trying to create a theme and I am displaying a search box in the header using:
<?php get_search_form(); ?>
Is there a way that I can get placeholder text to show in that box?
I ended up figuring out that if I don't have a file called "searchform.php" in my theme folder, Wordpress displays is default form that doesn't include any placeholder text:
<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="searchsubmit" value="Search" />
</div>
</form>
I created a new file called "searchform.php" in my theme folder and modified the default form to include the placeholder text:
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<div>
<input type="text" value="" name="s" id="s" placeholder="Search gear, clothing, & more.." />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
make searchform.php in theme folder and place that code in it
<form method="get" id="searchform" action="<?php bloginfo('url'); ?>">
<div>
<input class="text" type="text" value=" " name="s" id="s" />
<input type="submit" class="submit button" name="submit" value="<?php _e('Search');?>" />
</div>
</form>
Change whatever you want ! like class,placeholder,text any thing you want but not change like name, id and form attribute
Thanks
The WP codex has an example with a placeholder:
If your theme supports HTML5, which happens if it uses
add_theme_support('html5', array('search-form')), it will output the
following HTML form. This is the case since WordPress 3.6.
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
<label>
<span class="screen-reader-text">Search for:</span>
<input type="search" class="search-field" placeholder="Search …" value="" name="s" title="Search for:" />
</label>
<input type="submit" class="search-submit" value="Search" />
</form>
If you don't want to use HTML5 tags and attributes, you can create placeholders with JavaScript.
The official get_search_form explained the more flexible modified that could add in your funtions.php.
check out here with CodeX commit.
TL;DR
Checkout the code below.
function wpdocs_my_search_form( $form ) {
// replace the new form on your own.
$form = '<form role="search" method="get" id="searchform" class="searchform" action="' . home_url( '/' ) . '" >
<div><label class="screen-reader-text" for="s">' . __( 'Search for:' ) . '</label>
<input type="text" value="' . get_search_query() . '" name="s" id="s" />
<input type="submit" id="searchsubmit" value="'. esc_attr__( 'Search' ) .'" />
</div>
</form>';
return $form;
}
add_filter( 'get_search_form', 'wpdocs_my_search_form' );

Get search text value in URL

I have a form like this
<form method="get" id="searchform" action="http://domain.com">
<input name="s" id="s" type="text" />
<input type="submit" id="searchsubmit" type="submit" />
</form>
When I add some keyword on search input text, The URL is going as http://domain.com/?s=keyword
I need it as http://domain.com/?s=keyword&post_type=events
Means I need just &post_type=event added on url. How can I do it? any suggestion?
Add a hidden field
<input type="hidden" name="post_type" value="events">
Please try this code...
<form method="get" id="searchform" action="http://domain.com">
<input name="s" id="s" type="text" />
<input type="submit" id="searchsubmit" name="post_type" value='event'/>
</form>
When you press button it return to like.
http://domain.com/?s=keyword&post_type=events

Google CSE with a custom form

I have a GET form at the top of my page, but it is not yet configured. All I have for it is this:
<div class="top-search">
<form method="get" id="searchform" action="#">
<div>
<input type="text" value="Search..." name="s" id="s" onfocus="defaultInput(this)" onblur="clearInput(this)" />
<input type="submit" id="searchsubmit" value=" " />
</div>
</form>
</div>
How can I manipulate this to use Google CSE?
Seeming as no one decided to answer it, I found out for myself.
<div class="top-search">
<form id="searchform" action="http://www.google.com/cse">
<div>
<input type="hidden" name="cx" value="xxxx" />
<input type="hidden" name="ie" value="UTF-8" />
<input type="text" value="" name="q" id="q" autocomplete="off" />
<input type="submit" id="searchsubmit" name="sa" value=" " />
</div>
</form>
</div>
Where "xxxx" is your Search Engine Unique ID, found on the Basics page of your control panel.

Categories