doulin1867 2016-02-19 07:02
浏览 16
已采纳

一个php文件中的两个事件功能

Hi I'm new to php and jquery. Pardon my php vocabulary.

I have two events in my js file.

1) onsubmit: submits the user entered text to result.php which queries database and displays result. (result.php?name=xyz)

2) onkeyup: makes an ajax call to the same result.php which queries a url and gets json data. (result.php?key=xyz)

My question is if I can check for isset($_GET['key']) in result.php, query url and return json and the rest of the php is not parsed. Basically is there anything like return 0 as in case of C programming.

The question may seem silly, anyway I can have 2 different php files, but I want to know if it's possible. Thanks in advance :)

<form action = "result.php" method = "get">
<input type = "text" id = "name" >
<input type = " submit">
</form>
<script>
$('#name').on('keyup',function (e){
   input_val = $(this).val();
   $.ajax({
      url: "result.php?key=" + input_val,
      success: function(data){
      alert(data);
      }
  });
});

</script>
  • 写回答

3条回答 默认 最新

  • dongou4052 2016-02-19 07:15
    关注

    If I well understand, you want to know a way to use only one PHP script being able to process either Ajax and "normal" (returning whole page) tasks.

    So if yes, this can be easily achieve, using the following schema:

    //... some initialization, if needed
    if (isset($_GET['key'])) {
      // ... do the job for creating the expected Ajax response, say $ajax_response
      echo $ajax_response;
      exit;
      // nothing else will happen in the current script execution
    }
    // otherwhise you can do all "normal" job here, as usual...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源