Changing my src image with ajax getting location from db

How can I change img src from img using ajax. I have a save location in my database and I want to set img src from the value of something data[0]['patient_photo']I have html like this from my image:

<img id="checkup_pic" src="">

Here is my ajax code:

success: function(data) {
        $('#valfname').text(data[0]['patient_fname']);
        $('#valmname').text(data[0]['patient_mname']);
        $('#vallname').text(data[0]['patient_lname']);
        $('#valad').text(data[0]['patient_address']);
        $('#valcont').text(data[0]['patient_contact_info']);
        $('#valsex').text(data[0]['patient_sex']);
        $('#valcvilstat').text(data[0]['patient_civil_status']);
        $('#valbday').text(data[0]['patient_bday']);
        $('#valage').text(data[0]['patient_age']);
        $('#valheight').text(data[0]['patient_height']);
        $('#valweight').text(data[0]['patient_weight']);

        $('#checkup_pic').attr("src","");
+4
source share
3 answers

You need to use the attr jquery property and pass the src value to the value.

Try entering the code:

$('#checkup_pic').attr("src",data[0]['patient_photo']);
+6
source

jQuery prop() attr(), DOM:

prop(): .

attr(): .

, data[0]['patient_photo'] src img, #checkup_pic, :

$('#checkup_pic').prop("src", data[0]['patient_photo']);
//Or
$('#checkup_pic').attr("src", data[0]['patient_photo']);

, .

+6

Try the following:

$.ajax({
    url: url, // url of your file from your data is being fetched like index.php
    type: 'post',
    success: function(data) {
        $("#checkup_pic").attr('src', data[0]['patient_photo']);
    }
})
+2
source

Source: https://habr.com/ru/post/1660045/


All Articles