PDA

View Full Version : Radio Buttons for search


deverill2010
02-24-2012, 09:46 AM
I currently have a custom google search bar on my site which only searches sites that I have allowed in google when creating the search. The search results are also displayed within a page which looks like my website. This all works fantastically well but I would like to add to improve the search.

I want to add two radio buttons, one for WWW and one for my site, so when the radio button WWW is selected it does as abve searches the sites I have allowed in google and displays them within its page as it currently does.

When my sites radio button is selected it just does a quick search of MY SITE only and displays the results in a page looking like my site.

I have tried before to do this but haven't been able to master it, I would really appreciate any help as I really realy want this.

Here is my crrent search code in the navbar template -

<div id="globalsearch" class="globalsearch">
<form action="http://www.midwiferyonline.co.uk/googlesearch.php" id="cse-search-box">
<div>
<input type="hidden" name="cx" value="partner-pub-XXXXXXXXXXXXXXXXXXXXXXXX" />
<input type="hidden" name="cof" value="FORID:10" />
<input type="hidden" name="ie" value="UTF-8" />
<span class="textboxcontainer"><span> <input type="text" name="q" size="20" /></span></span>
<span class="buttoncontainer"><span><input type="image" class="searchbutton" src="{vb:stylevar imgdir_button}/search<vb:if condition="$stylevar['textdirection'] == 'rtl'">_rtl</vb:if>.<vb:if condition="(is_browser('ie') AND !is_browser('ie', 7) AND !is_browser('ie', 8))">gif<vb:else />png</vb:if>" name="submit" onclick="document.getElementById('navbar_search').submit;" tabindex="100"/></span></span>
</div>
</form>

And my google search.php

<?php



// ####################### SET PHP ENVIRONMENT ###########################

error_reporting(E_ALL & ~E_NOTICE);



// #################### DEFINE IMPORTANT CONSTANTS #######################



define('THIS_SCRIPT', 'Googlesearch');

define('CSRF_PROTECTION', true);

// change this depending on your filename



// ################### PRE-CACHE TEMPLATES AND DATA ######################

// get special phrase groups

$phrasegroups = array();



// get special data templates from the datastore

$specialtemplates = array();



// pre-cache templates used by all actions

$globaltemplates = array('googlesearch',

);



// pre-cache templates used by specific actions

$actiontemplates = array();



// ######################### REQUIRE BACK-END ############################

// if your page is outside of your normal vb forums directory, you should change directories by uncommenting the next line

// chdir ('/path/to/your/forums');

require_once('./global.php');



// ################################################## #####################

// ######################## START MAIN SCRIPT ############################

// ################################################## #####################



$navbits = construct_navbits(array('' => 'Google Search'));

$navbar = render_navbar_template($navbits);



// ###### YOUR CUSTOM CODE GOES HERE #####

$pagetitle = 'Google Search';



// ###### NOW YOUR TEMPLATE IS BEING RENDERED ######



$templater = vB_Template::create('googlesearch');

$templater->register_page_templates();

$templater->register('navbar', $navbar);

$templater->register('pagetitle', $pagetitle);

print_output($templater->render());



?>

Any help please?

deverill2010
02-26-2012, 03:41 PM
Anyone able to help me please as I'm stumped! :)

kh99
02-26-2012, 04:20 PM
You might be able to do that with javascript. Add an onsubmit function to the form tag, and if the radio button is selected for your site, change the action of the form to search.php?do=process. You also need the search term to be in a variable called "query", so you could add a new hidden field named query and set it to the value of the q input field if you're going the site search page.

deverill2010
02-26-2012, 04:23 PM
Thank you for replying but that just went straight over my head lol. I've never wrote JavaScript before and as for the rest of it I have no idea. I thought it would be something easy that I was just too thick to see.

