douliao5467 2012-11-22 04:01
浏览 208
已采纳

使用文本输入作为php变量

<input type="text" onkeydown="domaincheck()" />

<?php
   function domaincheck()
   {
      $recordexists = checkdnsrr("", "ANY");
      if ($recordexists) 
         echo "The domain name has been taken. Sorry!";
      else 
         echo "The domain name is available!";
   }
?>

I want my php code to process the text input and say if it's a valid domain or not. The only thing that doesn't work is linking the input to the php.

  • 写回答

3条回答 默认 最新

  • drus39136 2012-11-22 04:52
    关注

    That's not how PHP works. You're making a common thinking error, that a lot of beginners make, in that you seem to think that a client-side language like Javascript, can interact with a server-side language, such as PHP, in this fashion. This is due to a misunderstanding of the client/server and/or request/response model.

    When you request a web page with your browser (the client), your request is processed by the webserver (the server), that then returns a response, basically like this:

       Client (browser)             Server (webserver)
    ---------------------------------------------------
       Send page request   ----->    Parse request
                                     (execute PHP)
    
                                           |
                                          \|/
    
    Parse HTML response     <-----   Send HTML response
       (and execute                  (there will be no
        Javascript,                   more PHP code in
      if that was part                 this response)
      of the response)
    

    In other words: the client and server are two completely separated processes, that only communicate with each other based on a request/response principle.

    PHP cannot be executed on the client-side (save for the client-side version PHP-GTK, which we are not discussing here), and Javascript cannot be executed on the server-side (save for server-side versions of Javascript, which we are not discussing here). So, the PHP function domaincheck will not be available for the client to call, and the Javascript calls will not be received by the server.

    And so, if you don't want to request a complete web page again, but merely have a Javascipt function request and receive the output of a PHP function, you'd have to let Javascipt make another request to the server again ('in the background', meaning: without completely replacing the whole page in the browser), and have PHP create a response again, this time with output that Javascript can handle. This is typically known as AJAX (Asynchronous JavaScript and XML). Although the response doesn't have to be XML. A JSON response is very common. Fragments of HTML (or even complete HTML pages) are also possible.

    I'll probably add an example for you at some point in the near future.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(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跑不出来