We have 5 inputs (type="text") on html page /edit_person.php:
<input id="name" name="name" value="" />
<input id="surname" name="surname" value="" />
<input id="age" name="age" value="" />
<input id="begin" name="begin" value="" />
<input id="end" name="end" value="" />
And a php file /scripts/db.php
If we open in browser /scripts/db.php?get=1 it will give something like (an array with values):
$data = Array(
[name]=>Mark,
[surname]=>Twain,
[age]=>74,
[begin]=>November 30, 1835,
[end]=>April 21, 1910
)
How can we request /scripts/db.php?get=1 by ajax on /edit_person.php and insert received data to the inputs?
jQuery last version is used and PHP 5.2.
If input currently has some value, it should be replaced.
Output format of db.php can be changed, you can request options which would be better.
Thanks.
db.phpseems to be outputting the array in some custom, PHP-like format. I wouldn’t do that. Usejson_encode()instead: jQuery’s Ajax can handle JSON data natively.Docs on
jQuery.getJSON()with examplesIf your JSON data is in a variable named
data, the insertion into each input will be a simple$("#name").val(data.name);