1

I want to add users present in a given table. I am iterating whole table and sending each value to javascript file.

<?php 
          $sql = "select * from user where user_id not in (select second_user_id from friends where first_user_id = '$user_id'\n"
    . " union\n"
    . " select first_user_id from friends where second_user_id = '$user_id') limit 20";


              $result_not_friends=mysqli_query($dbc,$sql)
              or die("error in fetching");

            // print_r($row_not_friends);

           ?>
           <table class="table table-hover table-bordered">
            <h1>Users</h1>
              <tbody>
                <?php 
                  while ( $row_not_friends = mysqli_fetch_array($result_not_friends))
                  {
                    if ( $row_not_friends['user_id'] != $user_id )
                    {
                 ?>
                  <tr>
                    <td>
                      <?php echo $row_not_friends['user_name']; ?>
                    </td>
                    <!-- here I am sending request and processing it via ajax -->
                    <td><i class="fa fa-user-plus send_request"></i></td>
                    <input type="hidden" class="send_second" value="<?php echo $row_not_friends['user_id']; ?>">
                    <input type="hidden" class="send_first" value="<?php echo $user_id; ?>">
                  </tr>

                  <?php 
                    }
                    }
                   ?>
              </tbody>
          </table>

Now I am accessing each value in a javascript file as follow: // Here a request is send

$('.send_request').on('click',
    function (e) {
        e.preventDefault();

        var first = $('.send_first').val();
        var second = $('.send_second').val();
        alert('firt id is ' + first);
        alert('second id is ' + second);
        $.ajax(
        {

            url:'send_process.php',
            type:'POST',
            dataType:"json",
            data: { first: first, second: second },
            success:function(data)
            {
                if(data.send_success)
                {
                  window.location.href = "friend.php";

                }
                else
                {
                    alert("something went wrong");
                    window.location.href = "friend.php";
                }

            },
            error : function() { console.log(arguments); }
        }
            );
    }); 

But here var second = $('.send_second').val(); gives only top-most element value of $row_not_friends['user_id'] . When I am echoing the value, it gives correct result. Please help me.

1 Answer 1

1

Because you are selecting ALL the elements in the page and the default behavior of val() is it returns the first item. It has no clue you want the nth item.

First thing is you need to fix your HTML it is invalid. You can not have an input as a sibling of a tr element. You need to move it inside of a TD.

                <!-- here I am sending request and processing it via ajax -->
                <td><i class="fa fa-user-plus send_request"></i> <!-- removed the closing td from here  -->
                <input type="hidden" class="send_second" value="<?php echo $row_not_friends['user_id']; ?>">
                <input type="hidden" class="send_first" value="<?php echo $user_id; ?>"></td>  <!-- moved the closing td to here  -->
              </tr>

You need to find the elements in the same row as the button you clicked. Since the hidden inputs are npw siblings of the button you can use the siblings() method.

var btn = $(this);
var first = btn.siblings('.send_first').val();
var second = btn.siblings('.send_second').val();
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for introducing .siblings() to me! Now I can finally write that .parent().child() stuff in a nicer way
Your HTML is invalid that is why!
How can I make it valid?
Perfect, It worked.. You deserve an oscar :P . I can't upvote, it needs 15 reputation..
But you can mark it as an answer since it is your question and get the points to allow you to vote in the future.

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.