Gregor Ostrov
Gregor Ostrov

Reputation: 45

Live search with sqlsrv and $.ajax

why do i get all the time the Notice: Undefined index and Data Not Found as a result when i start to type in the search field ?

here ist my HTML code:

<body>
   <div class="container">
   <br />
     <div class="row">
       <div class="col-lg-6">
        <div class="input-group">
            <span class="input-group-btn">
                <button class="btn btn-secondary" 
                  type="button">Search</button>
            </span>             
            <input type="text" class="form-control" name="search_text"
             id="search_text" placeholder="search by a name">
         </div>
        </div>
       </div>
       <br />
     <div id="result"></div>
    </div>


   <script type="text/javascript">
     $(document).ready(function() {
      $('#search_text').keyup(function() {
        var search = $(this).val();
            $('#result').html('');
            $.ajax({
                url:"fetch.php",
                method:"POST",
                data:search,
                dataType:"text",
                success:function(data){
                    $('#result').html(data);
                }
            });
          });
       });
      </script>
     </body>
    </html>

php code:

<?php

    $serverName = "myServer";
    $connectionInfo = array( "Database"=>"myTable", "Uid" => "myUser", "PWD" => "myPassword");
    $conn = sqlsrv_connect( $serverName, $connectionInfo);
    $sql = "SELECT * FROM(SELECT AB.Bezeichnung1 AS Name,

    CASE FC.Ebenen 
        WHEN 1 THEN FC.Komponente1 
        WHEN 2 THEN FC.Komponente2 
        WHEN 3 THEN FC.Komponente3 
    END AS Farbe,

    GC.Bezeichnung AS Groesse,
    SKU.KommLagerplatz AS Position, SKU.Ueberlagerbereich

    FROM Artikel A 
        LEFT JOIN ArtikelBezeichnungen AB ON (A.Mandant=AB.Mandant AND A.HCode=AB.HCode AND A.Style = AB.Style AND AB.Sprache='DE')
        INNER JOIN ArtikelVariantenCSG CSG ON( A.Mandant= AB.Mandant AND A.HCode=AB.HCode AND A.Style = AB.Style)
        INNER JOIN ArtikelFarbcodes FC ON (CSG.Farbcode = FC.Farbcode AND CSG.Mandant=FC.Mandant)
        INNER JOIN ArtikelVariantenSKU SKU ON(SKU.Mandant=CSG.Mandant AND SKU.HCode=CSG.HCode AND SKU.Style=CSG.Style AND SKU.Farbcode=CSG.Farbcode)
        INNER JOIN ArtikelGroessencodes GC ON(GC.Mandant=SKU.Mandant AND GC.Groessencode= SKU.Groessencode)) AS QRY WHERE Name LIKE '%".$_POST["search"]."%'";

    $result = sqlsrv_query($conn, $sql);
    if($result === false) {
        die( print_r(sqlsrv_errors(), true) );
    }

    if(sqlsrv_num_rows($result) > 0){
        $output .='<h4 align="center">Search Result</h4>';
        $output .='<div class="table-responsive">
                        <table class="table table bordered">
                        <tr>
                            <th>Name</th>
                            <th>Farbe</th>
                            <th>Groesse</th>
                            <th>Position</th>
                        </tr>';
        while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
            $output .=  '<tr>
                            <td>'.$row["Name"].'</td>
                            <td>'.$row["Farbe"].'</td>
                            <td>'.$row["Groesse"].'</td>
                            <td>'.$row["Position"].'</td>
                        </tr>';
        }
        echo $output;
    }
    else{
        echo'Data Not Found';
    }


    sqlsrv_free_stmt($result);
?>

i added the sqlsrv drivers for a connection to MSSQL. So i can establish a connection to the SQL database. There is a problem how to achive a connection between $.ajax({}) and PHP.

thanks for your help

Upvotes: 2

Views: 575

Answers (1)

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26298

Change the following line:

data:search,

to

data:{
    search: search
}

and try again.

Explanation: data is an object having key : value pair in it. Like:

data: { key1: value1 }

and you can get its value in php like:

$data = $_POST['key1'];  // here `$data` contains `value1` in it

Upvotes: 1

Related Questions