:(

kh99
02-26-2012, 04:30 PM
There may be some other way to do it - you can probably redirect to the search page from the php script. But to be honest I can't quite figure out how that script works because it seems that it doesn't do anything except display a template.

deverill2010
02-26-2012, 04:44 PM
Lol oops that's because it does..

Here's the template -

{vb:stylevar htmldoctype}
<html xmlns="http://www.w3.org/1999/xhtml" dir="{vb:stylevar textdirection}" lang="{vb:stylevar languagecode}" id="vbulletin_html">
<head>
<title>{vb:raw vboptions.bbtitle} - {vb:raw pagetitle}</title>
{vb:raw headinclude}
{vb:raw headinclude_bottom}
</head>
<body>

{vb:raw header}

{vb:raw navbar}

<div id="pagetitle">
<h1>{vb:raw pagetitle}</h1>
</div>

<h2 class="blockhead">Your are searching using Google!</h2>
<div class="blockbody">
<div class="blockrow">
<div id="cse-search-results"></div>
<script type="text/javascript">
var googleSearchIframeName = "cse-search-results";
var googleSearchFormName = "cse-search-box";
var googleSearchFrameWidth = 800;
var googleSearchDomain = "www.google.co.uk";
var googleSearchPath = "/cse";
</script>
<script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>
</div>
</div>

{vb:raw footer}
</body>
</html>

So the bit in the navbar template is the google search box with my Adsense code.

When something is searched for The php page renders the template to display the results from google into (my google search template, the code in this post).

It's so the results look a part of my site. Does that make sense?

kh99
02-26-2012, 05:09 PM
You could try putting something like this near the beginning of your php script (maybe just under require_once('./global.php')):


if ($_REQUEST['radio_button'] == ???)
{
$vbulletin->url = 'search.php?do=process&query=' . $_REQUEST['q'];
print_standard_redirect('', false);
}



the 'if' condition needs to be changed, I couldn't figure it out because you provided a picture of the radio buttons but not the code (or maybe you only made a picture?)

ETA: the $_REQUEST['q'] probably needs to be encoded for a url - I'll have to look that up.

deverill2010
02-26-2012, 05:12 PM
Thanks, Yeah I only made a picture lol, I haven't done the code for the radio buttons as I don't now how to incorporate that into what I've already got.

I find it a lot harder editing/adding to something that already exists than writing something of my own from scratch.

kh99
02-26-2012, 05:21 PM
Well, I only know the basics of formatting using html, but I think basically it would be like:

<div id="globalsearch" class="globalsearch">
<form action="http://www.midwiferyonline.co.uk/googlesearch.php" id="cse-search-box">
<div>
<input type="hidden" name="cx" value="partner-pub-XXXXXXXXXXXXXXXXXXXXXXXX" />
<input type="hidden" name="cof" value="FORID:10" />
<input type="hidden" name="ie" value="UTF-8" />
<span class="textboxcontainer"><span> <input type="text" name="q" size="20" /></span></span>
Midwifery Online <input type="radio" name="source" value="0"> WWW <input type="radio" name="source" value="1"> <span class="buttoncontainer"><span><input type="image" class="searchbutton" src="{vb:stylevar imgdir_button}/search<vb:if condition="$stylevar['textdirection'] == 'rtl'">_rtl</vb:if>.<vb:if condition="(is_browser('ie') AND !is_browser('ie', 7) AND !is_browser('ie', 8))">gif<vb:else />png</vb:if>" name="submit" onclick="document.getElementById('navbar_search').submit;" tabindex="100"/></span></span>
</div>
</form>


I added the line in red. The maybe the code could be:

if ($_REQUEST['source'] == 0)
{
$vbulletin->url = 'search.php?do=process&query=' . urlencode($_REQUEST['q']);
print_standard_redirect('', false);
}

deverill2010
02-26-2012, 06:28 PM
Kh99, I tried it but both radio buttons still show results from the google search i.e the web. Just selecting my site doesn't use the sites search.
Any other ideas?

--------------- Added 1330284715 at 1330284715 ---------------

Sorry ignore that, I edited the wrong file.

If I tick my site for search and then search, it just gives me a blank page with this as the URL -

http://www.midwiferyonline.co.uk/googlesearch.php?cx=partner-pub-XXXXXXXXXXXXXXXX&cof=FORID%3A10&ie=UTF-8&q=study+day&source=0&submit.x=16&submit.y=18&siteurl=www.midwiferyonline.co.uk%2Fsearch.php%3Fs earchid%3D115155&ref=www.midwiferyonline.co.uk%252Fsearch.php%253Fs earchid%253D115154

And a blank page for the www search with this for the url -

http://www.midwiferyonline.co.uk/googlesearch.php?cx=partner-pub-XXXXXXXXXXXXXXXXXXXXXXXXXXX&cof=FORID%3A10&ie=UTF-8&q=study+day&source=1&submit.x=1&submit.y=9&siteurl=www.midwiferyonline.co.uk%2Fsearch.php%3Fs earchid%3D115155&ref=www.midwiferyonline.co.uk%252Fsearch.php%253Fs earchid%253D115154

?

kh99
02-26-2012, 06:50 PM
Hmm...I threw together a test page based on your script and it seems to work. I'll post it in case it helps you somehow:

<?php
// ####################### SET PHP ENVIRONMENT ###########################

error_reporting(E_ALL & ~E_NOTICE);



// #################### DEFINE IMPORTANT CONSTANTS #######################



define('THIS_SCRIPT', 'Googlesearch');

define('CSRF_PROTECTION', true);

// change this depending on your filename



// ################### PRE-CACHE TEMPLATES AND DATA ######################

// get special phrase groups

$phrasegroups = array();



// get special data templates from the datastore

$specialtemplates = array();



// pre-cache templates used by all actions

$globaltemplates = array();



// pre-cache templates used by specific actions

$actiontemplates = array();



// ######################### REQUIRE BACK-END ############################

// if your page is outside of your normal vb forums directory, you should change directories by uncommenting the next line

// chdir ('/path/to/your/forums');

require_once('./global.php');


if ($_REQUEST['do'] == 'search')
{
if ($_REQUEST['source'] == 0)
{
$vbulletin->url = 'search.php?do=process&query=' . urlencode($_REQUEST['q']);
print_standard_redirect('', false);
}
else
echo 'search google';

}
else
{
echo '<form action="test.php">
<input type="hidden" name="do" value="search">
<input type="text" name="q" size="20" /></br>
Midwifery Online <input type="radio" name="source" value="0"> WWW <input type="radio" name="source" value="1">
<input type="image" src="images/buttons/find.gif" name="submit" onclick="document.getElementById(\'navbar_search\').submit;" tabindex="100"/>
</form>';
}

deverill2010
02-26-2012, 07:00 PM
I get

Parse error: syntax error, unexpected $end in /home/midwife/public_html/googlesearch.php on line 1

That when using that?

kh99
02-26-2012, 07:08 PM
Are you sure you got it all? Maybe you missed the } on the last line or something.

