1

I am using AJAX in order to access data from a php file. I have problem with the format of retrieved data from database, please help.

So, this is my ajax function splice. It retrieves data from find_account.php

function processReqChange() { 
    // only if req shows "loaded" 
    if (req.readyState == 4) { 
        // only if "OK" 
        if (req.status == 200) { 
            form_prof.prof_id.value = req.responseText;
    form_prof.prof_name.value = req.responseText;
    form_prof.prof_username.value = req.responseText;
    form_prof.prof_password.value = req.responseText; 
        } 
        else { 
            alert("Problem in retrieving the XML data:\n" + req.statusText); 
        } 
    } 
}

find_account.php

<?php

include("connect.php");
session_start();

$account = $_GET['account'];
$query = "SELECT * FROM profs WHERE profs_name = '".$account."'";
$result = mysql_query($query);
$num = mysql_num_rows($result);

if(empty($num))
{
echo 'DATA NOT FOUND';
}
else
{
    $arr = mysql_fetch_array($result);
    $id = $arr['profs_number'];
$name = $arr['profs_name'];
$username = $arr['profs_username'];
$password = $arr['profs_password'];
}

header("Content-type: text/plain");
echo $id;
echo $name;
echo $username;
echo $password;
?>

and I have 4 input boxes in my HTML from where the req.responseText puts the value

and everytime I search the name in the input field for example:

Search: [ Dorothy Perkins ]

The output goes like [id,name,username,password]:

[20111Dorothy [email protected]] [same with 1st field] [same] [same] 

Wherein I want it to be like...

[20111]  [Dorothy Pekins]  [[email protected]]  [123456]

Where [ ] are input fields. Please help me arrange my format, I am so confused. I am new to this.

7
  • This is wiiide open to an SQL injection attack! Commented Feb 6, 2014 at 8:09
  • instead of mysql use mysqli or PDO also you should use json_encode to send the data back Commented Feb 6, 2014 at 8:15
  • @Strawberry why? and how can I avoid that?? Commented Feb 6, 2014 at 8:29
  • @Perry what is the difference between mysql and mysqli? thanks for json_encode tip. I'll try to use that. Commented Feb 6, 2014 at 8:30
  • @PaoloLambojon mysql is deprecated as of PHP 5.5.0 see the manual php.net/manual/en/intro.mysql.php Commented Feb 6, 2014 at 8:35

3 Answers 3

2

You can encode return values in json before sending back. In PHP

<?php

include("connect.php");
session_start();

$account = $_GET['account'];
$query = "SELECT * FROM profs WHERE profs_name = '".$account."'";
$result = mysql_query($query);
$num = mysql_num_rows($result);

if(empty($num))
{
$returnValues = 'DATA NOT FOUND';
}
else
{
    $arr = mysql_fetch_array($result);
    $returnValues = json_encode($arr);
}

echo $returnValues;
?>

In Javascript

function processReqChange() { 
    // only if req shows "loaded" 
    if (req.readyState == 4) { 
        // only if "OK" 
        if (req.status == 200) { 
           req = JSON.parse(reg);

            form_prof.prof_id.value = req.id;
    form_prof.prof_name.value = req.name;
    form_prof.prof_username.value = req.username;
    form_prof.prof_password.value = req.password; 
        } 
        else { 
            alert("Problem in retrieving the XML data:\n" + req.statusText); 
        } 
    } 
}
Sign up to request clarification or add additional context in comments.

1 Comment

I don't know why but this solution doesn't seem to work, the req.readyState turns to 1 or 'load', it doesn't complete. Thanks, though.
1

You have to write the data in some format from your PHP code (XML, json, or simply separate the values with a comma), and parse it from your javascript.

For example, in PHP:

echo $id . "," . $name . "," . $username . "," . $password;

And then in the javascript:

values = req.responseText.split(",");
form_prof.prof_id.value = values[0]
form_prof.prof_name.value = values[1];
form_prof.prof_username.value = values[2];
form_prof.prof_password.value = values[3];

Of course you may have to do something more complicated if the values may contain a comma.

1 Comment

you better use json or something don't send just plain text or html back when it is easier to use json
0

You can try this

$account = $_GET['account'];
$query = "SELECT * FROM profs WHERE profs_name = '".$account."'";
$result = mysql_query($query, MYSQLI_STORE_RESULT);
while($arr = $result->fetch_array(MYSQLI_ASSOC)) {
    $returnValues = json_encode($arr);
    break;
}

echo $returnValues;

Note that column names are used as associative index for $arr

Hope it works.

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.