How to remove dropdown duplicate

Viewed 71

I am fairly new to the PHP PDO world, I already know now, that the connection to the DB should be in another PHP file, but then the rest does not work :(. I have tried all day, I know I am so tired of it that I have to ask here. How do I get rid of the Duplicate? I think the duplicate is there as a result of the DB connection being in the same folder, as it is not localhost but a website. So how do I write the files out in multiple files?

In the Display.php file, I have the dropdown with HTML, followed by javascript that should trigger the table when a value from the dropdown has been chosen. Action.php is where the table is created and I tried to create a separate file for the DB connection, but then I can't concatenate the "uid" javascript value to the Action.php file.

I hoped you could help me get rid of the Duplicate of the dropdown.

Tell me if I can add more info to help.

Best regards

This I an image of the duplicate when I choose a value from the dropdown, it duplicates the dropdown, and add it below. It is meant to only show the table below. duplicate

Display.php

    <?php
    
    ini_set("display_errors", "On");
    error_reporting(E_ALL);
    
    $host='database.*****.us-east.rds.amazonaws.com';
    $db = '*****';
    $port = 5432;
    $username = '*****';
    $password = '*****';
    
    try {
      $conn = new PDO("pgsql:host=$host;port=$port;dbname=$db;user=$username;password=$password");
    } catch(PDOException $e) {
      echo "Error: " . $e->getMessage();
    }
    
    $sql ="select * from public.category";
    //Prepare the select statement.
    $stmt = $conn->prepare($sql);
    //Execute the statement.
    $stmt->execute();
    //Retrieve the rows using fetchAll.
    $users = $stmt->fetchAll();  
    ?>
    
// show menu dropdown
    <!Doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
    </head>
      <body>
        <div class="container" style="margin-top: 50px;">
          <h2 class="text-center">Væg en kategori </h2>
          <div class="row">
            <div class="col-md-4"></div>  
              <div class="col-md-4" style="margin-top:20px; margin-bottom:20px;">
                <form id="submitForm">
                  <div class="form-group">
                    <select class="form-control Investment" name="Investment" id="Investment">
                      <option value="">Vælg Kategori</option>
                          <?php foreach($users as $user): ?>
                            <option value="<?= $user['id']; ?>"><?= $user['name']; ?></option>
                          <?php endforeach; ?>
                    </select>
                  </div>
                </form>
              </div>
            </div>
            <div class="col-md-12">
              <div id="show-invest">
              </div>
            </div>    
          </div>
      </body>
    </html>
    
    <!---jQuery ajax load rcords using select box --->
    <script type="text/javascript">
      $(document).ready(function(){
          $(".Investment").on("change", function(){
            var InvestmentName = $(this).val();
            if (InvestmentName !== "") {
              $.ajax({
                url : "Action.php",
                type:"POST",
                cache:false,
                data:{InvestmentName:InvestmentName},
                success:function(data){
                  $("#show-invest").html(data);
                }
              });
            }else{
              $("#show-invest").html(" ");
            }
          })
      });
    </script>

Action.php

<?php

    // include database connection file
    include_once "testSide.php";
   // include_once "DBController.php";
    if(isset($_POST['InvestmentName']))
{
    $uid = $_POST['InvestmentName'];
}
    
    // load records using select box jquery ajax in PHP
    $qu = "select Prod.tocon as name, one.con as yield_one, five.con as yield_two, ten.con as yield_three, twenty.con as yield_four, one.st as st_one, five.st as st_two, ten.st as st_three, twenty.st as st_four, one.top as top_one, five.top as top_two, ten.top as top_three, twenty.top as top_four;";


    $result = $conn->query($qu);
    if (!$result) {
        trigger_error('Invalid query:' . $conn->error);
    }
    $output = '';
    if ($result->rowCount() > 0) {
        $output .= "<table class='table table-hover table-border'>
                        <thead>
                          <tr>
                            <th>Name</th>
                            <th>Afkast sidste år</th>
                            <th>Genst sidste 5 år</th>
                            <th>Genst stidste 10 år</th>
                            <th>Genst sidste 20 år</th>
                            <th>SR sidste år</th>
                            <th>SR 5 år</th>
                            <th>SR 10 år</th>
                            <th>SR 20 år</th>
                            <th>DD sidste år</th>
                            <th>DD 5 år</th>
                            <th>DD 10 år</th>
                            <th>DD 20 år</th>
                          </tr>
                        </thead>";
    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
        $output .= "<tbody>
        <tr>
          <td>{$row["name"]}</td>
          <td>{$row["yield_one"]}</td>
          <td>{$row["yield_two"]}</td>
          <td>{$row["yield_three"]}</td>
          <td>{$row["yield_four"]}</td>
          <td>{$row["st_one"]}</td>
          <td>{$row["st_two"]}</td>
          <td>{$row["st_three"]}</td>
          <td>{$row["st_four"]}</td>
          <td>{$row["top_one"]}</td>
          <td>{$row["top_two"]}</td>
          <td>{$row["top_three"]}</td>
          <td>{$row["top_four"]}</td>
        </tr>";
      
        }                       
        "</tbody>";               
        $output .= "</tbody></table>";
        echo $output;
    }else{
        echo "No records found";
    }
?>
1 Answers

I fixed it by moving the database connector in an isolated file. So it did not call it self in the same PHP file. Now i then call the DB from the other files with include_once

Related