0

First off sorry if I'm missing something simple just started working with AJAX today. I have an issue where I'm trying to get information from my database, but different records have different amounts of values. For instance, each record has a "features" column. In the features column I store a string. (ex: Feature1~Feature2~Feature3~Feature4... ) When I'm building the object I take apart the string and store all the features into an array. Some objects can have 1 feature others can have up to whatever. So... how do I return this values back to my ajax function from my php page? Below is my ajax function that I was trying and I'll provide a link with my php file. [ next.php : http://pastebin.com/SY74jV7X ]

$("a#next").click(function()
{
    $.ajax({
           type : 'POST',
           url  : 'next.php',
           dataType : 'json',
           data     : { nextID : $("a#next").attr("rel") },
           success  : function ( data ) {

                      var lastID = $("a#next").attr("rel");
                      var originID = $("a#next").attr("rev");

                      if(lastID == 1)
                      {
                        lastID = originID;  
                      }
                      else
                      {
                        lastID--;
                      }


                      $("img#spotlight").attr("src",data.spotlightimage);
                      $("div#showcase h1").text(data.title);
                      $("div#showcase h2").text(data.subtitle);
                      $("div#showcase p").text(data.description);
                      $("a#next").attr("rel", lastID);


                      for(var i=0; i < data.size; i++)
                      {
                          $("ul#features").append("<li>").text(data.feature+i).append("</li>");
                      }

                      /*
                      for(var j=1; j < data.picsize; j++)
                      {
                          $("div.thumbnails ul").append("<li>").text(data.image+j).append("</li>");
                      }
                      */                          
           },
           error    : function ( XMLHttpRequest, textStatus, errorThrown) {
                     $("div#showcase h1").text("An error has occured: " + errorThrown);
           }
    });
});
2
  • 1
    It'd help if you posted the HTML. Is the <a>'s rel the string 'Feature1~Feature2~Feature3~Feature4'? Commented Dec 24, 2010 at 23:47
  • so the <a>'s rel equals a number. (ex. "3") I then take that number and on line 13 of next.php I get the row from the database that's id corresponds to that number. The string 'Feature1~Feature2...' is gathered in the function used on line 16 of next.php. I get the string then use php's explode function to get the individual features and store them in the array '$features' seen on line 16 of next.php I figured this might be more helpful than the html. (if you really need html i can post) [entry class: pastebin.com/sLt10srM ] Thanks for taking the time to check out my problem! :) Commented Dec 24, 2010 at 23:59

1 Answer 1

3

First replace the below in your next.php file:

for ( $i=0; $i < $arraySize; $i++ )
{
    $return['feature'.$i.''] = $features[0];
}

With:

$return['features'] = $features;

P.S: the current code is wrong you should have ... = $features[$i]; anyway, you don't need that just send the array as is. and then in the JS part replace:

for(var i=0; i < data.size; i++)
{
    $("ul#features").append("<li>").text(data.feature+i).append("</li>");
}

With:

$.each(data.features, function(k,v){
    var li = '<li>' + v + '</li>';
    $("ul#features").append(li);
});

This way, don't need the data.size anymore.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply! Going to try reworking it now and will post back. I didn't think to pass it as an array ;(
Well it worked great. Only thing I had to add was $("ul.features li").remove(); So that it would remove the previous entries features from ul.features Thanks again !

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.