Pass a href value to ajax

dldeef67  于 2022-10-22  发布在  PHP
关注(0)|答案(1)|浏览(144)

I am trying to pass a href value to ajax function, how do i do? my a href is inside while loop. i can pass a href value going to next page, but i want to stay in same page while click on a href.

<?php
                                $sql ="SELECT * FROM user";
                                $query =mysqli_query($connection, $sql);
                                if($query->num_rows>0){
                                        while( $row =$query->fetch_assoc()){
                                            $id =$row['id'];
                                            $fname =$row['fname'];
                                            $first_character = substr($fname, 0, 1);   
                                            $lname =$row['lname'];
                                            $last_character =substr($lname, 0, 1);
                                            $pass =$row['password'];
                                            $online=$row['online_offline'];
                                            ?>
                            <a href="index.php?id=<?=$id;?>" class="list-group-item list-group-item-action border-0">
                                <div class="d-flex align-items-start">
                                    <!-- <img src="https://bootdey.com/img/Content/avatar/avatar3.png"
                                        class="rounded-circle mr-1" alt="Jennifer Chang" width="40" height="40"> -->
                                   <span class="circle"><?=$first_character.$last_character;?></span>
                                    <div class="flex-grow-1 ml-3">
                                        <?=$fname.' '.$lname;?>
                                        <?php if($online =='1'){ ?>
                                        <div class="small"><span class="fas fa-circle chat-online"></span> Online</div>

                                        <?php }else{?>
                                        <div class="small"><span class="fas fa-circle chat-offline"></span> Offline
                                        </div>

                                        <?php } ?>

                                    </div>
                                </div>
                            </a>

                            <?php }}
ugmeyewa

ugmeyewa1#

<a class="test_a" href="https://google.com">Press</a>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<script>
    $('.test_a').click(function(event){
        var hrefval = $(this).attr('href');
        $.ajax({
                type: 'GET',
                async: false,
                url: hrefval,
                success: function(response)
                {
                    // DO something with the response
                }
            });
        event.preventDefault();
    })
</script>

相关问题