0

I have the fallowing html:

<a href="./libs/php/edit.php?edit=<?php echo $row['lastName']; ?>" class="btn btn-info">Edit</a>

php script handling the data:

<?php

 ini_set('display_errors', 'On');
 error_reporting(E_ALL);
 $executionStartTime = microtime(true);

 include("config.php");
 $conn = new mysqli($cd_host, $cd_user, $cd_password, $cd_dbname, $cd_port, $cd_socket) or 
 die(mysqli_error($conn));


 $department = " ";
 $location = " ";
 $update = false;

 if (mysqli_connect_errno()) {

$output['status']['code'] = "300";
$output['status']['name'] = "failure";
$output['status']['description'] = "database unavailable";
$output['status']['returnedIn'] = (microtime(true) - $executionStartTime) / 1000 . " ms";
$output['data'] = [];

mysqli_close($conn);

echo json_encode($output);

exit;
}


if (isset($_GET['editDepartment'])) {
$id = $_GET['editDepartment'];
$result = $conn->query("SELECT * FROM department WHERE id=$id") or die(mysqli_error($conn));
$update = true;
if (!$result) {

    $output['status']['code'] = "400";
    $output['status']['name'] = "executed";
    $output['status']['description'] = "query failed";
    $output['data'] = [];

    mysqli_close($conn);

    echo json_encode($output);

    exit;
} else {
    $row = $result->fetch_array();
    $department = $row['name'];
    $location = $conn->query("SELECT locationID FROM department WHERE id=$id") or 
  die(mysqli_error($conn));
  }
   };

and a modal which is already handling my 'create entry' feature :

     <!-- Modal department-->
        <div class="modal fade" id="department" tabindex="-1" role="dialog" aria- 
         labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <form action="./libs/php/insertDepartment.php" method="POST">
                            <p><input type="text" name="departmentName" placeholder="Department name" 
                    id="departmentName" value="<?php echo $name ?>"></p>
                            <select name="locationID" id="locationID">
                                <?php
                                foreach ($outputAllLoc['data'] as $item) {
                                    echo  "<option value='($item[id])'>$item[name]</option>";
                                }
                                ?>
                            </select>


                    </div>
                    <div class="modal-footer">

                        <button type="submit" class="btn btn-primary">Save changes</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>



        <!-- end of modal -->

Is there a way to use that anchor tag to open this modal and send data to the PHP script? My goal is to have the same modal for creating entries and handling the edit feature. Thanks, everyone! Chi Chi

Chi Chi Huang
  • 43
  • 1
  • 5
  • using Ajax, when the anchor tag clicked send Ajax request to PHP script and return a json array with the output and append it to the modal then call modal.show() – Nasser Hajlawi Apr 04 '21 at 14:31
  • also, i am not seeing any get request in your PHP script for ``edit`` parameter. – Nasser Hajlawi Apr 04 '21 at 14:35
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Apr 04 '21 at 15:24
  • Thanks everyone for your help!!! – Chi Chi Huang Apr 04 '21 at 19:09

0 Answers0