-1

this is my PHP function

$sql = "SELECT * FROM `users` WHERE `id` = '".$_SESSION['id']."'";
$result = mysql_query($sql, $db_connection);
$row = mysql_fetch_assoc($result);
$sql = mysql_query("INSERT INTO medrecherche VALUES('','$medname','{$_SESSION['id']}')");

And I need it to execute at the same time I'm clicking on a result from the search bow that I'm developing,

my js function is the one below :

function set_item(item) {
    // change input value
    $('#medicine_id').val(item);
    // hide proposition list
    $('#listmed').hide();
}

Called like that in index.php

<div class="input_container">
    <input type="text" id="medicine_id" onkeyup="autocomplet()" >                 
    <ul id="listmed"></ul>
</div>
Aya Salama
  • 1,458
  • 13
  • 17
  • 2
    So, what have you tried, where are you stuck? There should be tons of tutorials about AJAX out there... – Nico Haase Feb 14 '19 at 16:25
  • To add to that, remember that PHP runs on the server before the page is sent, it does not run on the client browser. So to run PHP you need AJAX or an iframe or reloading the entire page. – Dave S Feb 14 '19 at 18:27

1 Answers1

-1

hi you can do it this way:

$("#yourbuttonId").on('click',function(){

var medicineId = $("#medicine_id").val();
///you should validate medicine id here and then call your ajax
$.ajax({
        method: "POST",
        url: "myfile.php",
        data: { action: "INS", medicineId: medicineId }
                            })
                            .done(function( response ) {
                              //response from your php
                                console.log(response);
                            });

})

your php file:

   //do your search on a different ajax i guess
    if(isset($_POST["action"]) && $_POST["action"] == "SLC"){
    $sql = "SELECT * FROM `users` WHERE `id` = '".$_SESSION['id']."'";
    $result = mysql_query($sql, $db_connection);
    $row = mysql_fetch_assoc($result);
    echo json_encode($row,JSON_UNESCAPED_UNICODE);
    }
    if(isset($_POST["action"]) && $_POST["action"]== "INS" && isset($_POST["medicineId"])){
      $medname = $_POST[medicineId];
      //validate your medname i guess in this section then do your insert
    $sql = mysql_query("INSERT INTO medrecherche 
     VALUES('','$medname','{$_SESSION['id']}')");
    //and return a response if the insert was sucefull 
     echo "insert-ok"; 

}
stan chacon
  • 768
  • 1
  • 6
  • 19