[英]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:
流程如下:
$_GET
variable tel
(assuming form has already been sent) $_GET
变量tel
(假设表格已经发送) echo
out the form echo
显表格 $_GET["tel"]
is visited, not the number in the form $_GET["tel"]
中的数字, 而不是表单中的数字 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.