Buongiorno a tutti stò provando a fare un campo di ricerca ma il file php mi da il seguente errore:
Notice: Undefined index: search in C:\xampp\htdocs\fetch.php on line 7
però effettivamente search è definito, vi faccio vedere:
Questo è il file fetch.php, e nella query c'è il $_POST["search"]
questo invece è il file html al cui interno è definita la chiamata ajax di jquery e se guardate nello script in basso troverete la funzione $.ajax() con all'interno il valore data:{search:txt} che appunto indica search che viene mandato al file php (credo)Codice PHP:<?php
$connect = mysqli_connect('localhost', 'root','', 'testing');
$output = '';
$sql = "SELECT * FROM tbl_customer WHERE CustomerName LIKE '%".$_POST["search"]."%'";
$result = mysqli_query($connect, $sql);
if(mysqli_num_rows($result) > 0){
$output .= '<h4 align="center">Risultati della ricerca</h4>';
$output .= '<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>Customer Name</th>
<th>Addres</th>
<th>City</th>
<th>Postal Code</th>
<th>Country</th>
</tr>';
while ($row = mysqli_fetch_array($result)){
$output .= '
<tr>
<td>'.$row["CustomerName"].'</td>
<td>'.$row["Addres"].'</td>
<td>'.$row["City"].'</td>
<td>'.$row["PostalCode"].'</td>
<td>'.$row["Country"].'</td>
</tr>';
}
echo $output;
}
else{
echo 'nessun risultato per la ricerca selezionata';
}
?>
Codice HTML:<!DOCTYPE html> <html lang="it"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title> test ajax </title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src='js/bootstrap.js'></script> <link href='css/bootstrap.css' rel='stylesheet'/> </head> <body> <div class='container'> <br /> <h2 align='center'>Chiamata Ajax</h2> <div class="form-group"> <div class='input-group'> <span class='input-group-addon'>Search</span> <input type="text" name='search_text' id='search_text' placeholder="inserisci le lettere per avviare la ricerca" class="form-control" /> </div> </div> <br /> <div id='result'></div> </div> <script> $(document).ready(function(){ $('#search_text').keyup(function){ var txt= $(this).val(); if (txt != ''){ $.ajax({ url:'fetch.php', method:'post', data:{search:txt}, dataType:'text', seccess:function(data){ $('#result').html(data); } }); }else{ $('#result').html(''); } }); }); </script> </body> </html>
Vi ringrazio in anticipo per l'attenzione e dato che non sò effettivamente se sia un errore del file php se credete che io stia sbagliando l'interpretazione dell'errore perche è in jquery ditemelo, grazie.
Buona giornata a tutti.

LinkBack URL
About LinkBacks

