0

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

([{"id":"1","email":"[email protected]","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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

ah - and the selector is the id of the input?
thanks! what about the radio buttons, where each input has an id?
cant get the radio to work - in the javascript, han can it see which input to check? as i see it you only get the id with the variable?
I assume you have 2 radio buttons. One with id="male" another with id="female". If item.gender="male" then $('#'+item.gender) will select the male radio button. Similarly with the female radio input. Does that make sense?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.