目前有这个:
<script>
function copyToClipboard(info, text) {
window.prompt(info, text);
}
</script>
<?php
function getLink($user) {
return '<a class="clicky" onclick="copyToClipboard(
\'Copy to clipboard: Ctrl+C, Enter\nUse this on any forum with [img] tags!\',
\'site/pcard.php?user='.$user.'\');">
<span class="label label-primary">Get Link</span>
</a>';
}
?>
<div class="well">
<form method="post">
<label>Get Signature Image</label>
<input type="text" placeholder="Username..." name="signame" />
<button type="submit" class="btn btn-primary">Look-up</button>
<?php
if (isset($_POST)) {
getLink($_POST['signame']);
}
?>
</form>
我将如何继续使用发布的信息来调用该脚本?另外,这里还有其他错误吗?
最佳答案
注意到两件事:
$_POST
始终存在。因此isset($_POST)
始终为true
。您应该检查其中的参数是否存在(例如$_POST['signme']
)或检查其是否不为空(例如!empty($_POST)
)。getLink
函数本身并不真正打印结果。它只是返回您刚刚忽略的 HTML 字符串。您应该打印getLink
的返回结果。
我认为这就是您所需要的:
<script>
function copyToClipboard(info, text) {
window.prompt(info, text);
}
</script>
<?php
function getLink($user) {
return '<a class="clicky" onclick="copyToClipboard(
\'Copy to clipboard: Ctrl+C, Enter\nUse this on any forum with [img] tags!\',
\'site/pcard.php?user='.$user.'\');">
<span class="label label-primary">Get Link</span>
</a>';
}
?>
<div class="well">
<form method="post">
<label>Get Signature Image</label>
<input type="text" placeholder="Username..." name="signame" />
<button type="submit" class="btn btn-primary">Look-up</button>
<?php
if (isset($_POST['signame'])) {
print getLink($_POST['signame']);
}
?>
</form>
</div>
关于javascript - PHP - 在提交后调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25028004/