<?php require_once '../Connections/cmctrfdb.php'; ?>

<?php

// Connessione al database

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {

    die("Connessione al database fallita: " . $conn->connect_error);

}



// Esegui la query per cercare corrispondenze nel database

$searchQuery = $_POST['searchQuery'];

$idcompany = $_POST['idcompany']; // Assumi che sia già stato settato

$searchQuery = $conn->real_escape_string($searchQuery); // Prevenzione da SQL injection

$query = "SELECT * FROM contacts_td WHERE companyname LIKE '%$searchQuery%' AND companyid = $idcompany";

$result = $conn->query($query);



// Costruisci la risposta da inviare al client

if ($result->num_rows > 0) {

    while ($row = $result->fetch_assoc()) {

        echo '<div class="search-result" data-companyname="' . $row['companyname'] . '" data-address="' . $row['address'] . '" data-city="' . $row['city'] . '" data-cap="' . $row['cap'] . '" data-country="' . $row['country'] . '" data-telephone="' . $row['telephone'] . '" data-email="' . $row['email'] . '" data-fax="' . $row['fax'] . '" data-contactname="' . $row['contactname'] . '" data-manufacturermark="' . $row['manufacturermark'] . '">';

        echo '<strong>' . $row['companyname'] . '</strong><br>';

        echo 'Address: ' . $row['address'] . '<br>';

        echo 'City: ' . $row['city'] . '<br>';

        echo '</div>';

    }

} else {

    echo 'Nessun risultato trovato';

}



$conn->close();

?>

