-2

Here is my code:

 var myVariable = "<div>";

 for (i = 1; i < 5; ++i) {
 myVariable += '<div class="user' + i + '">'; 

 $.ajax({
      type: "POST",
      data: "uziv=" + i,
      url: "../php/ajax_objednavky.php",
      success: function(data){
      //alert(data);  work well
      //$('#content').html(data); work well
      myVariable += data; //doesn't work
      }              
    });

 myVariable += '</div>';

 } 

 myVariable += "</div>";
 $('#myDiv').html(myVariable);

PHP file looks like

<? echo "data from mysql"; ?>

I can view the data using alert, but how can I add data to a variable?

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114

3 Answers3

0

Edited.

It is probably because the ajax request take some time to finnish. By the time the first result gets back, the JS is allready finnished.

This code might be improved, but I hope you get what I mean.

var myVariable = "<div>";

 for (i = 1; i < 5; ++i) {
 myVariable += '<div class="user' + i + '">'; 

 $.ajax({
      type: "POST",
      data: "uziv=" + i,
      url: "../php/ajax_objednavky.php",
      success: function(data){
        myVariable += data; //doesn't work
        myVariable += '</div>';

        if(i == 4){
          myVariable += "</div>";
          $('#myDiv').html(myVariable);
        }
      }              
    });
 } 

Instead of sending every i, you could send all of them in on ajax-request as json and get all result back in one request. This will need some reconstruction of your service, but It might be the better way of doing this.

DannyThunder
  • 994
  • 1
  • 11
  • 29
0

I'm surprised by your post :

data: "ter="+ objednavky + "&uziv=" + uzivatel,

I always used data: { ter: objednavky, uziv: uzivatel }

Easier to read, IMHO.

then, What do you mean by "doesnt work ?"

try this :

 myVariable += data;
 alert("foo");

Do you see the foo alert ? (I would expect that you messed up with "" and it is not PRINTED on the div, but it "works"). Exemple : ""

mansuetus
  • 782
  • 5
  • 17
-1

I guess that is what you need because there is no need to concatenate the data with myVaraible.

Updates:

The reason, the ajax data is returning an object. So concatenating will be not a good one. So I guess you might have data.something. Check this in such a way.

In-case if it is a object, then the concatenation output will be

<div>[object Object] 
Praveen
  • 55,303
  • 33
  • 133
  • 164
  • myVariable is simple variable – iJade Oct 17 '13 at 08:47
  • @user2887469 Agree@iJay , it won't work. Can you provide more details. What are you actually trying? Do you want the data to shown inside the div (myVariable) – Praveen Oct 17 '13 at 08:57
  • @user2887469 The reason, the ajax `data` is returning the object. So concatenating will be not a good one. So I guess you might have `data.something`. Check this in such a way. If it is a string it would have worked http://jsfiddle.net/VAyNX/ – Praveen Oct 17 '13 at 09:11
  • @user2887469 what is your `data` have? – Praveen Oct 17 '13 at 09:14