I am using this code for my search:
<?php
function wp_search_form($form) {
$form = '<form method="get" id="searchform" action="' . get_option('home') . '/" >
'.wp_dropdown_categories('exclude=1 Categories&hide_empty=0&echo=0&selected='.intval($_GET['cat']).'').'
<input type="text" class="search_input" value="' . attribute_escape(apply_filters('the_search_query', get_search_query())) . '" name="s" id="s" />
<input type="submit" alt="Search" class="greybutton float_right" value="Search" />
</div>
</form>';
return $form;
}
I want to remove the catagory dropdown and instead make it search the current catagory
I don't want to steal TheDeadMedic's karma... my rating isn't high enough to comment on existing answers yet...
Here's his code put together:
function wp_search_form($form) {
global $wp_query;
$form = '<form method="get" id="searchform" action="' . get_option('home') . '/" >
<input type="hidden" name="cat" value="'. $wp_query->get_queried_object_id() .'" />
<input type="text" class="search_input" value="' . attribute_escape(apply_filters('the_search_query', get_search_query())) . '" name="s" id="s" />
<input type="submit" alt="Search" class="greybutton float_right" value="Search" />
</div>
</form>';
return $form;
}
Remove the wp_dropdown_categories() call and replace with;
<input type="hidden" name="cat" value="<?php echo $wp_query->get_queried_object_id(); ?>" />
Note if you're using that inside a function, you'll need to globalise $wp_query.
I make the search in current category with the following code (in searchform.php or where you want to put a search form:
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<div>
<input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
<input type="hidden" name="cat" value="<?php echo $cat ;?>" />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
Related
I'm having trouble adding a shortcode directly to my theme.
The problem occurs with the short code.
Can someone help me?
Thank you!
[ihc-level-link id=1]
<div id="advc-menu" class="search">
<form method="get" id="searchform" action="<?php echo esc_url( home_url() ); ?>">
<input type="text" placeholder="Search..." name="s" id="s" value="<?php echo dt_clear($_GET['s']); ?>" autocomplete="off">
<button class="search-button" type="submit"><span class="icon-search2"></span></button>
</form>
</div>
[/ihc-level-link]
Use this code:
<?php
$html = '<div id="advc-menu" class="search">
<form method="get" id="searchform" action="'. esc_url( home_url() ) .'">
<input type="text" placeholder="Search..." name="s" id="s" value="'. dt_clear($_GET['s']) .'" autocomplete="off">
<button class="search-button" type="submit"><span class="icon-search2"></span></button>
</form>
</div>';
echo do_shortcode("[ihc-level-link id=1] ". $html ." [/ihc-level-link id=1]");
?>
When I click submit, the data is submitted to the database but the URL id of the book disappears to ----book.php
I want the URL to go back to the id of the page e.g. ----book.php?id=3
Is it possible to keep the first line as action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" and add the value="<?php echo $book_id ?>"?
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" value="<?php echo $book_id ?>" name="book_id" />
<p>Author: <input type="text" value="<?php echo $_SESSION['author']; ?>" name="author" id="author" readonly /></p>
<p>Summary: <input type="text" name="summary" value="<?php echo $summary;?>" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
PHP code:
if (isset($_GET['id'])) {
$book_id = $_GET['id'];
}
Please try this code:
// save values in DB
header('Location: book.php?id=' . $book_id);
exit;
It is because $_SERVER["PHP_SELF"] contains only URL . It does not contain get queries . To solve the problem leave your action empty
e.g
<form method="post" action="">.....
You send a POST form but are trying to retrieve a GET value. Additinally, the parameter is named book_id, not id.
Use $book_id = $_POST['book_id'];
if (isset($_GET['id'])) { also won't work, for the same reasons.
There is no name with "id"
If you want to get the book id:
if (isset($_POST['book_id'])) {
$book_id = $_POST['book_id'];
}
OR modify the HTML like this:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" value="<?php echo $book_id ?>" name="id" />
<p>Author: <input type="text" value="<?php echo $_SESSION['author']; ?>" name="author" id="author" readonly />
</p>
<p>Summary: <input type="text" name="summary" value="<?php echo $summary;?>" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
On the other hand, if you have problems with POST, just modify the first line of HTML for this one:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]) . "?id=" . echo $book_id; ?>">
You can use anyone of these:
PHP_SELF returns just URL. Use REQUEST_URI that returns URL with query string:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]);?>">
[you can also omit action values - that will have same behavior]
or if you want just id, then use:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]) . "?id=" . $_GET['id'];?>">
Note: Use validation wherever necessary. I just gave you an idea.
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>
i'm trying to make CMS with PHP
i need to get the if from post request
<form action="updatevideo.php" method="post" role="form">
Title: <input name="title" type="text" required> <br />
description:<input name="desc" type="text" required> <br />
url: <input name="ytl" type="text" required> <br />
<input type="hidden" name="id" />
<input type="submit" name="addVideo" value="Add New Video" />
</form>
how can i make this input's value = id
<input type="hidden" name="id" />
on control page
public function Update()
{
/*
1-get data into variables
2-validation
3-Database
*/
if(isset($_GET['id']) && (int)$_GET['id']>0)
{
$id = (int)$_GET['id'];
$user = $this->videoModel->Get_By_Id($id);
print_r($user);
echo
'
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<form action="updatevideo.php" method="post" role="form">
Title: <input name="title" type="text" required> <br />
description:<input name="desc" type="text" required> <br />
url: <input name="ytl" type="text" required> <br />
<input type="hidden" name="id" />
<input type="submit" name="addVideo" value="Add New Video" />
</form>
</body>
</html>
';
}
else
{
if(isset($_POST['addVideo']))
{
$id = $_POST['id'];
echo "5ara" ;
$title = $_POST['title'];
$desc = $_POST['desc'];
$url = $_POST['ytl'];
//Validation
$vid = $this->getVid($url); //video id -_-
$data = array(
'title' => $title,
'desc' => $desc,
'vid' => $vid
);
if($this->videoModel->Update($id,$data))
{
System::Get('tpl')->assign('message','User Updated');
System::Get('tpl')->draw('success');
}
else
{
System::Get('tpl')->assign('message','Error Updating User');
System::Get('tpl')->draw('error');
}
}
else
{
System::Get('tpl')->assign('message','NO USER CHOSEN');
System::Get('tpl')->draw('error');
}
}
}
Quite simple really. As you checked it exists and moved it into a variable you can just echo the $id into the value attribute of that input tag.
<input type="hidden" name="id" value="' . $id . '" />
Using your code:
echo '
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<form action="updatevideo.php" method="post" role="form">
Title: <input name="title" type="text" required> <br />
description:<input name="desc" type="text" required> <br />
url: <input name="ytl" type="text" required> <br />
<input type="hidden" name="id" value="' . $id . '" />
<input type="submit" name="addVideo" value="Add New Video" />
</form>
</body>
</html>';
Define the VALUE attribute :
<input type="hidden" name="id" value="ID_VALUE"/>
OR if you have id in varible than use below code :
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
OR
As you are using the HTML in PHP than:
<input type="hidden" name="id" value="'.$id.'"/>
ID_Value should be your id like 1,2,3..etc.
And Get the id on Action page :
$id = $_POST['id'];
Apart from this you have mention POST in form method and on your
action page you are trying to get the values using GET method,
which is wrong.
Edited due to question edit.
<input type="hidden" name="id" value="'.$id.'" />
You echo a big block of text, this way you can concatenate the $id by stopping block output, including $id and resuming block output.
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' );