How to display form output in an iframe?

1.7k views Asked by At

I want a search box on my website that displays the result of a Google search of my own site in an iframe on my site (XHTML 1.0 Strict).

I wasn't able to figure out how to use Google CSE, so I'm trying to roll my own.

I have this test code:

<iframe name='test' id='test'></iframe>

<form action='http://cnn.com' target='test'> 
  <input type="text" name="q" size="20" maxlength="255" /> 
  <input type='submit' value="Search" /> 
</form> 

<form action="http://www.google.com/search">
  <input type="text" name="q" size="20" maxlength="255" /> 
  <input type="hidden" name="sitesearch" value="mysite.com" />
  <input type="submit" value="Search" />
</form>

<form action="http://www.google.com/search" target='test'>
  <input type="text" name="q" size="20" maxlength="255" /> 
  <input type="hidden" name="sitesearch" value="mysite.com" />
  <input type="submit" value="Search" />
</form>

As expected, the first form above loads the CNN page into the iframe "test".

The second form works fine except that it displays the result in a new page (instead of inside the iframe).

The third form (same as the 2nd except for the 'target' attribute) doesn't do anything at all when the submit button is pushed.

What am I doing wrong? And how can I make the W3C Validator happy with this?


Edit: I think I've almost got it:

<iframe name='test' id='test'></iframe>

<form method='post' action="test.php" target='test'>
  <input type="text" name="q" size="20" maxlength="255" /> 
  <input type="submit" value="Search" />
</form>

Then this in test.php:

<?php 
  $query = $_POST["q"];
  print file_get_contents("http://www.google.com/search?q=$query&sitesearch=mysite.com");
?>

I think this is syntactically correct - the only problem is that Google gives back a 503 error (I think it doesn't want to do this without being paid, or something).

1

There are 1 answers

0
THE ONLY ONE On
<?php
    $useragent = "Opera/9.80 (J2ME/MIDP; Opera Mini/4.2.14912/870; U; id) Presto/2.4.15";
    $ch = curl_init ("");
    curl_setopt ($ch, CURLOPT_URL, "http://www.google.com/search?q=".urlencode($_GET["q"])."&btnG=Search");
    curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);   
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    echo $output = curl_exec ($ch);
    curl_close($ch);    
?>

This is the php code that will work in your test.php file but

Remember THIS IS AGAINST GOOGLE TOS please do not abuse it