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
id
value to my fetch.php via javascript and query the associated name for that IDfetch.php
to display the query into a JSON format, fetch the result and display it on $("#print")
div.Replace
if (isset['id'] == true {
with
if (isset($_GET['id'])) {
-----^^^^^^^^^^^^---
OR
if (isset($_REQUEST['id'])) {
-----^^^^^^^^^^^^^^^---
You are using if (isset['id'] == true {
which is wrong if condition to check if passed value isset
or not you need to use superglobal
i.e $_GET,$_POST,$_REQUEST
Same for MySQL query you are using ['id']
which behave as normal string instead you must use supergloabal as described above.
$query = "select * from `members` where `member_no` = '".$_REQUEST['id']."'";
OR
$query = "select * from `members` where `member_no` = '".$_GET['id']."'";