Типовое предложение: получите значение вместо значения и передайте идентификатор вместе с URL - PullRequest
0 голосов
/ 09 апреля 2019

У меня есть код PHP и Javascript, который я использую для функции поиска по типу заголовка, и он работает нормально. Но если вы посмотрите на приведенный ниже код, я передаю значение поиска, как я могу получить идентификатор и перейти к URL-адресу формы?

HTML

<form method="get" class="form-inline customers" autocomplete="off" action="/customers/<?php echo id; ?> ">       
<input class="customers" id="customers" type="text" name="customers">
<button type="submit" title="Search Customers"></button>
</form>

Javascript

    <script>
    $( document ).ready(function() {
    $('input.customers').typeahead({
    source: function (query, process) {
var countrycode = '<?php echo $agencyid; ?>';
    return $.get('fetch_customers.php', { query: query, cc: countrycode }, function (data) {
    data = $.parseJSON(data);
    return process(data);
    });
    },
    });
    });
    </script>

PHP

$countrycode1 = $_GET['cc'];
 $sql="SELECT
       first_name,
       last_name,
       id,
       status,
       agency_id
       FROM customers
       WHERE (agency_id = '$countrycode1') AND first_name LIKE '%".$_GET['query']."%' LIMIT 20";
   $resultset = mysqli_query($conn, $sql) or die("database error:". 
   mysqli_error($conn));
   $json = array();
   while( $rowscity = mysqli_fetch_assoc($resultset) ) {
   $json[] = $rowscity["first_name"];
   $json[] = $rowscity["id"];
    }
   $output = json_encode($json);
   echo $output;
   mysqli_close($conn);
    ?>

1 Ответ

0 голосов
/ 09 апреля 2019

Ваш PHP скрипт:

<?php

$countrycode1 = $_GET['cc'];
$sql = "SELECT
       first_name,
       last_name,
       id,
       status,
       agency_id
       FROM customers
       WHERE (agency_id = '" . $countrycode1 . "') AND first_name LIKE '%" . $_GET['query'] . "%' LIMIT 1";
$resultset = mysqli_query($conn, $sql) or die("database error:" . mysqli_error($conn));
$json = array();
while ($rowscity = mysqli_fetch_assoc($resultset)) {
    echo $rowscity["id"]; exit; 
}
?>

Ваш скрипт может иметь вид:

<script>
    $(document).ready(function () {
        $('input.customers').typeahead({
            source: function (query, process) {
                var countrycode = '<?php echo $agencyid; ?>';
                $.get('fetch_customers.php', {query: query, cc: countrycode}, function (data) {
                    $('form').attr('action', '/customers/' + data); 
                });
            },
        });
    });
</script>
...