javascript - HTML & PHP - Type a telephone number, and call it - Stack Overflow

I wrote a code, but it doesn't work.Code:<?php$number = $_GET["tel"];echo '<

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

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>
';
Share Improve this question edited Aug 23, 2017 at 10:13 Vadim Kotov 8,2848 gold badges50 silver badges63 bronze badges asked Dec 27, 2015 at 13:27 Bence8OSBence8OS 111 gold badge1 silver badge2 bronze badges 1
  • 1 you're mixing php and javascript. when the user hits the submit-button, the javscript function gets called, but that doesn't have any value for $number yet; because this would only be set if after the form is submitted. – Jeff Commented Dec 27, 2015 at 13:33
Add a ment  | 

1 Answer 1

Reset to default 2

In PHP, variables in strings are only allowed in double quotes. Change your single quotes to double quotes to make the $number work in the string.

See this SO post for more details.

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)
  2. echo out the form
  3. on form submit, the number in the $_GET["tel"] is visited, not the number in the form
  4. then, the form gets submitted, but this doesn't work because the window.open() has occurred already

Here's an alternate solution without PHP (and without an actual form send):

<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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745392908a4625744.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信