JS $ .post没有加载PHP文件

时间:2016-10-28 09:49:09

标签: javascript php jquery ajax

我从YouTube教程复制了代码,修改了数据库连接和SELECT项目以连接到我现有的数据库,但我似乎无法让JS加载PHP文件。在使用“检查”工具的Chrome中,我可以看到JS文件正在加载,但是当我点击我的HTML页面上的GRAB时,它没有做任何事情。几乎像JS文件正在加载但没有运行。

Webserver文件夹结构:

    ROOT FOLDER
      test.html
      -AJAX (folder)
          name.php
      -JS (folder)
          global.js
      -CONNECTIONS (folder)
          dbase.php

HTML CODE

    <!doctype html>
    <html>
    <head>
    <title>AJAX Database</title>
    </head>
    <body>
    Name: <input type="text" id="name">
    <input type="submit" id=name-submit" value="Grab">
    <div id="name-data"></div>

    <script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
    <script src="js/global.js"></script>
    </body>
    </html>

Javascript文件

    $('input#name-submit').on('click', function() {
        var name = $('input#name').val();
         if ($trim(name) != '') {
           $.post('ajax/name.php', {name: name}, function(data) {
                 $('div#name-data').text(data);
           });
         }
    });

PHP文件

3 个答案:

答案 0 :(得分:0)

首先尝试在你的javascript点击功能中添加一个console.log('something'),就在var name = ... line之前,看看你的事件是否正在触发。如果不是,您需要正确注册事件。 Click函数是否被

包围
$(document).ready(function () { 
    $('input#name-submit').on('click', function() {
        var name = $('input#name').val();
        if ($trim(name) != '') {
            $.post('ajax/name.php', {name: name}, function(data) {
                $('div#name-data').text(data);
            });
        }
    });
});

答案 1 :(得分:0)

FluorineFx.NET  
Null values
The <nullable> configuration section allows the use of special value of the given value type as the null value. 

Use this solution only when you can identify a value which is unused.

<nullable>
    <type name="System.Int32" assembly="MinValue"/>
    <type name="System.Double" assembly="MinValue"/>
    <type name="System.DateTime" assembly="MinValue"/>
    <type name="System.Guid" assembly="Empty"/>
</nullable>

The name attribute is the fully qualified type name, the value attribute is a static member of the type (such as "MinValue") or a parseable value (0 for System.Int32 for example).

The acceptNullValueTypes option
Fluorine will accept null values sent from client for value-types if configured accordingly

    <acceptNullValueTypes>false</acceptNullValueTypes>

If acceptNullValueTypes = true (the default is false if not specified) any value-type that is not explicitly initialized with a value will contain the default value for that object type (0 for numeric types, false for Boolean, DateTime.Min for DateTime)

答案 2 :(得分:0)

我不确定为什么我的PHP代码是从这里编辑和删除的,但我最终重写了整个PHP页面。我在{SELECT}语句中添加了Stream<Quarter>,发现它需要我在FROM中指定我的数据库。我的服务器上有多个数据库,即使在连接字符串中指定了DB,它也会在SQL语句中再次使用它。我解决的下一个问题是删除了or die(mysql_error());,因为这不像演示那样工作。

ORIGINAL PHP

mysql_num_rows

原版对于我想要的东西来说太复杂了,也让我解决问题所以我重写了它:

新PHP文件

    <?PHP
    if (isset($_POST['name']) === true && empty ($POST['name']) === false) {
           require '../db/connect.php';

    $query =mysql_query("
           SELECT 'names'.'location'
           FROM 'names'
           WHERE 'names'.'name' = '" . mysql_real-escape_string(trim($_POST['name'])) . "'
           ");

    echo (mysql_num_rows($query) !== 0) ? mysql_result($query, 0, 'location') : 'Name not found';
    }
    ?>

感谢那些帮助过的人。理解...

我知道PHP文件中已弃用的标签,但是当我从一个演示版本进行复制时,我希望在使用新标签更新它之前使其正常工作。

相关问题