I am trying to create a search box on my website that will allow readers to type in search words and display results strictly from all pages of my .org. I found a php code on this website that I think will do this? So I have created a page called "action_page.php" to process the keyword search request with the php code. I am missing something because it is all not working. I can type a keyword in the box and click submit but no results. Do I need to create a separate display results page? Or can I make the results display on the same page where I have the search box? Or do I need to edit any of the php code with my specific web address?

I tried to post my html/php code but the "code block" function would not work.

Recommended Answers

All 9 Replies

My html code

<form role="search" action="/action_page.php" method="get">
  <div class="search-container">
    <input type="search" id="mySearch" name="q"
     placeholder="Search the site..."
     aria-label="Search through site content">
    <button type="submit"><i class="fa fa-search"></i></button>

My php code

function search($search_term,$site)
global $dsite;
$bits = explode('/', $site);
if ($bits[0]=='http:' || $bits[0]=='https:')
    } else {
curl_setopt($curl_handle, CURLOPT_HEADER, false);
curl_setopt($curl_handle, CURLOPT_FAILONERROR, true);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20080623 Firefox/") ); // request as if Firefox   
curl_setopt($curl_handle, CURLOPT_POST, false);
curl_setopt($curl_handle, CURLOPT_NOBODY, false);
$buffer = curl_exec($curl_handle);
preg_match_all("/<cite>[^ ]+ - [0-9]+k - <\/cite>/",$bufferb,$match['url']);
$match['url'][0]=preg_replace('/<cite>([^ ]+) - [0-9]+k - <\/cite>/','$1',$match['url'][0]);
preg_match_all("/<a href=\"[^\"]+\"\ class\=l[^>]+>[^<]+<\/a>/",$bufferb,$match['title']);
while (isset($match['title'][0][$id]))
return $result;
echo "<form method='post' style='margin:0; padding:0;'><table border=0 cellpadding=0 cellspacing=0>
<tr><td align='right'>Website:</td><td><input type='text' size=40 name='site'></td></tr>
<tr><td align='right'>Search Term:</td><td><input type='text' size=40 name='searchval'><input type='submit' value='search'></td></tr></table></form><br>";
if (isset($_POST['searchval']) && strlen($_POST['searchval'])>=1)
    echo "<table border=0 cellspacing=0 cellpadding=0 width=640><tr><td bgcolor='#66CCFF'><table border=0 cellpadding=3 cellspacing=0><tr><td>".
    '<b>Website searched: '.$dsite.'<br> There were '.count($result['title'])." results found with the term '<i>".$_POST['searchval']."</i>'</b></td></tr></table></td></tr>";
    while (isset($result['url'][$id]) && isset($result['des'][$id]))
        echo '<tr><td><a href="http://'.$result['url'][$id].'"><font color=#0000FF>'.$result['title'][$id].'</font></a></td></tr><tr><td>'.$result['des'][$id].'</td></tr><tr><td height="16px"></td></tr>';
    echo "</table>";

Had your code calling for the function with correct parameter parsed in from action_page.php?

commented: I am not sure what you mean? +0

The PHP code you provided basically scrapes Google search results, and if they match a specific format (which may not exist anymore, as Google is frequently updating their website, of course), it spits out the results. No additional pages are required, but I highly suggest that you use Google Custom Search instead. https://cse.google.com/cse/all It's designed for this, and doing it right.

I try working with google again. :) Perhaps since I moved the search box I can get it to work right. Not sure. Thank you for taking a look at this coding!

I scrapped the the action_page.php. I have been working since last night with the google search editor. However, when the search bar works properly then the search box is so heavily padded and huge that it is ridiculous. And when I have a nice custom sized search box that looks good then I cannot get it work through google. lol, but this is crazy!

So google uses javascript. I think that I am missing something minor here. I have two different search boxes I have been playing with. Both look good but I prefer the second one because the search bar stretches across any size page very nicely.

Search bar 1:

<form onsubmit="return cse();">
<input id="search" type="search" name="query" placeholder="Search here.."/>
<button type="submit"><i class="fa fa-search"></i></button>

Search bar 2

<form name="google search" action="FULL_URL_STRING_OF_CURRENT_PAGE"> 
<input id="search" name="q" size="50" type="text" /> 
<input value="Search" type="submit"/> 

Where should I be placing the google javascript code to make this work because placing it on the same page does not work. Also I have an apache 'include' function setup so I can make my search bar appear on every page of my website from one page. How can I set up the google code to make it work this way?

well you can search in google , stackoverflow will give you answers and ofcourse youtue might be good idea for you to solve your issues

Well, it is after 1:00 a.m. where I live. Days and hours later it is now. I could never explain how I did it but I finally made a custom google search WORK! :)

My next question about this search engine that I am searching online for an answer is can I place the cse google code that goes before the closing head tag on ONE SINGLE PAGE somewhere vs. pasting it on 1,000 pages?

So far I have not found an answer for this on google.

The answer is.............................. it is not possible.

It’s possible. Do a google search for shared html headers and footers. If you’re using php, you can put the google search code in its own php file, and use an include statement. Sorry I can’t type out the code because I’m typing from my iPhone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.