vendredi 10 février 2017

How can I get data from mysql via Ajax?

Vote count: 0

I want to get some data via Ajax, but there must be some mistake because the expected result is empty:

script:

   <script>
    function showUser(value) {
        var values = $(this).serialize();
        $.ajax({
        url: "test.php",
        data: {
            id: value
        },
        type: "POST",
        success: function(data){
            $("#result").html(data);
          }
    })
    }
    </script>

html:

    <form>
    <select name="users" onchange="showUser(this.value)">
      <option value="">Select a person:</option>
      <option value="1">Peter Griffin</option>
      <option value="2">Lois Griffin</option>
      <option value="3">Joseph Swanson</option>
      <option value="4">Glenn Quagmire</option>
      </select>
    </form>
    <br>
    <div id="result"></div>

test.php

$id = @$_POST['id'];


    $pdo = $db->query('SELECT * FROM people WHERE id = "'.$id.'"');
    while ($row = $pdo->fetch(PDO::FETCH_ASSOC)) {
       echo  $row['id'];
    }                             
    ?>

asked 17 secs ago

Let's block ads! (Why?)



How can I get data from mysql via Ajax?

Aucun commentaire:

Enregistrer un commentaire