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

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.

图片转代码服务由CSDN问答提供 功能建议

长期应答者,第一次提问。

I 我正在配置页面页脚中引用的一小段js:

 &lt; script type ='text / javascript'src ='http://test.site。  com / wp-content / themes / spacious / js / jsfile.js?ver = 1.0.5'&gt;&lt; / script&gt; 
   
 
 

这是html 输入:

 &lt; div&gt; 
&lt; form id =“search”action =“”method =“POST”&gt; 
&lt; input type =  “text”id =“str”name =“str”value =“”/&gt; 
&lt; input type =“submit”value =“search”/&gt; 
&lt; / form&gt; 
&lt; div  id =“search_results”&gt;&lt; / div&gt; 
&lt; / div&gt; 
   
 
 

js脚本文件如下: < pre> (function func(){ $(“#search”)。bind('submit',function(){ var value = $('#str')。val(); $ .post('db_query.php',{ value:value },function(data){ $(“#search_results”)。html(data); }); 返回false; }); });

php文件如下:

  &lt;?php 
 try {
 $ db = new PDO('sqlsrv:Server = xx.xx.xx.xx; Database = xxxx','user','pass'); 
} \  n catch(Exception $ e){
 echo'PDO connection error:'。  $ e-&gt; getMessage(); 
 exit(1); 
} 
 
 $ sql = $ db-&gt; prepare(“SELECT top 20 Timestamp,Information,Location FROM Table WHERE JobReference =:val”  ); 
 $ sql-&gt; execute(array(':val'=&gt; $ _ REQUEST ['value'])); 
 
 echo'&lt; table&gt;'; 
 echo'&lt; th&gt; 日期时间&lt; / th&gt;&lt; th&gt;信息&lt; / th&gt;&lt; th&gt;位置&lt; / th&gt;'; 
 
 while($ row = $ sql-&gt; fetch())
 {
 \ echo  “&LT; TR&GT;&LT; TD&GT; $行[时间戳] LT; / TD&GT;&LT; TD&GT; $行[信息]&LT; / TD&GT;&LT; TD&GT; $行[位置]&LT; / TD&GT;&LT; /  tr&gt;“; 
} 
 echo'&lt; / table&gt;'; 
?&gt; 
   
 
 

当我尝试使用Chrome进行调试时,它 停在第一个'('然后跳转到函数的末尾。起初我以为是因为.php文件引用,但它似乎甚至没有击中它?

我不知道该去哪儿...

非常感谢,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
    
    点赞 打赏 评论
  • dsvf46980 2016-04-14 04:56

    You are just declaring a JavaScript Function called func. But you also have to call that function in order to make your code work.

    Add another bracket to the end of your function like this:

    (function func() {
    $("#search").bind('submit', function () {
        var value = $('#str').val();
        $.post('db_query.php', {
            value: value
        }, function (data) {
            $("#search_results").html(data);
        }); return false;
      });
    })();
    
    点赞 打赏 评论

相关推荐