1

I know how to fill a single INPUT field with an ajax call like this. Suppose this is GO.PHP:

$qry = mysql_query("SELECT * FROM reviews WHERE id = ".$_GET['param']." "); 
while($review = mysql_fetch_array($qry)) {
    echo $review['description'];
}

When I click the following:

<a class="editlink" id="<?php echo $review['id']; ?>" href="#"><?php echo $review['title']; ?></a>

I call ajax like this:

$(".editlink").click(function() {
    $.get("go.php", {param: $(this).attr('id')}, 
        function(data) {
            $('textarea#area1').html(data);
        });    
    return false;
});

'data' is filled by 'echo $review["description"]' and passed to TEXTAREA which is populated correctly. Ok, up to now everything works. But I don't know how to populate two, three, four, etc fields with the same mySQL query. Suppose I also had:

<input type="text" name="title">
<input type="text" name="date">
<input type="text" name="author">

How do I have to pass 'data' (GO.PHP) to populate all the INPUT fields?

2
  • The same value, or different values from the same query? Commented Feb 6, 2012 at 15:30
  • Different values from the same query: title, date, description, etc Commented Feb 6, 2012 at 15:43

2 Answers 2

1

I would generate and send a JSON string with GO.PHP. The JSON string from GO.PHP would be something like:

 {"title" : "mytitle", "date" : "mydate", "author" : "myauthor"}

Then on the client side:

jsonOBJ = {};
$.ajax({
  type: "GET",
  url: "go.php",
  cache: false,
  success: function(data){
     jsonOBJ = jQuery.parseJSON(data);
     for (var key in jsonOBJ) {
       $("input[name=" + key + "]").val(jsonOBJ[key]);
     }
  },
});
Sign up to request clarification or add additional context in comments.

Comments

0

You can select mutiple elements in a range of different ways.

I´d recommend that you both wrap and add a class to the input elements you´d like to select. That way you´d be able to use a selector like this;

$('div#container-element input.customClass').html(data);

Other ways to select the elements;

Mutiple elements by IDs

$('#input1, #input2')

All input elements within an element

$('div#container-element input')

All input elements of type "text"

$('input[type="text"]')

Read the jQuery documentation about selectors for information and inspiration :)

1 Comment

Sorry but this does not answer my question. I know how to use selectors. I don't know how to do the following relation: db_record_1 => input_filed_1; db_record_2 => input_field_2. Please read again my post. Thank you.

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.