使用Ajax PHP在数据库之间切换

时间:2018-11-25 16:50:43

标签: php jquery mysql ajax

我试图根据所选的数据库名称在数据库连接之间切换(只需更改主机名)。

我得到了带有值的<a>标签。

<div class="user-menu dropdown-menu">
    <a class="nav-link" href="db" id="db_one"><i class="fa fa-database"></i> DB 1</a>
    <a class="nav-link" href="db" id="db_two"><i class="fa fa-database"></i> DB 2</a>   
</div>

现在,我要检查单击了哪个<a>,然后取值db_one/db_two并将其通过ajax发送到我的php连接文件中。

我的错在哪里?我仍然有默认连接

$("a[href='db']").click(function(e) {
  e.preventDefault();
  var hostname = e.target.id;
  console.log(hostname);
  $.ajax({
    url: 'connection.php',
    data: hostname,
    type: "post",
    success: function() {
      alert('done');
      console.log('this is: ' + hostname);
      location.reload();
    },
  });
});

我正在重新加载页面以与数据库“重新连接”。

最后是我的php连接。

<?php

    $host = $_POST['hostname'];
    $hostname = $host;

    switch ($hostname) {
      case "db_one":
        $conn = mysqli_connect("$hostname", "my_user", "my_password", "my_db");
        if($conn === false){
            die("ERROR: Could not connect. " . mysqli_connect_error());
        } else {
          echo "<script>console.log('connected');</script>";
        }
        break;
      case "db_two":
        $conn = mysqli_connect("$hostname", "my_user", "my_password", "my_db");
        if($conn === false){

            die("ERROR: Could not connect. " . mysqli_connect_error());

        } else {
          echo "<script>console.log('Polaczenie z baza nawiazane');</script>";
          echo "<script>console.log('connected');</script>";

        }
        break;
      default:
        $conn = mysqli_connect("localhost", "my_user", "my_password", "my_db");
        if($conn === false){
            die("ERROR: Could not connect. " . mysqli_connect_error());

        } else {
          echo "<script>console.log('default connect');</script>";

        }
    }
?>

3 个答案:

答案 0 :(得分:1)

您没有发送键/值对。...仅是一个值。

更改

data: hostname

收件人

data: {hostname:hostname}

答案 1 :(得分:0)

使用以下代码发送ajax请求:

$("a[href='db']").click(function(e) {
  e.preventDefault();
  var hostname = this.id;
  console.log(hostname);
  $.ajax({
    url: 'connection.php',
    data: {'hostname': hostname},
    type: "post",
    success: function() {
      alert('done');
      console.log('this is: ' + hostname);
      location.reload();
    },
  });
});

希望这可以解决问题

答案 2 :(得分:0)

现在使用此完整代码

<?php
if ((function_exists('session_status') //start session if not started
  && session_status() !== PHP_SESSION_ACTIVE) || !session_id()) {
 $lifetime=strtotime('+7 days', 0);
  session_set_cookie_params($lifetime);
  session_start();
} if(isset($_POST['hostname'])){
    $_SESSION['hostname']=$_POST['hostname'];
}  $hostname =$_SESSION['hostname'];

    switch ($hostname) {
      case "db_one":
        $conn = mysqli_connect("$hostname", "my_user", "my_password", "my_db");
        if($conn === false){
            die("ERROR: Could not connect. " . mysqli_connect_error());
        } else {
          echo "<script>console.log('connected');</script>";
        }
        break;
      case "db_two":
        $conn = mysqli_connect("$hostname", "my_user", "my_password", "my_db");
        if($conn === false){

            die("ERROR: Could not connect. " . mysqli_connect_error());

        } else {
          echo "<script>console.log('Polaczenie z baza nawiazane');</script>";
          echo "<script>console.log('connected');</script>";

        }
        break;
      default:
        $conn = mysqli_connect("localhost", "my_user", "my_password", "my_db");
        if($conn === false){
            die("ERROR: Could not connect. " . mysqli_connect_error());

        } else {
          echo "<script>console.log('default connect');</script>";

        }
    }
?>