doujian4752 2019-05-29 05:31
浏览 48
已采纳

似乎我的ajax工作但没有发布任何内容

I am trying to post the value of my selector to a php file but it isnt working. My success function works and when i go to "your_php_script.php" the page works and the php runs as expected. Why wont the php page show up on the page with the ajax? I edited my post to add the database connection function from database_connection.php

// html & jquery
 <select name="rooftop" id="rooftop" class="selectMenu">
            <option value="">Select an option</option>
            <option value="Yes">Yes</option>
            <option value="No">No</option>
        </select>


    <button id="barButton">click</button>

    <div id="showBars">

    </div>


    <script type="text/javascript">

        $(document).ready(function(){

                          $("#barButton").click(function() {
                            var selected = $("#rooftop").val();
                            $.ajax({
                                   type: "POST",
                                   url: "your_php_script.php",
                                   data: {selected: selected},
                                   success: function(){
                                   alert("works");
                                   }

                            });
                          });

// code on your_php_script.php
<?php

require_once("database_connection.php");
$db = db_connect();

 echo "<script type='text/javascript'> alert('works1'); </script>";
    $selected = $_POST["selected"];
    if(isset($selected)){
        echo "<script type='text/javascript'> alert('works2'); </script>";
        $sql = "SELECT bar_name, area, hourStart, hourEnd FROM barInfo WHERE rooftop = 1";
        $result = mysqli_query($connection, $sql);
        if (mysqli_num_rows($result) > 0) {
            echo "<table id='list'><tr><th><h3>Name</h3></th><th><h3> Area</h3></th><th><h3>Happy Hour Times</h3></th></tr>";
            while($row = mysqli_fetch_assoc($result)) {
                echo "<tr> <th> <h6> " . $row["bar_name"] . "</h6> </th> <th> <h6>" . $row["area"] . "</h6> </th> <th> <h6>" . $row["hourStart"] . "-" . $row["hourEnd"] . "</h6> </th> </tr>";
            }
            echo "</table";
        }

    }
db_disconnect($db);

?>


// database connection
function db_connect() {
        global $connection, $servername, $username, $password, $database;
        $connection = new mysqli($servername, $username, $password, $database) or die("Unable to Connect");
        return $connection;
    }
  • 写回答

2条回答 默认 最新

  • dongzhou1865 2019-05-29 06:50
    关注

    As you mentioned that the number of rows in result is not more than 0, That does means either $connection is not properly defined OR there is no such entry in database where barInfo = 1 so its returning back nothing.

    So firstly you should check your database_connection.php file if the connection to the DB is done properly or not, because it could be the case that this file (your_php_script.php) is not getting $connection from database_connection.php file.

    Also, as i mentioned earlier, dataType is important (not mandatory though). Even more important is to append the incoming data in the apt. element, for which your success function should look like :

    $.ajax({
        type: "POST",
        dataType: "html"
        url: "your_php_script.php",
        data: {
            selected: selected
        },
        success: function(result) {
            alert("works");
            jQuery("#showBars").html(result); 
        },
        error: function(error) {
           alert(error);
        }
    });
    

    Things to remember while using ajax:

    1) Make sure the dataType attribute is appropriately defined. If none is specified, jQuery will try to infer it based on the MIME type of the response.

    2) Success function should be defined properly with the proper usage of the incoming data, on how to use it in your HTML view.

    3) Make habit of using error function as it will help you get your errors when it not enters Success function and have any error(s).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM