0

Ok , I am trying to get different id values through Jquery , and pass them to Jquery AJAX that will hit a PHP file so I can get some data back .... I'm not sure how to get all the multiple different ids because Jquery is only getting the first id of many of the unique id values generated by the while loop .. and I would like each unique ID to also be passed to the AJAX function in Jquery .. Your help would be so much appreciated . I'm still new to the Jquery world

<?php
require('../database/connection.php');
?>
        <script type="text/javascript">
          jQuery(document).ready(function() {
           var ID = $('div#opposition img').attr("id"); alert(ID);
           $.ajax({
            type:'GET',
            url :'get_users_images.php',
            data:'screen_name='+ ID,
            success: function(result){
             $('div#opposition img').attr('src', result);
           }
         });

         });

    </script>


    <?php
    $select2  = "SELECT * FROM AUTHORS WHERE ID <> $id";   
    $result2 = mysql_query($select2);
    $result_count = mysql_num_rows($result2);
    echo '<div id ="opposition">';
    while ($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) { 

    echo "<img id ='".$row2['Twitter']."' src='images/ajax-loader.gif' class ='image".$row2['Twitter']."'/>"; //  echos different ids, 
    }
     ?>
    </div>
1
  • 1
    are you getting set of images from ajax return. you could check with Network tag in firefox or chrome debugging. Commented Jun 16, 2013 at 21:52

2 Answers 2

0

You can send an stringified array of id's like this -

jQuery(document).ready(function () {
    var ID = $('div#opposition img').map(function(){
       return this.id;
    }).get();

    $.ajax({
        type: 'GET',
        url: 'get_users_images.php',
        data: { screen_name : JSON.stringify(ID)},
        success: function (result) {
            $('div#opposition img').attr('src', result);
        }
    });
});
3
  • is there a way I could pass each one of them to AJAX instead of putting them all into a string ? because each ID gets processed by the twitter API service which is in PHP .. would that be quicker ?
    – Stack One
    Commented Jun 16, 2013 at 22:01
  • 1
    @StackOne Then you will end up with multiple http request's equal to the number of Id's, which seem's inefficient , instead you can send all id's and process each id in a function may be inside loop (don't know much about php though) Commented Jun 16, 2013 at 22:07
  • You know what.. I am thinking that's the way to go with twitter .. Could you please tell me how I could pass each one of these IDS to AJAX instead of putting them into an array ? I'd greatly appreciate it.
    – Stack One
    Commented Jun 16, 2013 at 22:20
0

If I am correct, Actually , why you are placing the set of images returned by php in one img tag, $('div#opposition img').attr('src', result); . Rather I think you must do something like $('div#opposition').innerHTML(result) .

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.