Home > Software engineering >  Pass a href value to ajax
Pass a href value to ajax

Time:10-28

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;?>" >
                                <div >
                                    <!-- <img src="https://bootdey.com/img/Content/avatar/avatar3.png"
                                         alt="Jennifer Chang" width="40" height="40"> -->
                                   <span ><?=$first_character.$last_character;?></span>
                                    <div >
                                        <?=$fname.' '.$lname;?>
                                        <?php if($online =='1'){ ?>
                                        <div ><span ></span> Online</div>

                                        <?php }else{?>
                                        <div ><span ></span> Offline
                                        </div>

                                        <?php } ?>

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

                            <?php }} 

CodePudding user response:

<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>
  • Related