dongyi9330 2016-04-14 04:51 采纳率: 100%
浏览 55
已采纳

Javascript没有输入带.php文件引用的函数

long time answer-seeker, first time question-asker.

I am configuring a small snippet of js which is referenced in the page footer:

<script type='text/javascript' src='http://test.site.com/wp-content/themes/spacious/js/jsfile.js?ver=1.0.5'></script>

Here is the html for the inputs:

<div>
    <form id="search" action="" method="POST">
        <input type="text" id="str" name="str" value="" />
        <input type="submit" value="search" />
    </form>
    <div id="search_results"></div>
</div>

js script file as follows:

(function func() {
    $("#search").bind('submit', function () {
        var value = $('#str').val();

        $.post('db_query.php', {
            value: value
        }, function (data) {
            $("#search_results").html(data);
        }); 

        return false;
    });
});

php file as follows:

<?php
    try {
        $db = new PDO('sqlsrv:Server=xx.xx.xx.xx;Database=xxxx','user','pass');
    }
    catch (Exception $e) {
        echo 'PDO connection error: ' . $e->getMessage();
        exit(1);
    }

    $sql=$db->prepare("SELECT top 20 Timestamp,Information,Location FROM Table WHERE JobReference = :val");
    $sql->execute(array(':val'=>$_REQUEST['value']));

    echo '<table>';
    echo '<th>Date Time</th><th>Information</th><th>Location</th>';

    while ($row=$sql->fetch())
    {
        echo "<tr><td>$row[Timestamp]</td><td>$row[Information]</td><td>$row[Location]</td></tr>";
    }
    echo '</table>';
?>

When I try to use Chrome to debug, it stops on the first '(' and then jumps to the end of the function. At first I thought it was because of the .php file reference, but it seems like it is not even hitting it?

I don't know where to go with this...

Many thanks, Cameron.

  • 写回答

2条回答 默认 最新

  • dongluo1853 2016-04-14 05:04
    关注

    In addition to @Nano answer, you can fix like this:

    (function($) { //pass $ as jQuery in this scope
    $("#search").bind('submit', function () {
        var value = $('#str').val();
        $.post('db_query.php', {
            value: value
        }, function (data) {
            $("#search_results").html(data);
        }); return false;
      });
    })(jQuery); //$ is jQuery
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度