2

I'm having a small college project about discussion room service. Right now I'm being tasked to implement autocomplete feature of name that orders it. I already google some tutorials. I'm not sure what went wrong, when i try to type a name, there's no data being typed ahead. Here's my form code:

<?php
$host = "localhost";
$user = "root";
$pass = "";
$name = "pinjamruang";
$koneksi = mysqli_connect($host, $user, $pass, $name);

//Periksa apakah koneksi berhasil
if(mysqli_connect_errno()){
    echo "Error: ";
    echo mysqli_connect_error();
    echo "<br   /> Error Code: ";
    echo mysqli_connect_errno();
    die();
}
$sql = "SELECT * FROM ruangan
        WHERE id = $_GET[id]";
$hasil = mysqli_query($koneksi,$sql);
$row = mysqli_fetch_assoc($hasil);
$sql2 = "SELECT * FROM shift
        WHERE id = $_GET[shift]";
$hasil2 = mysqli_query($koneksi,$sql2);
$row2 = mysqli_fetch_assoc($hasil2);
?>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
  $(function() {
    $( "#typeahead" ).autocomplete({
      source: 'typeaheads.php';
    });
  });
</script>
<h1> Konfirmasi Pemesanan Ruang <?php echo $row['kode']; ?></h1><br>
<form class="form-horizontal" action="process/process-order-ruang.php" method="post">
    <div class="form-group">
        <label for="inputNamaPemesan" class="col-sm-2 control-label">Nama</label>
        <div class="col-sm-10">
            <input type="text" name="nama_pemesan" class="form-control" id="typeahead" placeholder="Nama Pemesan">
        </div>
    </div>
    <div class="form-group">
        <label for="inputKeperluan" class="col-sm-2 control-label">Keperluan</label>
        <div class="col-sm-10">
            <select name="keperluan" class="form-control" id="inputKeperluan">
                <option value="Diskusi Belajar">Diskusi Belajar</option>
                <option value="Diskusi Tugas">Diskusi Tugas</option>
                <option value="Dokumentasi">Dokumentasi</option>
                <option value="Lain-lain">Lain-lain</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label for="inputWaktu" class="col-sm-2 control-label">Waktu</label>
        <div class="col-sm-10">
        <input type="text" class="col-sm-5" name="waktu" value="<?php $row2['shift'];
                        $timestamp = strtotime($row2['shift']);
                        $waktuk = date('H.i A', $timestamp);
                        $int = (int)$waktuk;
                        echo $int; ?>:00" disabled> - <input type="text" class="col-sm-5"value="<?php $row2['shift'];
                        $timestamp = strtotime($row2['shift']);
                        $waktuk = date('H.i A', $timestamp);
                        $int = (int)$waktuk;
                        echo $int+2; ?>:00" disabled>
        </div>
    </div>
    <?php $shift = $_GET['shift'];
          $ruangan = $_GET['id'];?> 
    <input type="hidden" value="<?php $int2 = (int)$shift;?>" name="shift">
    <input type="hidden" value="<?php $int3 = (int)$ruangan;?>" name="ruangan">
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-primary">Pesan</button>
        </div>
    </div>
</form>

and here is my code which should return json data from my table

<?php
$host = "localhost";
$user = "root";
$pass = "";
$name = "pinjamruang";
$koneksi = mysqli_connect($host, $user, $pass, $name);
//connect with the database

//get search term
$searchTerm = $_GET['term'];
//get matched data from table
$query = $koneksi->query("SELECT * FROM user 
                          WHERE nama LIKE '%".$searchTerm."%' ORDER BY nama ASC");
while ($row = $query->fetch_assoc()) {
    $data[] = $row['nama'];
}
//return json data
echo json_encode($data);
?>

Any help would be much appreciated. Thanks a lot!

8
  • where is your ajax Commented May 15, 2018 at 6:43
  • You should use $_GET[ ' getid ' ] to retrieve the value, not $_GET[anyid] Commented May 15, 2018 at 6:44
  • Um, sorry.. What ajax? @RahulShrivastava I'm using this guide -> (codexworld.com/autocomplete-textbox-using-jquery-php-mysql) Commented May 15, 2018 at 6:50
  • @SebinPJohnson, In my first input text, i already write it's id to typeahead Commented May 15, 2018 at 6:52
  • What is the problem? Commented May 15, 2018 at 6:55

1 Answer 1

1

Use below code in script. remove semicolon from source. You can use colon for other parameters.

<script>
    $(function() {
        $( "#typeahead" ).autocomplete({
            source: 'typeaheads.php'
        });
    });
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

I used this code, and it is working fine. you can check your db column name. "nama". it may be column name is "name" in the db.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.