2

i want to change div value when clicking php while loop., My php code

<?php $query = mysql_query("select * from tbl_sub_product where product_id='$id'"); 
           while($row=mysql_fetch_array($query))
          {    ?>  
<div><a href="#" class="showElement" id="showElement" >
<?php echo $row['name']; ?><?php echo $row['id']; ?></a></div>
                                <?php } ?>  

my query was

<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
var $target = $('#target');
$(".showElement").click( function(e) {
    e.preventDefault();
    $target.append( $(this).text() );
});
});
</script>

i want the result in this div

 <div id="target">user Id:<php echo $id ?>User Nmae:<?php echo $name ?>user designation :<?php echo $designation ?></div>

what changes i want to do my code for getting data in my second div

Twinxz
  • 303
  • 5
  • 16

1 Answers1

0

You're asking to take the mysql data from php and magically know what it is without querying again with AJAX or some other request.

There are many ways you can do this, but the simplest is to store the exact view of what you want in a child div below the a tag.

You need to change to prepared statements too.... If you don't want to fine just change the stmt stuff back to what you have and row will do what you want.

<style>
    .hidden{
        display: none;
    }
</style>

<?php 

    $stmt = mysqli_prepare("select * from tbl_sub_product where product_id='$id'"); 
    
    $stmt->execute();
    
    $result = $stmt->get_result();

    
    while($row = $result->fetch_assoc()){    
?>  
    <div>
        <a href="#" class="showElement" >
            <?php echo $row['name']; ?> <?php echo $row['id']; ?>
        </a>
        <div class="hidden getTarget">
            user id: <php echo $row['id']; ?> 
            User name: <?php echo $row['name']; ?>
            user designation: <?php echo $row['designation']; ?>
        </div>
    </div>
<?php 
    } 
?>

<script>
    $(document).ready(function(){
        var $target = $('#target');
        
        $(".showElement").click( function(e) {
            e.preventDefault();
            $target.append( $(this).parent().find('.getTarget').text() );
        });
    });
</script>

Don't use ids in a loop. Unless you have a specific identifier to separate each one.

Don't copy and paste as well.. please read the code and understand it.

Community
  • 1
  • 1
Cayce K
  • 2,288
  • 1
  • 22
  • 35