0

I'm trying to pull the data gathered from the MySQL query referenced below and populate the corresponding textboxes with that data:

<?php
header('Content-type: application/json');

$server = "••••";
$username = "••••";
$password = "••••";
$database = "••••";

$username_pull = $_POST['a_username'];

$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());
mysql_select_db($database, $con);

$sql = "SELECT uname, pword, fullname, phone_num, email_add, car_details FROM app_login WHERE uname = '$username_pull'";
$result = mysql_query($sql) or die ("Query error: " . mysql_error());

$records = array();

while($row = mysql_fetch_assoc($result)) {
    $records[] = $row;
}

mysql_close($con);

echo $_GET['jsoncallback'] . json_encode($records);

?>

If I insert in $username_pull = "admin", I get the following json info, so I know it works:

[{"uname":"admin","pword":"password","fullname":"John Smith","phone_num":"123-456-7890","email_add":"[email protected]","car_details":"2013 Black Cadillac ATS"}]

Here's my json callback function:

function fillTheGap() 
{
    var f_uname = $("#a_username").val();

    $.ajax({
        url: "login2.php",
        data: f_uname,
        dataType: "jsonp",
        jsonp: "jsoncallback",
        success: function(data) 
        {
                $("#a_phonenum").val(data.phone_num);
            $("#a_fullname").val(data.fullname);
            $("#a_email").val(data.email_add);
            $("#a_cardetails").val(data.car_details);
            },});
}

Here's the form that initiates the data pulling:

    <form name="check_user" method="" action="post">
        Your Username: 
        <input id="a_username" name="a_username" type="text" onBlur="fillTheGap()">
        Your Full Name: 
        <input id="a_fullname" name="a_fullname" type="text">
        Your Phone Num:
        <input id="a_phonenum" name="a_phonenum" type="text">
        <input name="submit" type="button" value="Submit">
</form>

How do I make it to where it populates the textboxes with its corresponding ID name?

I greatly appreciate everyone's help in advance!

2
  • "same ID" how exactly, you can only use an ID once, it's unique? Also, your database functions are wide open to XSS attacks! Commented May 9, 2013 at 20:23
  • You're right. I'll take the necessary precautions to avoid those problems. And you're right about the ID's. I meant to try doing this with classes instead. Commented May 9, 2013 at 20:39

1 Answer 1

1

Try something like this:

data = {"uname":"admin","pword":"password","fullname":"John Smith","phone_num":"123-456-7890","email_add":"[email protected]","car_details":"2013 Black Cadillac ATS"}

$.each(data, function(key, value){
       $("#a_"+key).val(value);
});

So your function should be something like:

function fillTheGap() 
{
    var f_uname = $("#a_username").val();

    $.ajax({
        url: "login2.php",
        data: f_uname,
        dataType: "jsonp",
        jsonp: "jsoncallback",
        success: function(data) 
        {
              $.each(data, function(key, value){
                 $("#a_"+key).val(value);
              });
        }
    });
}

Also you can try it here: http://jsfiddle.net/nhKks/

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.