0

I made a button inside a while loop and then passed a jquery function to it. Now the jquery function is working fine on the first button but doesn't work on other buttons.

   <script>
   $(document).ready(function() {
   $("#myBtn").click(function() {
       var id = this.value;
       $("#myModal").modal();
       $("#employee").change(function() {
           var eid = this.value;

           var dataString = 'id=' + id + '&eid=' + eid;
           $.ajax({
               type: "POST",
               url: "ajax.php",
               data: dataString,
               success: function(result) {

               }
           });
       });
   });
   });

   /*  $(document).ready(function(){
    $("#myBtn").click(function(){
     var id = this.value;
     if(id == true){alert(id++);}
     
 });
 });*/
</script>
   



   <div>
  <div class="panel">
     <table id="basicTable" class="table table-striped table-bordered responsive">
        <thead class="">
           <tr>
              <th>S.No</th>
              <th>Title</th>
              <th>Lead Type</th>
              <th>Customer Name</th>
              <th>Customer Phone No.</th>
              <th>Customer Email</th>
              <th>Property Type</th>
              <th>Property Location</th>
              <th>Active</th>
              <th>Edit</th>
              <th>Assign</th>
           </tr>
        </thead>
        <tbody>
           <?php
              $que=mysqli_query($con,"select * from lms_table");
              $number=1;
              while ($res=mysqli_fetch_array($que))
              {
              $id=$res['lead_id'];
              $pt=$res['required_property_type'];
              ?>
           <tr>
              <td align="center"><?php echo $number; ?></td>
              <td align="center"><a href="comment.php?id=<?php echo $id; ?>"><?php echo $res['title']; ?></a></td>
              <td align="center"><?php echo $res['lead_type']; ?></td>
              <td align="center"><?php echo $res['customer_name']; ?></td>
              <td align="center"><?php echo $res['customer_no']; ?></td>
              <td align="center"><?php echo $res['customer_email']; ?></td>
              <?php  $qe=mysqli_query($con,"SELECT * FROM `add_property_type` WHERE property_id='$pt'"); 
                 $re=mysqli_fetch_array($qe); ?>
              <!-- <td align="center"><?php// echo $re['property_type']; ?></td>-->
              <td align="center"><?php echo $res['required_property_type']; ?>
              <td align="center"><?php echo $res['property_required_location']; ?></td>
              <?php
                 if($res['lead_status']==1){
                 ?>
              <td align="center"> <a class="tooltips" data-toggle="tooltip" title="Active" href="lead-active.php?l_id='<?php echo $id; ?>'&id=2"><i class="fa fa-user text-success"></i></a></td>
              <?php
                 } else{
                 ?>
              <td align="center"> <a  class="tooltips" data-toggle="tooltip" title="Inactive" href="lead-active.php?l_id='<?php echo $id; ?>'&id=1"><i class="fa fa-user text-danger"></i></a></td>
              <?php  } ?>
              <td align="center"><a href="update-lms-table.php?id=<?php echo $id; ?>"><i class="fa fa-edit"></i></a></td>
              <td align="center"> <button type="button" class="btn btn-default " name ='but' id="myBtn" value="<?php echo $id['lead_id']; ?>" onclick='get_id(this.value)'>Assign</button> </td>
           </tr>
           <?php 
              $number++; 
              } 
              ?>
        </tbody>
     </table>
  </div>
  <!-- panel -->
   </div>
   <!-- Modal -->
   <div class="modal fade" id="myModal" ?role="dialog">
  <div class="modal-dialog">
     <!-- Modal content-->
     <div class="modal-content">
        <div class="modal-header" style="padding:35px 50px;">
           <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
           <h4 class="modal-title" id="exampleModalLabel">Assign Employee</h4>
        </div>
        <div class="modal-body" style="padding:40px 50px;">
           <form action="half.php">
              <select class="form-control select1 "  id="employee">
                 <option selected value="">Select Employee</option>
                 <?php
                    $que1 =mysqli_query($con,"select * from add_employee");
                    while($r=mysqli_fetch_array($que1)){
                            $empid=$r['employee_code']; 
                      echo "<option value='$empid' onchange='get_id(this.value)'>".$r['employee_first_name']."</option>";
                    }                                                      
                    ?>
              </select>
        </div>
        <div class="modal-footer">
        <button type="button" class="btn btn-success btn-default" data-dismiss="modal" ><span class="glyphicon glyphicon-off"></span> Login</button>
        <button type="submit" class="btn btn-danger btn-default pull-right" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
        </form>
        </div>
     </div>
  </div>
   </div>
   </div>

This are the code used. Please shed some light.

3
  • clean your code Commented Mar 22, 2017 at 11:57
  • Ids have to be unique Commented Mar 22, 2017 at 11:59
  • you can have one name as ID, you must use class if you have multiple buttons and you want onclick event on those. Commented Mar 22, 2017 at 11:59

1 Answer 1

2

Instead of using the callback in the <options> tag, add it to the select tag. You do not need to attach the callback on every option.

It will work with the select tag alone.

Replace your select tag with this:

<select class="form-control select1 " onchange='get_id(this.value)' id="employee">
        <option selected value="">Select Employee</option>
        <?php
            $que1 =mysqli_query($con,"select * from add_employee");
            while($r=mysqli_fetch_array($que1))
            {
                $empid=$r['employee_code'];
                echo "<option value='$empid'>".$r['employee_first_name']."</option>";
            }

                                                ?>
</select>

Hope it would help.

Sign up to request clarification or add additional context in comments.

1 Comment

Not it didn't work i got half of the solution as adding a unique number in to id="Mybtn<?php echo $num; ?>" and writing the same script multiple times and passing number 1 2 3 4 5 6 and so on with the $("#myBtn1") and $("#myBtn2") works. after ten values there is pagination and on 11th button it again dosent works. Thanks for trying.

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.