我有一个搜索栏,当用户按 Enter 键(正在运行)时,它使用 JavaScript 函数提交表单,因为它没有提交按钮,但我需要使用 php 来处理文本框中的数据在帖子上。表单正在提交,但在发布时无法获取搜索文本框中的内容。
代码如下:
<form id="siteWideSearch" name="siteSearch" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<input id="homeSearch" type="text" maxlength="100" onkeypress="startSiteSearch(event);" />
</form>
Javascript:
if (event.keyCode == 13) {
document.getElementById("siteWideSearch").submit();
}
PHP:
if($_SERVER["REQUEST_METHOD"] == "POST") {
echo "<script type=\"text/javascript\">window.alert(\"Post reached. Yay!!\");</script>";
echo "<script type=\"text/javascript\">window.alert(\"Search Criteria: ".trim($_POST['homeSearch'])."\");</script>";
}
我看到弹出窗口说帖子已到达,但第二个弹出窗口只输出“搜索条件:”,没有其他内容。
最佳答案
您的表单输入中缺少名称属性。如果没有它,则不会提交该值。
<input id="homeSearch" type="text" maxlength="100" onkeypress="startSiteSearch(event);" />
应该是:
<input name="homeSearch" id="homeSearch" type="text" maxlength="100" onkeypress="startSiteSearch(event);" />
关于javascript - 使用 Javascript 提交并使用 php 获取表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22516489/