Javascript HTML&;PHP-键入一个电话号码,然后拨打它

Javascript HTML&;PHP-键入一个电话号码,然后拨打它,javascript,php,html,phone-number,phone-call,Javascript,Php,Html,Phone Number,Phone Call,我写了一段代码,但不起作用。 代码: 在PHP中,字符串中的变量只能用双引号括起来。将单引号更改为双引号,以使$number在字符串中起作用 有关更多详细信息,请参阅 因此,您的代码应该如下所示: <?php $number = $_GET["tel"]; echo "<form action='/' method='get'> <a>Enter the phone number, what you want to c

我写了一段代码,但不起作用。

代码:


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

有关更多详细信息,请参阅

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

<?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和javascript。当用户点击submit按钮时,会调用javscript函数,但它还没有
$number
的任何值;因为只有在表单提交后才会设置。它会打开一个新页面,url为tel:,所以不会works@Bence8OS您的代码有点混乱:您试图将表单发送到PHP,PHP会将变量放入JS中。相反,您所需要的只是JS。我来举个例子。你根本不需要PHP
<?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>
        ";
?>
<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>