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!

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

报告相同问题?

悬赏问题

  • ¥15 铜与钢双金属板叠加在一起每种材料300mm长,18mm宽,4mm厚一端固定并加热至80℃,当加热端温度保持不变时另一端的稳态温度。ansys
  • ¥15 django5安装失败
  • ¥15 Java与Hbase相关问题
  • ¥15 后缀 crn 游戏文件提取资源
  • ¥15 ANSYS分析简单钎焊问题
  • ¥20 bash代码推送不上去 git fetch origin master #失败了
  • ¥15 LOL外服加入了反作弊系统,现在游戏录像rofl文件离线都无法打开
  • ¥15 在centos7安装conda
  • ¥15 c#调用yolo3 dll文件获取的数据对不上
  • ¥20 WPF 如何实现多语言,label 和cs(live Charts)中是否都能翻译