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

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题