【问题标题】:JS/PHP POST to php script for database query用于数据库查询的 JS/PHP POST 到 php 脚本
【发布时间】:2023-03-28 07:56:01
【问题描述】:

我正在尝试从用户那里获取输入,然后将其发布到另一个 PHP 脚本以将输入用于 MariaDB 查询。

Index.php:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="robots" content="index, follow">
    <meta name="viewport" content="width=device.width, inital-scale=1">

    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    <!-- jQuery UI library -->
    <link rel="stylesheet" 
    href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
    <!-- Initialise autocomplete -->
    <script type="text/javascript" src="jquery.js"></script>
   
</head>

<body>
   <form>
      <input type="text" id="name" name="search" placeholder="Enter Gene Name...">
      <button id="searchBtn" type="button" name="btn" onclick="post()" >Search</button>
   </form>
      
    <script>
        function post() {
        var name = document.getElementById("name").value;

        $.ajax({
                type : "POST", 
                url  : "search.php",  
                data : { name : name},
                success: function(res){  
                        }
            });
        }
    </script>
</body>

</html>

Search.php

<?php    
$name = $_POST['name'];

echo $name;

$dbhost = 'localhost';
$dbuser = '****';
$dbpass = '****';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);

// select and print all from database
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM cardiomic_database.genes WHERE gene LIKE '%".$name."%'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo $row["gene"]. "<br>";
  }
} else {
  echo "0 results";
}
$conn->close();
?>

我认为有很多错误。这个想法是在文本文件中使用 id="name" 获取用户输入,该文本文件应该发布到 search.php 并在 DB 查询中使用。我没有任何错误,它只是不工作。有什么建议吗?

【问题讨论】:

标签: javascript php html mysql mariadb


【解决方案1】:

我可以在表单事件中得出问题。请仅保留以下表格并删除所有其他表格,如果问题仍然存在,请告诉我。

<form>
    <input type="text" id="name" name="search" placeholder="Enter Gene Name...">
    <button id="searchBtn" type="button" name="btn" onclick="post()" >Search</button>
</form>

请尝试更新帖子功能如下。

<script>
    function post() {
    var name = document.getElementById("name").value;

    $.ajax({
            type : "POST", 
            url  : "search.php",  
            data : { name : name},
            success: function(res){ 
                $("body").prepend(res); 
            }
        });
    }
</script>

【讨论】:

    【解决方案2】:

    为什么要在这里使用ajax..?,而您可以直接将表单提交到php文件。

    <form method="post" action="search.php">
        <input type="text" id="name" name="search" placeholder="Enter Gene Name...">
        <button id="searchBtn" type="submit" name="btn">Search</button>
    </form>
    

    如果你真的想使用 ajax,那么我在这里看不到任何问题 TBH,但我的使用方式是。

    $.post("search.php", {
               name : name
            }, function (data, status) {
            //  alert (data + '\n' + "status" + status);
                if (status === 'error') {
                    console.log("error"); // For debugging purpose
                } else if (status === 'success') {
                  console.log("dont");
                }
            }); 
    

    【讨论】:

      猜你喜欢
      • 2011-09-28
      • 2014-11-29
      • 2015-09-18
      • 2011-04-12
      • 1970-01-01
      • 2017-04-23
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多