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条)

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来