i have an <input> box that requires user to enter their ID number. upon entering i want to print the associated name of that ID from mysql via JSON.
my question is, how do i pass the ID value in javascript/jquery
$('input#name-submit').on('click', function () { //get the ID input
var id = $('input#name').val(); // store it in this string
$.getJSON("fetch.php", {id: id}, function(data) { //pass the ID in fetch.php
$.each(data.member, function(key, value) {
$("#print").text(data.emp[key]["name"]);
});
});
my fetch.php (not the actual, but this is how i thought it goes)
if (isset['id'] == true {
require 'db.php';
$query = "select * from `members` where `member_no` = '[id]'";
$result = $db->($query);
$myArray = array();
while ($row = mysqli_fetch_array($result))
array_push($myArray, array('name' => $row [1]);
echo json_encode(array("result" => $myArray));
}
the missing piece im looking are
- how to pass the
idvalue to my fetch.php via javascript and query the associated name for that ID - correct logic syntax of my
fetch.phpto display the query into a JSON format, fetch the result and display it on$("#print")div.
$('input#name')exists.. also remove theinput.. to$('#name').val()