简体   繁体   English

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

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

I wrote a code, but it doesn't work. 我写了代码,但是没有用。

Code: 码:

<?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>
';

In PHP, variables in strings are only allowed in double quotes . 在PHP中,字符串中的变量只能用双引号引起来 Change your single quotes to double quotes to make the $number work in the string. 将单引号更改为双引号,以使$number在字符串中起作用。

See this SO post for more details. 有关更多详细信息,请参见此SO帖子

Therefore, your code should look like: 因此,您的代码应如下所示:

<?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>
        ";
?>

But this code is strange. 但是这段代码很奇怪。 Here's how it flows: 流程如下:

  1. get $_GET variable tel (assuming form has already been sent) 获取$_GET变量tel (假设表格已经发送)
  2. echo out the form echo显表格
  3. on form submit, the number in the $_GET["tel"] is visited, not the number in the form 在提交表单时,访问的是$_GET["tel"]中的数字, 而不是表单中的数字
  4. then, the form gets submitted, but this doesn't work because the window.open() has occurred already 然后,提交表单,但这不起作用,因为window.open()已经发生

Here's an alternate solution without PHP (and without an actual form send): 这是一个没有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>

See it working at JSFiddle.net . 可以在JSFiddle.net上看到它的工作。

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

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