Pass variable to Google Custom Search Engine - search-engine

Is it possible to pass a search variable into the Google Custom Search Engine that I have embedded on my website? I can get the search engine to work, but I can't pass it a term via POST (it's coming from a search button on other pages of the website)
I tried to hack the code I found here: http://code.google.com/apis/ajax/playground/?exp=search#hello_world
And this is what I have so far... ($q is the term I am passing to it)
<script type="text/javascript">
google.load('search', '1', {language : 'en'});
function OnLoad()
{
var customSearchControl = new google.search.CustomSearchControl('***my key****');
customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
customSearchControl.draw('cse');
searchControl.execute("$q");
}
google.setOnLoadCallback(OnLoad);
</script>
Thanks

Sorry, I know it's a crappy answer, but you've actually got it right apart from referencing the wrong variable name. Oh, also, as an aside, I would also hope you're doing some kind of sanitisation on $q, in case someone posted something like this to your form: term"); alert("aha!
customSearchControl.draw('cse');
searchControl.execute("$q");
should be:
customSearchControl.draw('cse');
customSearchControl.execute("$q");
Also, thank you for the question - I was looking for how to do this myself!

This is to help anyone using PHP trying to accomplish this same goal. The example above uses...
customSearchControl.execute("$q");
to read the parameter being passes in. On a PHP site you would use...
customSearchControl.execute("<?php echo $_POST['your_paramter_name_here'];?>");
You could use $_GET or $_REQUEST if your parameter is not in the post.
Of course you should should sanitize the input first. Something like this is pretty weak but it's a start...
customSearchControl.execute("<?php echo htmlentities( trim( $_POST['your_paramter_name_here'] ), ENT_QUOTES );?>");

In case someone is looking for a bit more straight forward / simple solution. All you have to do is to pass search keywords into GET parameter named q (from your custom form into page where your GCS is), GCS will automatically use that search phrase.
Source: https://developers.google.com/custom-search/json-api/v1/using_rest

Related

Problems spawning draggable objects from database using web2py and jQueryUI

