繁体   English   中英

HTML&PHP-输入电话号码,然后呼叫

[英]HTML & PHP - Type a telephone number, and call it

我写了代码,但是没有用。

码:

<?php
$number = $_GET["tel"];
echo '<form action="/" method="get">
         <a>Enter the phone number, what you want to call</a>
         <input type="tel" name="tel"/>
         <input type="submit" onclick="call()" value="Call"/>
      </form>
      <script>
          function call() {
              window.open("tel:$number");
          }
      </script>
';

在PHP中,字符串中的变量只能用双引号引起来 将单引号更改为双引号,以使$number在字符串中起作用。

有关更多详细信息,请参见此SO帖子

因此,您的代码应如下所示:

<?php
    $number = $_GET["tel"];
    echo "<form action='/' method='get'>
                <a>Enter the phone number, what you want to call</a>
                <input type='tel' name='tel' />
                <input type='submit' onclick='call()' value='Call' />
            </form>
            <script>
                function call() {
                    window.open('tel:$number');
                }
            </script>
        ";
?>

但是这段代码很奇怪。 流程如下:

  1. 获取$_GET变量tel (假设表格已经发送)
  2. echo显表格
  3. 在提交表单时,访问的是$_GET["tel"]中的数字, 而不是表单中的数字
  4. 然后,提交表单,但这不起作用,因为window.open()已经发生

这是一个没有PHP(也没有实际发送表单)的替代解决方案:

<form action='/' method='get'>
<a>Enter the phone number, what you want to call</a>
    <input type='tel' name='tel' />
    <input type='submit' onclick='call();return false;' value='Call' />
</form>
<script>
    function call() {
        var number = document.querySelector("input[name=tel]").value;
        window.open('tel:' + number);
    }
</script>

可以在JSFiddle.net上看到它的工作。

暂无
暂无

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

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