Iresha Shyamean
Iresha Shyamean

Reputation: 61

Passing a PHP echo value to a modal box

I'm creating a web back end for a restaurant. There is an option to block/unblock restaurant owners. I have put an "if" condition to appear button (a tag) according to the status of the restaurant owner. Within this "a" tag I want to pass the value (id) to a modal box.

I put some code in the "data-target" of "a" tag, as well as in the modal id. The value didn't pass and also didn't open the modal.

                                        <tbody>
                                            <?php
                                            global $con;
                                            $sql = "SELECT * FROM `fd_owner_details`";
                                            $result=mysqli_query($con,$sql);
                                            while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
                                                $kk=$row['id'];
                                            ?>

                                            <tr>
                                                <td><?php echo $row['id']; ?></td>

                                                <td><?php echo $row['fname']. " " .$row['lname'] ; ?></td>

                                                <td><?php echo $row['address_line1']. ", " .$row['address_line2'] ; ?></td>

                                                <td><?php echo $row['contact_no1'].", ".$row['contact_no2']; ?></td>

                                                <td><?php echo '<img src= "'.$row['image'].'">'; ?></td>

                                                <td>
                                                    <?php
                                                if ($row['status']==1){
                                                     echo "<span class='label mb-2 mb-xl-0 label-dark'>Active</span>";
                                                }
                                                else {
                                                     echo "<span class='label mb-2 mb-xl-0 label-light'>Disabled</span>";
                                                }
                                                ?>
                                                </td>

                                                <td>
                                                    <span>
                                                        <a href="edit-restaurant-owners.php?id=<?php echo $row['id']; ?>" data-toggle="tooltip" data-placement="top" class="btn btn-success btn-xs" data-original-title="Edit"><i class="fa fa-pencil" aria-hidden="true"></i></a>
                                                        <?php
                                                if ($row['status']==1){
                                                     echo '<a href="#" data-toggle="modal" data-target="#blockRestaurantOwner?id=', $row['id']; ,'" class="btn btn-warning btn-xs" data-toggle="tooltip" data-placement="top" data-original-title="Block"><i class="fa fa-ban" aria-hidden="true"></i></a>';
                                                }
                                                else {
                                                    echo "<a href='#' data-toggle='modal' data-target='#unblockRestaurantOwner' class='btn btn-warning btn-xs' data-toggle='tooltip' data-placement='top' data-original-title='Unblock'><i class='fa fa-check' aria-hidden='true'></i></a>";
                                                }
                                                ?>
                                                        <a href="#" data-toggle="modal" data-target="#deleteRestaurantOwner" class="btn btn-danger btn-xs" data-toggle="tooltip" data-placement="top" data-original-title="Delete"><i class="fa fa-trash" aria-hidden="true"></i></a>

                                                    </span>
                                                </td>
                                            </tr>
                                            <?php        }        ?>

                                        </tbody>


--------------------------------------------------------------------------------

<div class="modal fade" id="blockRestaurantOwner?id=<?php echo $row['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="blockRestaurantOwnerTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Confirm Delete </h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times; </span>
                        </button>
      </div>
      <div class="modal-body">
        <p id="main-content">Are you sure you want to block this restaurant owner ???</p>
        <p id="content">You will not be able to recover this action !!!</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">No, Cancel it </button>
        <button type="button" class="btn btn-warning">Yes, Block</button>
      </div>
    </div>
  </div>
</div>

Where Should I change, to get the "id" to the modal ?

Upvotes: 0

Views: 738

Answers (2)

suresh bambhaniya
suresh bambhaniya

Reputation: 1687

you can set onclick listener to block button and pass owner id to that function like below

<tbody>
    <?php
    global $con;
    $sql = "SELECT * FROM `fd_owner_details`";
    $result=mysqli_query($con,$sql);
    while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
        $kk=$row['id'];
    ?>

    <tr>
        <td><?php echo $row['id']; ?></td>

        <td><?php echo $row['fname']. " " .$row['lname'] ; ?></td>

        <td><?php echo $row['address_line1']. ", " .$row['address_line2'] ; ?></td>

        <td><?php echo $row['contact_no1'].", ".$row['contact_no2']; ?></td>

        <td><?php echo '<img src= "'.$row['image'].'">'; ?></td>

        <td>
            <?php
        if ($row['status']==1){
             echo "<span class='label mb-2 mb-xl-0 label-dark'>Active</span>";
        }
        else {
             echo "<span class='label mb-2 mb-xl-0 label-light'>Disabled</span>";
        }
        ?>
        </td>

        <td>
            <span>
                <a href="edit-restaurant-owners.php?id=<?php echo $row['id']; ?>" data-toggle="tooltip" data-placement="top" class="btn btn-success btn-xs" data-original-title="Edit"><i class="fa fa-pencil" aria-hidden="true"></i></a>
                <?php
        if ($row['status']==1){
             echo '<a href="#" data-toggle="modal" onclick="set_target_id(<?= $row['id'] ?>)" data-target="#blockRestaurantOwner" class="btn btn-warning btn-xs" data-toggle="tooltip" data-placement="top" data-original-title="Block"><i class="fa fa-ban" aria-hidden="true"></i></a>';
        }
        else {
            echo "<a href='#' data-toggle='modal' data-target='#unblockRestaurantOwner' class='btn btn-warning btn-xs' data-toggle='tooltip' data-placement='top' data-original-title='Unblock'><i class='fa fa-check' aria-hidden='true'></i></a>";
        }
        ?>
                <a href="#" data-toggle="modal" data-target="#deleteRestaurantOwner" class="btn btn-danger btn-xs" data-toggle="tooltip" data-placement="top" data-original-title="Delete"><i class="fa fa-trash" aria-hidden="true"></i></a>

            </span>
        </td>
    </tr>
    <?php        }        ?>

</tbody>


<div class="modal fade" id="blockRestaurantOwner" tabindex="-1" role="dialog" aria-labelledby="blockRestaurantOwnerTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Confirm Delete </h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times; </span>
                        </button>
      </div>
      <div class="modal-body">
        <p id="main-content">Are you sure you want to block this restaurant owner ???</p>
        <p id="content">You will not be able to recover this action !!!</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">No, Cancel it </button>
        <button type="button" class="btn btn-warning">Yes, Block</button>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript">
    var target_owner_id="";
    function set_target_id(id){
        target_owner_id = id;
        console.log(target_owner_id);
    }
</script>

Upvotes: 1

Hariharan V.
Hariharan V.

Reputation: 191

Just copy the whole script and try to past inside a php file and try to run it. This can be modified as expected at your end using while loop.

<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

</head>


            <?php 
                $array[] = array('id'=>1, 'fname'=>'hari','lname'=>'ltest','address_line1'=>'no:9','address_line1'=>'rajan street','contact_no1'=>'92222','contact_no2'=>'899782','status'=>1);
                $array[] = array('id'=>2, 'fname'=>'vvvv','lname'=>'ltest2','address_line1'=>'no:92','address_line1'=>'siv street','contact_no1'=>'522','contact_no2'=>'2922','status'=>0);

            ?>
          <table border=1>
           <tbody>
              <?php foreach($array as $row){ ?>

                    <tr>
                        <td><?php echo $row['id']; ?></td>

                        <td><?php echo $row['fname']. " " .$row['lname'] ; ?></td>

                        <td><?php echo $row['address_line1']. ", " .$row['address_line2'] ; ?></td>

                        <td><?php echo $row['contact_no1'].", ".$row['contact_no2']; ?></td>

                        <td>
                            <?php
                        if ($row['status']==1){
                             echo "<span class='label mb-2 mb-xl-0 label-dark'>Active</span>";
                        }
                        else {
                             echo "<span class='label mb-2 mb-xl-0 label-light'>Disabled</span>";
                        }
                        ?>
                        </td>

                        <td>
                            <span>
                                <a href="edit-restaurant-owners.php?id=<?php echo $row['id']; ?>" data-toggle="tooltip" data-placement="top" class="btn btn-success btn-xs" data-original-title="Edit"><i class="fa fa-pencil" aria-hidden="true"></i></a>
                                <?php if ($row['status']==1){ ?>
                             <a data-toggle="modal" href="#blockRestaurantOwner<?=$row['id']?>"  class="btn btn-warning btn-xs" data-toggle="tooltip" data-placement="top" data-original-title="Block"><i class="fa fa-ban" aria-hidden="true"></i></a>
                    <div id="blockRestaurantOwner<?php echo $row['id']; ?>" class="modal fade" aria-hidden="true">
                          <div class="modal-dialog modal-dialog-centered" role="document">
                            <div class="modal-content">
                              <div class="modal-header">
                                <h5 class="modal-title">Confirm Delete </h5>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                    <span aria-hidden="true">&times; </span>
                                                </button>
                              </div>
                              <div class="modal-body">
                                <p id="main-content">Are you sure you want to block this restaurant owner ???</p>
                                <p id="content">You will not be able to recover this action !!!</p>
                              </div>
                              <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">No, Cancel it </button>
                                <button type="button" class="btn btn-warning">Yes, Block</button>
                              </div>
                            </div>
                          </div>
                        </div>

                        <?php }
                        else { 
                            echo "<a href='#' data-toggle='modal' data-target='#unblockRestaurantOwner' class='btn btn-warning btn-xs' data-toggle='tooltip' data-placement='top' data-original-title='Unblock'><i class='fa fa-check' aria-hidden='true'></i></a>";
                        }
                        ?>
                                <a href="#" data-toggle="modal" data-target="#deleteRestaurantOwner" class="btn btn-danger btn-xs" data-toggle="tooltip" data-placement="top" data-original-title="Delete"><i class="fa fa-trash" aria-hidden="true"></i></a>

                            </span>
                        </td>
                    </tr>
                    <?php } ?>
                </tbody>
                </table>

Upvotes: 2

Related Questions