使用Javascript提交并使用php获取表单数据

我有一个搜索栏,使用javascript函数在用户点击Enter时提交表单(工作正常) 因为它没有提交按钮,但我需要使用php来处理帖子上文本框中的数据。 表单正在提交,但是在帖子上它无法抓取搜索文本框中的内容。</ p>

以下是代码:</ p>

 &lt  ; form id =“siteWideSearch”name =“siteSearch”action =“&lt;?php echo htmlentities($ _ SERVER ['PHP_SELF']);?&gt;”  method =“post”&gt; 
&lt; input id =“homeSearch”type =“text”maxlength =“100”onkeypress =“startSiteSearch(event);” /&gt;
&lt; / form&gt;
</ code> </ pre>

Javascript:</ p>

  if(event.keyCode == 13  ){

document.getElementById(“siteWideSearch”)。submit();
}
</ code> </ pre>

PHP:</ p>

< pre> if($ _ SERVER [“REQUEST_METHOD”] ==“POST”){
echo“&lt; script type = \”text / javascript \“&gt; window.alert(\”Post arrival.Yay! !\“);&lt; / script&gt;”;
echo“&lt; script type = \”text / javascript \“&gt; window.alert(\”Search Criteria:“。terim($ _ POST ['homeSearch'] )。“\”);&lt; / script&gt;“;
}
</ code> </ pre>

我得到弹出文件说已经到达帖子,但第二个弹出窗口只输出 “搜索标准:”,别无其他。</ p>
</ div>

展开原文

原文

I have a search bar that uses a javascript function to submit the form when the user hits Enter (which is working) because it doesn't have a submit button, but I need to use php to handle the data in the textbox on post. The form is submitting, but on post it's not able to grab what was in the search textbox.

Here's the code:

<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>";
}

I get the popup saying that post was reached, but the second popup just outputs "Search Criteria: " and nothing else.

1个回答



您在表单输入中缺少name属性。 如果没有它,则不提交该值。</ p>

 &lt; input id =“homeSearch”type =“text”maxlength =“100”onkeypress =“startSiteSearch(event);”  /&gt; 
</ code> </ pre>

应为:</ p>

 &lt; input name =“homeSearch”id =“homeSearch”  type =“text”maxlength =“100”onkeypress =“startSiteSearch(event);”  /&gt; 
</ code> </ pre>
</ div>

展开原文

原文

You're missing the name attribute on your form input. Without it that value is not submitted.

<input id="homeSearch" type="text" maxlength="100" onkeypress="startSiteSearch(event);" />

should be:

<input name="homeSearch" id="homeSearch" type="text" maxlength="100" onkeypress="startSiteSearch(event);" />

duankui1532
duankui1532 哇,那很快哈哈。 你是对的,不知道我是怎么做到的。 非常感谢! (现在一直在和这个摔跤)
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问