Dsiplay results from database using ajax and php code

it’s 5 days i search How i display a simple adresse from my database Mysql when i select a Name from a dropdown list.
I tried to use ajax but i don’t know where i will insert the javascript code(in a file existed or creat a new file).
this my php code :

<select class='Nom' onchange="showUser(this.value) id=Nom>
<option value="">--- Select ---</option>

    [insert_php]
    $servername = "localhost";
    $username = "root";
    $password = "";
    $conn = new mysqli($servername, $username, $password, "somapam_bd");

     $sql = mysqli_query($conn, "SELECT Nom FROM herboristes");

    while($ligne_liste=mysqli_fetch_array($sql)) {
        echo '<option value="'.$ligne_liste['Nom'].'">'.$ligne_liste['Nom']."</option>n";
    }
    echo '</select>';
    [/insert_php]
        <br>
        <div id="txtHint"><b>Adresse info will be listed here.</b></div>

this is the code to display result (i insert it in the file : function.php) :

add_action( 'wp_ajax_my_action', 'showUser' );
add_action( 'wp_ajax_nopriv_my_action', 'showUser' );

     function showUser() {
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password, "somapam_bd");


  $q = intval($_GET['q']);


  $query = "select Adresse, Nom from herboristes where Nom = '".$q."'";

  $result = mysqli_query($query);
  while($row = mysqli_fetch_assoc($result)) {
echo $n = $row['Adresse'];


}
   }

in the end i found some javascript files and i don’t know how i insert it in my page.

Please someone can help and explaine to me every step to make this project.
Thank you

Read more here: Dsiplay results from database using ajax and php code

Leave a Reply

Your email address will not be published. Required fields are marked *