I am attempting to build a small proof-of-concept web application using the web2py framework. I'm so close, but my basic lack of knowledge of what's going on means I'm just hacking at it with pure guesswork rather than understanding what's going on. I was hoping someone on here could explain where I am going wrong...
The functionality I'm after is that the data needed to create the draggable items is held in a database table (and will ultimately form a hierarchy) with as little information held in the HTML as possible.
There's a fair bit of information for just about everything in this stack, so much so that I'm drowning in it, I don't know where to start. I suppose I should begin with what I've got so far...
The HTML:
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
...
<script>
$(document).ready(function(){
$.ajax( {
type: "POST",
url: 'dragndrop.py',
success: function( response ) {
$("#draggable7").html(response);
}
} )
});
</script>
<div id="draggable7"></div>
The dragndrop.py script:
## My main draggable spawner
selected = [row.id for row in db(db.t_user_shop_layouts.id==7).select()]
return ''.join([DIV('draggable'.join(k), _class='draggable ui-widget-content', _snap=".ui-widget-header", _snapMode= "inner", _grid= [ 80, 80 ], _style='position: relative;') for k in selected])
And, just for completeness, the model web2py script (although the column I'm interested in is the "id" column, which is auto-generated):
db.define_table('t_shop_layout_items',
Field('f_item_display_name_string', type='string', notnull=True,
label=T('Item Display Name String')),
Field('f_item_icon_file', type='upload',
label=T('Item Icon File')),
Field('f_item_parent_id', type='integer',
label=T('Item Parent Id')),
auth.signature,
format='%(f_item_display_name_string)s',
migrate=settings.migrate)
I'm forcing the db call to only pick up one row at the moment (id == 7) just to get the ball rolling, but eventually what I'd like to do is have the (db.id == db.f_item_parent_id) items shown first. Then when double-click, any (db.f_item_parent_id == this.id) children get spawned using helpers. Then I'm going to get an 80x80 grid size target to land on to set shop layout, and save to db. But all this paragraph is for later, I'm just giving you an idea of where I'm going with it.
Finally, some great tutorials that have helped me along the way, but were either not web2py (PHP seems popular for this) or not dynamically spawning (but hard-coded in the HTML, or what-have-you. If I've missed something obvious, please let me know:
An excellent fiddle: http://jsfiddle.net/robertrozas/qLhke/25/ and its stack overflow beginnings Jquery drag drop form hidden value inserting into php mysql
The web2py documentation: http://web2py.com/books/default/chapter/34/11/jquery-and-ajax
OK, hope that's enough! Any help would be appreciated!
In web2py, you do not create .py files and then use them as URLs. Instead, you create functions in controllers and have URLs of the form /appname/controller/function. See the documentation on dispatching. It is also best to use the built-in URL() function to generate URLs. Also, this should probably be a GET request rather than POST.
You might also want to look into web2py's built-in ajax() function as well as Ajax components.
Regarding your data model, if the f_item_parent_id field is a self reference, then you should define it as a reference field (i.e., type='reference t_shop_layout_items').
More generally, before proceeding further, it will probably be very helpful if you read more of the documentation, particularly chapters 4, 5, 11, and possibly 12.

Is there a best method to evaluate URL variables to determine the appropriate page?

I am using ColdFusion 9.0.1.
I have a new web site that uses Bikes.cfm and Makers.cfm as template pages. I need to be able to pass BikeID and MakerID to both of the these pages, along with other variables. I don't want to use the Actual page name in the URL, such as this:
MyDomain.com/Bikes.cfm?BikeID=1234&MakerID=1234
I want my URL to look more like this:
MyDomain.com/?BikeID=1234&MakerID=1234
I need to NOT specify the page name in the URL.
I want these two URLs to access different data:
MyDomain.com/?BikeID=1234&MakerID=1234 // goes to bike page
MyDomain.com/?MakerID=1234&BikeID=1234 // goes to maker page
So, if BikeID appears in the URL before MakerID, go to the Bikes.cfm page. If MakerID appears before BikeID, go the Makers.cfm page.
Is there an easy and existing method to arrange the URL keys in such a way to have them point to the appropriate page?
Should I just parse the the URL as a list and determine the first ID and go to the appropriate page? Is there a better way?
Any thoughts or hints or ideas would be appreciated.
UPDATE -- It certainly appears that using the order of parameters in a URL is a bad idea for the following reasons:
1) many programs append variables to the URL
2) some programs may reorder the variables
3) GoogleBot may not consider order relevant and will most likely not index the site correctly.
Thanks to everyone who provided advice in a positive manner that my approach was probably a bad idea and would not produce the results I wanted. Thanks to everyone who suggested alternate means to produce the results I wanted.
If anyone of you positive people would like to put your positive comment/advice as an answer, I'd be happy to accept it as the answer.
Despite my grave misgivings about the whole idea, here's how I would do it if I were forced to do so:
index.cfm:
<cfswitch expression="#ListFirst(cgi.query_string, '=')#">
<cfcase value="BikeID">
<cfinclude template="Bikes.cfm">
</cfcase>
<cfcase value="MakerID">
<cfinclude template="Makers.cfm">
</cfcase>
<cfdefaultcase>
<cfinclude template="Welcome.cfm">
</cfdefaultcase>
</cfswitch>

Google Search with RoR, how make it works?

I am new to RoR, and I wonder how I can do a google search by using a field in view of my project, anyone know how I could do that?
I have in my application.html.haml the code :
%form#search
%input
How can i implement this?
Set the action of your form to http://www.google.com/search, the method to get, and name your input field q. Something like the following:
%form#search(action="http://www.google.com/search" method="get")
%input(type="text" name="q")
%input(type="submit")
The result will be the normal URL for google searches with the results displayed:
http://www.google.com/search?q=term1+term2

Mathjax after Ajax call

So my problem is as follow:
So firstly, I made a ajax call to another page and replaces some of the elements in my current DOM with the new information, which utilize Mathjax to render their math formulas. However, after my ajax call, Mathjax doesn't seem to be loaded again thus not rendering the math formulas for my new math formulas. Does anyone have encountered this before? What is the solution?
Thanks a lot
This Mathjax documentation page should help you a lot.
For example, it has MathJax.Hub.Queue(["Typeset",MathJax.Hub]); method call to update every new node on the page.
In addition to Daniel O'Hara answer you also might try to call MathJax.Hub.Queue(["Reprocess",MathJax.Hub]). Or call MathJax.Hub.Typeset() directly but its not recommended to do so in production

asp:listbox how to provide data?

I'm having some trouble with in my MVC-View. The data for the listbox is passed by the controller and accessible via Model.templateList. So now I have to pass these data to the asp:listbox. Is there any way to do this or do I have to use some sort of DataProvider. What would be bad in terms of SoC. I considered using the Html-Helper Html.ListBox but I have no idea how to get actions like double-click and so on to work with it. Hope there are many smart people with some knowledge about this.
Thank you for reading
To bind to the list box you would using something like this
#Html.ListBox("ListBoxName", new SelectList(Model,"dataValueField", "dataTextField"));
with regards to the the actions you would need to use JavaScript. A google query like "listbox double click javascript" will help you get to the next level.

Resources