3

I've a page index.php where I added a search form that's connected with the database. So that When users search any word, result would show up from database. Once results generated on the page I want users to export the result into a .doc file.

I want only the query results into the .doc file but for some reasons I'm getting a blank .doc file.

Here are my codes:

searchform:

<form id="searchform" method="post">
    <input type="text" name="searchit" id="searchit" class="txt"  />
    <input type="submit" value="Search" id="button" class="button" />
</form>

Query:

<?php
include("database.php");
   $term = strip_tags(substr($_POST['searchit'],0, 100));
   $term = $mysqli->real_escape_string($term); 

   if($term=="") {
     echo "<div class='error'>Enter Something to search</div>";
     exit();
   }

   termcheck($term, $mysqli);             
   function termcheck($term, $mysqli){
     $qry="Select * from pogel where title = '$term'";
     if($result = $mysqli->query($qry)){

       $num_rows = $result->num_rows;
         if($num_rows > 0) {

           while($row = $result->fetch_assoc())
             {
               echo "Stem : ".$row['title']."<br>";
             }

         }
}
}
?>
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Mehreen
  • 85
  • 7
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – FirstOne Dec 24 '15 at 14:01
  • 1
    Do you have a element with name 'searchit' on your HTML page which is sending the POST data? – Phiter Dec 24 '15 at 14:07
  • Yes, 'searchhit' is already on my search page but still getting above error. – Mehreen Dec 24 '15 at 14:11

1 Answers1

1

Seems like you just don't have this key in your POST.

You may try this if no such element comes to script:

$searchit = filter_input(INPUT_POST, 'searchit');

if(!$searchit) {
  echo "<div class='error'>Enter Something to search</div>";
  exit();
}

$term = strip_tags(substr($searchit,0, 100));
$term = $mysqli->real_escape_string($term); 

Try this to generate doc file:

<?php
include("database.php");
$term = strip_tags(substr($_POST['searchit'],0, 100));
$term = $mysqli->real_escape_string($term); 

if($term=="") {
    echo "<div class='error'>Enter Something to search</div>";
    exit();
}

$output = termcheck($term, $mysqli);

function termcheck($term, $mysqli){
    $qry = "Select * from pogel where title = '$term'";
    $result = '';

    if($result = $mysqli->query($qry)){
        $num_rows = $result->num_rows;
        if($num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                $result .= "Stem : ".$row['title']."<br>";
            }
        }
    }

    return $result;
}

ob_flush();

header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=document_name.doc");

echo $output;

//ob_flush_clean() might be useful here, but not sure
?>
  • Your codes fixed that error but unfortunately, I'm getting a blank page and query is not posting any result. – Mehreen Dec 24 '15 at 14:17
  • Could you please update your question with a form, that submits `searchit`? Btw, try `var_dump`ing $_POST array to see if there's `searchit` key present. – Denis Alexandrov Dec 24 '15 at 14:19
  • just updated my Question and codes. Please check again! – Mehreen Dec 24 '15 at 14:31
  • Thank you. Is this the same file with HTML and PHP? – Denis Alexandrov Dec 24 '15 at 14:36
  • yeah same file and my results are showing on webpage but when I click the button "Export as MS word" then I'm able to generate a blank .doc file. – Mehreen Dec 24 '15 at 14:41
  • yeah. I' using this at the top of page to generate a .doc file – Mehreen Dec 24 '15 at 14:47
  • & I've already shared my form and query. Is there any other thing that I need to apply to run the query for .doc file? My query is working on webpage but not on .doc file. May be any good tutorial for me to follow or any other help? – Mehreen Dec 24 '15 at 14:48
  • I've updated my answer, give it a try. (oops, misspelled last variable. fixed it now `echo $output` instead of `echo $result`) – Denis Alexandrov Dec 24 '15 at 15:01
  • I've tried your codes and I'm still getting blank page. – Mehreen Dec 24 '15 at 15:19
  • Ok. Sorry, I can't test it right now, but try using `ob_flush_clean()` before headers and echo random string before them. This string should be in your doc later – Denis Alexandrov Dec 24 '15 at 15:26