I’m looking to retrieve a user’s avatar using their user name, retrieve it in an input field. However what I did does not work and I am not familiar with Ajax. Could someone help me and explain the procedure to me?
<input type="text" name="username" class="input" placeholder="Your username">
<img id="#result" src=""></img>
Here is my ajax
$(document).keyup(function (event) {
$.ajax({
url: "App/Actions/PlayerGetFigure.php",
type: "post",
data: {
login: function () {
return $(':input[name="username"]').val();
},
},
success: function (data) {
$('#result').html(data);
}
});
});
And here is my PHP
require '../../vendor/autoload.php';
use AppModelsUsersManager as Users;
$Users = new Users();
$username = $_POST['username'];
if (isset($username)) {
$user = $Users->getByUsername($username);
if ($user) {
echo $user['avatar'];
} else {
return false;
}
}
2
Answers
If this is your HTML:
I would advise the following jQuery.
This assumes that the PHP Script will return a (Relative or Absolute) URL Path to the Image.
I would personally take this approach, it looks a bit cleaner for me (assuming that
$user['avatar']
returns the path to the image)HTML
AJAX
PHP