Google CSE with a custom form - php

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.

Related

Wordpress search form action

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>

Nested 'post' calls inside html

If I add the first div inside the form, the submit button stops working. Must be somehow related to the nested 'post' calls and the different .php files.
<form action="register.php" method="post">
<div id="kv-avatar-errors-1" class="center-block" style="width:800px;display:none"></div>
<form class="text-center" action="/avatar_upload.php" method="post" enctype="multipart/form-data">
<div class="kv-avatar center-block" style="width:200px;text-align:center;">
<input id="avatar-1" name="profile_pic" type="file" value="" class="file-loading">
</div>
</form>
</div>
<label>Username:</label>
<input type="text" name="username" value="" />
<label>Email:</label>
<input type="text" name="email" value="" />
<label>Password:</label>
<input type="password" name="password" value="" /> <br /><br />
<input type="submit" class="btn btn-info" value="Register" />
</form>
What's the best way to fix this?
Cause form inside form is an invalid syntax. – u_mulder

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']:''); ?>" />

php: order of posted variables in array is backard

Somehow when I submit this form the values are stored last name then first name. How can I change the order?
<form action="..posted" method="post" id="register-form">
<div class=register-form-div>
First Name: <input class=register-form-input type="text" name="register[First name]" />
</div>
<div class=register-form-div>
Last Name: <input class=register-form-input type="text" name="register[Last Name]" />
</div>
<input type="submit" id="register-submit" name="register-submit" value="Create Account" />
</form>
try that
<form action="..posted" method="post" id="register-form">
<div class=register-form-div>
First Name: <input class=register-form-input type="text" name="register[1][First name]" />
</div>
<div class=register-form-div>
Last Name: <input class=register-form-input type="text" name="register[0][Last Name]" />
</div>
<input type="submit" id="register-submit" name="register-submit" value="Create Account" />
</form>

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>

Categories