Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Echo JS onclick fill function AJAX tidak berfungsi

Anda dapat menggunakan tanda ` sebagai ganti tanda kutip tunggal dan ganda untuk javascript.

Di sini Anda hanya perlu memperbarui baris 16 di ajax.php file seperti ini.

echo "<li onclick='fill(`".$Result['Name']."`)'>".$Result['Name']." 

Kode lengkapfile ajax.php

<?php
//Including Database configuration file.
include "db.php";
//Getting value of "search" variable from "script.js".
if (isset($_GET['search'])) {
//Search box value assigning to $Name variable.
$Name = $_GET['search'];
//Search query.
$Query = "SELECT Name FROM search WHERE Name LIKE '$Name%' LIMIT 5";
//Query execution
$ExecQuery = MySQLi_query($con, $Query);
//Creating unordered list to display result.
    if ($ExecQuery->num_rows > 0) {
          echo "<ul>";
          while ($Result = MySQLi_fetch_array($ExecQuery)) {
            echo "<li onclick='fill(`".$Result['Name']."`)'>".$Result['Name']." 
     </li>";
     }
    echo "</ul>";
   }
  }
 die();
?>

Kode JS.

//Getting value from "ajax.php".
function fill(Value) {
//Assigning value to "search" div in "index.php" file.
$('#search').val(Value);
//Hiding "display" div in "index.php" file.
$('#display').hide();
}
$(document).ready(function() {
//On pressing a key on "Search box" in "indexd.php" file. This function will be called.
$("#search").keyup(function() {
//Assigning search box value to javascript variable named as "name".
$('#display').hide();
$('#backspace').css("display", "none");
var name = $('#search').val();
//Validating, if "name" is empty.
if (name == "") {
   //Assigning empty value to "display" div in "index.php" file.
   $('#backspace').css("display", "block");
}
//If name is not empty.
else {
    //AJAX is called.
    $.ajax({
        //AJAX type is "GET".
        type: "GET",
        //Data will be sent to "ajax.php".
        url: "ajax.php",
        //Data, that will be sent to "ajax.php".
        data: {
            //Assigning value of "name" into "search" variable.
            search: name
        },
        //If result found, this funtion will be called.
        success: function(html) {
          if (html == '<ul><li>No Result Found!</li></ul>') {
             $('#no-results').css("display", "block");
          }else{
             //Assigning result to "display" div in "index.php" file.
             $("#display").html(html).show();
           }
        }
    });
   }
 });
});



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. MySQL INSERT berfungsi dengan baik seperti yang didokumentasikan

  2. Menambahkan nol ke array jika nilai kosong terjadi dalam array

  3. XAMPP - MySQL mati secara tidak terduga

  4. XAMPP/MySQL:tidak dapat membuka file tablespace tabel tunggal .\mysql\innodb_index_stats.ibd setelah restart MySQL

  5. Query untuk menghitung jumlah jarak (bujur, lintang) di baris berturut-turut di Mysql