-1

Notice: Undefined variable: output in C:\xampp\htdocs\tests\ser.php on line 24

i got this problem i don't know why this show ! try everything

any fix !?

this is my code php

the idea of code live search like google

<?php

require_once("conf.php");

if(isset($_POST['SearchValue'])){
         $se=$_POST['SearchValue'];
     $s=preg_replace("#[^0-9a-z]#i","",$se);
     $query=mysql_query("SELECT * FROM search WHERE P_A LIKE '%$s%'");
     $connect=mysql_num_rows($query);
     if ($connect == 0){
        $output="no reset";
         }
    else {
    while ($row = mysql_fetch_array($query)){
    $pA = $row ['P_A'];
    $pB = $row ['P_B'];
    $pC = $row ['P_C'];
    $pD = $row ['P_D'];
        }
    $output .= '<div>' . "" . $pA . "" . $pB . "" . $pC . "" . $pD .  '</div>' ;
}    
}

echo ($output);

?>

and this is my html code

<html>
<head>
    <script src="jquery.min.js"></script>
<script type="text/javascript">
function mysearch(){
   var sText =$("input [name='Search']").val();
   $.post("ser.php",{SearchValue:sText},function(output){

    $("#output").html(output);

   }
   );
};
</script>
</head>
<body>
<form action="#" method="post">
  <input type="text" name="Search"  onkeydown="mysearch();" />
  <input type="submit" value="search"  />

</form>
<div id="output" >
</body>
</html>
Ahmed Safadi
  • 4,402
  • 37
  • 33
  • Bad formatted code, not described desired behaviour - all that I love. – Regent Nov 17 '14 at 11:04
  • you're trying to access it without it being defined. define it up top – Kevin Nov 17 '14 at 11:05
  • 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) – Jocelyn Nov 17 '14 at 18:26

2 Answers2

1

Yes, you have not defined $output variable first and joining it like $output .= ....

So, use this at the top of your code:

$output = '';
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
1

if $_POST['SearchValue'] is not set the variable $output is never defined. That's the error you see.

Also in your jquery code there is an error finding your text. Change:

var sText =$("input [name='Search']").val();

To:

var sText =$("input[name='Search']").val();

In the first one you are searching an item with name 'Search' inside an input. In my code you search an input with the atribute name = 'Search'

Serpes
  • 672
  • 4
  • 14