[英]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>
";
?>
但是这段代码很奇怪。 流程如下:
$_GET
变量tel
(假设表格已经发送) echo
显表格 $_GET["tel"]
中的数字, 而不是表单中的数字 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.