douchunjing6587 2014-04-09 17:00
浏览 22
已采纳

如何激活特定的PHP包括

I have many PHP includes on my webpage; I don't want them active until a button is clicked or someone hits enter.

AND then I would like to call the specific DIV ID [php include] and not all of them on the page.

I have an input field that the user would enter a zip code, then hit the go button or enter key, after that happens; I would like the script to activate the php include for that div ID.

For example: I have a - div id="zip60538" - that has a php include - [include myfile.php]

which I don't want activated on page load, I only want the DIV ID php include activated after the go button or enter is hit.

There will be a 100 div id's with PHP includes so I would like to be specific and only fire the matching DIV ID PHP Include and not all of them on the page. Each DIV is setup as zip(then the actual zip code) so zip60538 is the div ID and the user would enter 60538 in the input field.

What I was thinking is: 1. look at input box 2. if zip+(input box values) matches 3. then activate my.php include, in div id zip60538

is that possible?

 $(document).ready(function(){
var zipCodes   =[60538,60504,75001,76008,75002,75013,76009,76225,75409,76226,76001,76002,76006,76010,
76011,76012,76013,76014,76015,76016,76017,76018,76227,76020,75413,75180,75101,76021,
76022,75414,76126,75424,75418,76023,76028,75135,75006,75007,75010,76127,75104,75423,
75009,75105,76031,75211,76034,76233,75121,75019,75114,76035,76036,];
$("#buttontest").click(function(){
 var zipIndex = zipCodes.indexOf(parseInt($("#full_day").val()));
 $("#zipMessage > div").hide("fast");
 var zipId = zipIndex > -1 ? zipCodes[zipIndex] : "Error";
$("#zip"+zipId).show("fast");
});

$("#full_day").keydown(function(e){
  if(e.which === 13){
     $("#buttontest").click();
}
});
});

http://jsfiddle.net/7SPGh/35

  • 写回答

1条回答 默认 最新

  • dqaxw44567 2014-04-09 17:22
    关注

    PHP runs on the server and by the time the browser loads your web page, PHP has "already done its thing" and can no longer hold an open conversation with the browser.

    That being said, your best option here is to use AJAX. And in keeping things as simple as possible, I recommend using jQuery's .ajax() method.

    This way, once a user takes an action, you can handle that event using JavaScript, call something from the PHP on the server, and return it back to the browser for use (essentially creating an open conversation between the browser and the server).

    An example would look like this:

    $("#some_button").on('click', function(e) {
        $.ajax({
            type: 'POST',
            url: 'some_php_script.php',
            data: {foo : "bar"}
        })
        .done(function(results) {
            // check to see if results is actually html
            // going to assume it is for the sake of example
             $("#zip60538").html(results);
        });
    });
    

    You mentioned you have hundreds of separate includes, so creating a basic function that takes arguments and abstracts this functionality would be best for your particular case. AJAX is the way to go. Good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求帮,直连能连上oracle12,但是thinkphp6就是报错
  • ¥15 paddleocr运行报错
  • ¥15 怎么用 matlab 设计滞后-超前串联校正网络
  • ¥15 MFC引用C#生成的dll,将dll放置到非exe程序目录,如何操作
  • ¥15 C#创建webservice接口,三方通过多次跳转访问本方服务,获取wsdl文档,wsdl中ip地址为局域网内本机地址而非三方直接访问的地址。
  • ¥15 关于#wireshark#的问题:需要安卓app流量数据集要安卓流量做包序列长度的实验,比如某些流量是在看视频还是在发评论
  • ¥15 Smail语句如何使用判断语句跳过验证卡密界面
  • ¥15 关于#wireshark#的问题:并且能够给数据做标注,如这个流量是在看视频或者是在转账
  • ¥15 运筹优化,gurobi,python
  • ¥20 springmvc重定向和返回json