简体   繁体   中英

Undefined Index in PHP $_POST Array

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>";
             }

         }
}
}
?>

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
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM