繁体   English   中英

用户输入邮政编码时,如何在另一个文本框中动态填充地址?

[英]How can I dynamically populate an address in another textbox when user enter zip code?

当用户在另一个文本框(postalCode)中键入邮政编码时,我试图将地址从getAdd($ Url)函数动态填充到文本框(addressD)。 但是根本没有值填充到addressD文本框中。 谁能告诉我怎么了?

<?php
  function getAdd($Url){

      if (!function_exists('curl_init')){
          die('cURL is not installed. Install and try again.');
      }

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, 'http://gothere.sg/a/search?q='.$Url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $output = curl_exec($ch);
      curl_close($ch);


        $trimData = strstr($output, 'locf');
        $extractData = substr($trimData, 13);
        $findPos = strpos($extractData, '<');
        $extractData2 = substr($extractData, 0, $findPos);

        echo "<script type="text/javascript">";
        echo "document.getElementByID('addressD').value = $extractData2";
        echo "</script>";
  }
?>

<html>
<body>
    <form id="contact-form" method="post">
       Postal Code*:
       <input type="text" name="postalCode" value="" onkeypress="getAdd(this.value)" />    

       Address*:
       <input type="text" name="addressD" id="addressD" value="" />    
    </form> 
</body>
</html>

当您按下按钮时,浏览器将尝试执行一个名为getAdd()的javascript函数。 但是,您已经创建了一个名为getAdd()的php函数-无法使用。 如果不使用ajax调用,则无法从浏览器调用服务器端函数。 php函数永远不会发送到用户的浏览器,服务器会对其进行解析,然后将其下方的html发送出去。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM