我想用按钮做一些功能。我想当我点击按钮它将显示结果。结果来自数据库

时间:2018-06-05 08:09:56

标签: php html mysqli

我是PHP的新手我不知道如何使用这个功能...输出是一个按钮但是当我点击按钮功能不运行时。我想从数据库中获取数据。

<?php include('config.php');
if (isset($_POST['submit']))  {

$sql = "SELECT book_name from books";
$result = $conn->query($sql);
if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
    echo "book name: " . $row["book_name"]. "<br>";


   }
} else {
    echo "0 results";
}
}
$conn->close();
?>



<!DOCTYPE html>
<html>
<head>
    <title>test</title>
</head>
<body>
    <form action="check.php" method="post">
        <input type="submit" name="btn">
    </form>

</body>
</html>

3 个答案:

答案 0 :(得分:1)

只需按isset($_POST['submit'])更改isset($_POST['btn'])

提交表单时,使用$_POST方法发送所有输入名称。

此处您只有一个输入name = 'btn'

我做了这个测试:

<?php
if (isset($_POST['btn']))  {
    echo 'IT WORKS !';
}
?>

点击&#34; Valider&#34;它对我有用。我得到了echo

enter image description here

以下是如何使用jQuery和Ajax尝试执行此操作的示例。我试图解释它是如何工作的,并做了一个编辑你的图书清单而不重新加载提交页面的例子。希望它有所帮助!

&#13;
&#13;
$(document).ready(function() {
  // When the form with id 'get_book_list' is sumbit, I will do something :
  $("#get_book_list").on('submit', function(e) {
    e.preventDefault(); 
    
    // This is how an ajax call looks like (one example) :
    // $.ajax({
    //     type: "POST", // the method you will use to send the data
    //     url: "yourfile.php", // the php file you want to call
    //     data : /* the data you want to send in your php file */,
    //     dataType: "json", // the data type you want to receive from the php file
    //     success: function(response){
    //         // Do something if the ajax call works : here you will edit your book list
    //         // 'response' is what the php will return, here imagine it's a JSON (dataType = 'json')
    //     },
    //     error: function(x,e,t){
    //         // Do something if the ajax call return error
    //     }
    // });
    
    //I will do the same but without the Ajax :
    // 1/ Imagine you are in the success part of the ajax call
    // 2/ This is the "response" you get from the php after the select :
    
    var response = [{"book_name" : "title1"}, {"book_name" : "title2"}, {"book_name" : "title3"}];
    
    // 3/ Now just edit your book_list to get what you want :
    
    // I will build a list with each title
    var book_list = "<ul>";
    $.each(response, function(key, book) {
      book_list += "<li>"+book.book_name+"</li>";
    });
    book_list += "</ul>";
    
    // I add my list in my div
    $('.book-list').html(book_list);
  
  });



});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
  <html>
  <head>
      <title>test</title>
  </head>
  <body>
  
  <div class="book-list">
  <!-- here you will update the list of book -->
  </div>
  
  <form action="" method="post" id="get_book_list">
      <input type="submit" name="btn">
  </form>

  </body>
  </html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您的表单操作说“check.php”。因此,当您点击提交按钮时,您将被重定向到check.php页面。 你的表格应该就像

<form action="" method="post">
    <input type="submit" name="btn">
</form>

当action =“”时,你的php的if部分将针对你当前的情况执行

答案 2 :(得分:0)

isset($ _ POST [&#39; submit&#39;])为false,您应该提交一些信息。您可以添加<input type="text" name="submit" value="1" />,让您的表单有信息。