简体   繁体   中英

Pass values from db to form with php + json

I successfully select some values with php and sql, which are passed to my application with JSON.

([{"id":"1","email":"mail@mail.org","county":"world","age":"6","gender":"unisex","poll":"option1"}]);

In my application i can select the values passed with jquery ajax:

var landmark = '<p>'+item.email+'<br>' + item.county+'<br>' + item.age+'<br>' + item.gender+'<br>' + item.poll+'</p>';

But instead of putting them into paragraphs, how can i just update the form with the values?

Using Jquery assuming your form has elements with the following ids:

$('#email').val(item.email);
$('#county').val(item.county);
$('#age').val(item.age);
$('#gender').val(item.gender);
$('#poll').val(item.poll);

Using straight javascript:

document.getElementById("email").value=item.email;
document.getElementById("county").value=item.county;
document.getElementById("age").value=item.age;
document.getElementById("gender").value=item.gender;
document.getElementById("poll").value=item.poll;

for radio buttons:

if (item.gender=="Male")
    $("#male).attr("checked", "checked");
else
    $("#female").attr("checked", "checked");

OR more concisely -- but less robust:

$("#"+item.gender).attr("checked", "checked");

OR in javascript:

document.getElementById(item.gender).checked = true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM