Hi i have a very tiny problem. I am using ajax call to fetch values onblur of customer id. I am able to get one value in form but i want multiple values to fill form fields. my partial code is as follows:
req.onreadystatechange = processReqChange;
req.open("GET", url + '?mob=' + number, true);
req.send(null);
function processReqChange() {
if (req.readyState == 4) {
if (req.status == 200) {
document.getElementById('oper').value = req.responseText;
}
}
}
My php code for sending the data:
$f = fopen('file.csv', "r");
$result = false;
while ($row = fgetcsv($f)) {
if ($row[0] == $mob) {
$result = $row[1];
break;
}
}
fclose($f);
echo $result;
How should i send multiple values like $row[2], $row[3] to fill form fields like username, email etc. Please advice.
The standard way would be to have PHP return a JSON string and parse that in JS.
Example in PHP (nothing dynamic here):
echo '{"key1":"value1", "key2":"value2", "key3":"value3"}';
Then using that in JS:
var result = JSON.parse(req.responseText);
// now you can use the values returned by PHP
alert(result.key1);
For more info on how to produce JSON with PHP: http://www.php.net/manual/book.json.php
Please note that this example is highly primitive. You might want to add more JSON sanity/validity checks in the JS code and, of course, a more elegant PHP generation.