1

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 id value to my fetch.php via javascript and query the associated name for that ID
  • correct logic syntax of my fetch.php to display the query into a JSON format, fetch the result and display it on $("#print") div.
2
  • it should work... check your browsers network tab to inspect the request sent Commented Sep 27, 2013 at 7:44
  • also make sure that the element $('input#name') exists.. also remove the input.. to $('#name').val() Commented Sep 27, 2013 at 7:45

1 Answer 1

2

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']."'";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.