簡體   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