-1

I have tried making a from and wrote a script that fetch image from google but it does not display any image nor it gives any error what shall i do. here is my code.

<html>
<head>
<title>Images</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="Post">
<table border="1">
<tr>
<td>Text:</td>
<td><input type="text" name="image" id="ImageType" value=""></td>
<td><input type="Submit" value="Click to Submit"></td>
</tr>
</table>
</form>
<br>

</body>
</html> 

<?php
include "simple_html_dom.php";
if(isset($_POST['submit'])) 
{ 
$search_query = $_POST['image'];


 // $search_query = "love";
 $search_query = urlencode( $search_query );
 $html = file_get_html( "https://www.google.com/search?q=$search_query& tbm=isch" );
 // $image_container = $html->find('div#rcnt', 0);
  $images = $html->find('img');
  $image_count = 10; //Enter the amount of images to be shown
  $i = 0;
  foreach($images as $image){
    if($i == $image_count) break;
    $i++;
    // DO with the image whatever you want here (the image element is '$image'):
    echo $image;
}
}
?>
Ali
  • 23
  • 7

1 Answers1

1
$html = file_get_html( "https://www.google.com/search?q=$search_query& tbm=isch" );

should be

$html = file_get_html( "https://www.google.com/search?q=$search_query&tbm=isch" );

A space between search_query& and tbm needs to be removed.

EDIT

<?php

if (isset($_POST['submit'])) {

    include "simple_html_dom.php";
    $search_query = $_POST['image'];
    $html = file_get_html("https://www.google.com/search?q=$search_query&tbm=isch");
    $images = $html->find('img');
    $image_count = 10; //Enter the amount of images to be shown
    $i = 0;
    foreach ($images as $image) {
        if ($i == $image_count)
            break;
        $i++;
        // DO with the image whatever you want here (the image element is '$image'):
        echo $image;
    }
}
?>

EDIT - 2 You should give name attribute to submit button such as:

<input type="Submit" name="submit" value="Click to Submit">

OR use : if (isset($_POST['image'])) { instead of if (isset($_POST['submit'])) {

Ashish Choudhary
  • 2,004
  • 1
  • 18
  • 26