deverill2010
02-26-2012, 07:12 PM
Sorry I'll try it again, im not that hot with this stuff. I appreciate your help honestly I do. This is something I have wanted for ages!

So the above code is meant to go where? replace my googlesearch.php? if so then what goes here - echo '<form action="test.php"> ?

kh99
02-26-2012, 07:17 PM
OK, I'm sorry, I wasn't very clear. That's just a test file, it shouldn't replace anything you have. Put it in a file called test.php, copy it to your forum directory, then go to it in your browser. When you press 'submit query' it should go to the forum search page or just print out "search google", depending on which radio button is selected. It's just supposed to show that the radio buttons and the redirect work.

deverill2010
02-26-2012, 07:24 PM
And so it does lol.

Sorry for being a total dim ass.

Why wont it work actually on the site then?

I did have a play with this before several times and I just couldnt figure it out :(

kh99
02-26-2012, 07:29 PM
So you put this in your googlesearch.php just under the global.php require line?:

if ($_REQUEST['source'] == 0)
{
$vbulletin->url = 'search.php?do=process&query=' . urlencode($_REQUEST['q']);
print_standard_redirect('', false);
}


If that's true, then temporarily change it to this:

if (1 || $_REQUEST['source'] == 0)
{
$vbulletin->url = 'search.php?do=process&query=' . urlencode($_REQUEST['q']);
print_standard_redirect('', false);
}


that should make it always go to your site search page.

deverill2010
02-26-2012, 07:45 PM
Just gives me a blank page again on both radio buttons with this as the url -

http://www.midwiferyonline.co.uk/googlesearch.php?cx=partner-pub-XXXXXXXXXXXXXXXXXXXX&cof=FORID%3A10&ie=UTF-8&q=help&source=0&submit.x=12&submit.y=17&siteurl=www.midwiferyonline.co.uk%2Fforum.php&ref=www.midwiferyonline.co.uk%2Fsearch.php%3Fsearc hid%3D115193

Heres the googlesearch.php

<?php



// ####################### SET PHP ENVIRONMENT ###########################

error_reporting(E_ALL & ~E_NOTICE);



// #################### DEFINE IMPORTANT CONSTANTS #######################



define('THIS_SCRIPT', 'Googlesearch');

define('CSRF_PROTECTION', true);

// change this depending on your filename



// ################### PRE-CACHE TEMPLATES AND DATA ######################

// get special phrase groups

$phrasegroups = array();



// get special data templates from the datastore

$specialtemplates = array();



// pre-cache templates used by all actions

$globaltemplates = array('googlesearch',

);



// pre-cache templates used by specific actions

$actiontemplates = array();



// ######################### REQUIRE BACK-END ############################

// if your page is outside of your normal vb forums directory, you should change directories by uncommenting the next line

// chdir ('/path/to/your/forums');

require_once('./global.php');
if (1 || $_REQUEST['source'] == 0)
{
$vbulletin->url = 'search.php?do=process&query=' . urlencode($_REQUEST['q']);
print_standard_redirect('', false);
}
// ################################################## #####################

// ######################## START MAIN SCRIPT ############################

// ################################################## #####################



$navbits = construct_navbits(array('' => 'Google Search'));

$navbar = render_navbar_template($navbits);



// ###### YOUR CUSTOM CODE GOES HERE #####

$pagetitle = 'Google Search';



// ###### NOW YOUR TEMPLATE IS BEING RENDERED ######



$templater = vB_Template::create('googlesearch');

$templater->register_page_templates();

$templater->register('navbar', $navbar);

$templater->register('pagetitle', $pagetitle);

print_output($templater->render());



?>

kh99
02-26-2012, 07:52 PM
Well, I'm puzzled - it looks OK to me. Try commenting out the print_standard_redirect line (put // in front of it)

deverill2010
02-26-2012, 08:01 PM
Ah!

Now my search is still giving me the blank page with the funny URL, even though I have removed the edits?

My googlesearch.php is back to how it was and I have removed the bit from the template too..?

kh99
02-26-2012, 08:13 PM
Yeah, that's strange. There's got to be something wrong with that file, but I copied what you posted in the first post and it works for me.

What program are you using to edit your files?

deverill2010
02-26-2012, 08:14 PM
Notepad++

I have recopied what I put in my very first post which was my original file, but its still showing me a blank page with all the funny stuff in the url.

*Shrugs Shoulders*

It worked fine before we started playing with it hahaha!

kh99
02-26-2012, 08:23 PM
That stuff in the url I think is normal, because the search forum uses the "GET" method which puts all the info on the url. Hate to ask, but are you sure you're editing the correct file? (you said earlier that you edited the wrong one).

Also, if you have access to your web server error logs, check there.

Oh - another thing you could try, edit that file and put

die("This is a test");


right under the <?php at the top and see if you get that message.

deverill2010
02-26-2012, 08:30 PM
Haha yep right file.

If I add that bit of code I get this when doing a search -

Fatal error: Call to undefined function phpdie() in /home/midwife/public_html/googlesearch.php on line 1

kh99
02-26-2012, 08:32 PM
OK, that confirms that it's the correct file. The error looks like you just have no space between the <?php and the die(). (or if you do, maybe it's a clue to the problem). Do you have a windows server by any chance? And I assume you're editing on your local computer and uploading it? Maybe it's a problem with ftp settings.

deverill2010
02-26-2012, 09:03 PM
Hmm Dunno I have had to revert back to the standard search, so never mind.