简体   繁体   中英

How can I fetch data from MySQL in jQuery without a click of a button using an hidden field value?

I'm trying to fetch a user information from MySQL by passing the value of an hidden field to jQuery without needing to click on a button, just at an interval of every x-minutes.

What I have so far:

function getdetails() {
    var value = $('#userId').val();
    $.ajax({
        type: "POST",
        url: "getInfo.php",
        data: 'myinfo=' + value,
        success: function (data) {
            $("#info").val(data);
        }
    });
    setInterval(function () {
        getdetails()
    }, 1000);
};

getinfo.php

  $id = $_POST['myinfo'];
  $query = "select * from alumni_users where userId = '$id' ";
  $update = mysqli_query($mysqli, $query);

  while($row = mysqli_fetch_array($update)){
  .......
  }

0

Hi you can do it this way:

your php script:

if (isset($_POST["action"])) {
        $action = $_POST["action"];
        switch ($action) {
            case 'SLC':
                if (isset($_POST["id"])) {
                    $id = $_POST["id"];
                    if (is_int($id)) {
                        $query = "select * from alumni_users where userId = '$id' ";
                        $update = mysqli_query($mysqli, $query);
                        $response = array();
                        while($row = mysqli_fetch_array($update)){
                        .......
                        fill your response here

                        }
                       echo json_encode($response);
                    }
                }
                break;

        }
    }

Where action is a command you want to do SLC, UPD, DEL etc and id is a parameter

then in your ajax:

function getdetails() {
    var value = $('#userId').val();
   return $.ajax({
        type: "POST",
        url: "getInfo.php",
        data: 'myinfo=' + value
    })

}


call it like this:

getdetails().done(function(response){
var data=JSON.parse(response);
if (data != null) {
//fill your forms using your data
}
})

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM