php 使用ajax通过POST将多个参数传递给php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17382579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing multiple parameters by POST using ajax to php
提问by akashaggarwaal
I'm trying to pass multiple parameters via the POST method using AJAX to my PHP file so that I can make a query to a MySQL database.
我正在尝试使用 AJAX 通过 POST 方法将多个参数传递到我的 PHP 文件,以便我可以查询 MySQL 数据库。
HTML file :
HTML 文件:
<div class="dropdown dropdown-dark">
<select class="dropdown-select" id="searchselect11" required>
<option value="faculty">Faculty</option>
<option value="dept">Dept.</option>
<option value="course">Course</option>
<option value="year">Year</option>
<option value="name">Name</option>
</select>
</div>
<td style="padding:5px;"> <input type="text" id="searchtext11" required></td>
<button id="searchgo1" onclick="searchone()"></button>
This is the Javascript file where I successfully access dropdown value and textbox value and store them in sv
and searchtext11
variables respectively. But the problem is to pass the two values to the PHP file. The problem seems to be in the_data
variable that is passed in xmlhttp.send(the_data);
这是我成功访问下拉值和文本框值并将它们分别存储在sv
和searchtext11
变量中的 Javascript 文件。但问题是将这两个值传递给 PHP 文件。问题似乎出在the_data
传入的变量中xmlhttp.send(the_data);
The searchone()
function is as follows:
该searchone()
功能如下:
function searchone()
{
//alert("hi");
var xmlhttp;
var sel = document.getElementById('searchselect11');
var sv = sel.options[sel.selectedIndex].value;
var searchtext11= document.getElementById("searchtext11").value;
var the_data = 'select='+sv+'text='+searchtext11;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST", "searchone.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(the_data);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
document.getElementById("searchresults").innerHTML = xmlhttp.responseText;
}
}
}
This PHP code works only if
此 PHP 代码仅适用于
var the_data='select='+sv;
searchone.php
搜索引擎.php
<?php
if (isset($_POST['select'])) {
$str = $_POST['select']; // get data
echo $str;
}
?>
How do i get both dropdown and textbox value to my PHP file so that i can form sql query using those variables.
我如何将下拉列表和文本框值都添加到我的 PHP 文件中,以便我可以使用这些变量形成 sql 查询。
回答by Paul S.
You need to use an ampersand, just as if it was a GET. Further, you should encode your textto make sure that it doesn't contain any characters that could have a special meaning
您需要使用&符号,就像它是GET 一样。此外,您应该对文本进行编码以确保它不包含任何可能具有特殊含义的字符
var the_data = ''
+ 'select=' + window.encodeURIComponent(sv)
+ '&text=' + window.encodeURIComponent(searchtext11);
// ^ ^^
You don't need to decode manually server-side because you are already letting it know that POSTdata is x-www-form-urlencoded
.
您不需要在服务器端手动解码,因为您已经让它知道POST数据是x-www-form-urlencoded
.
回答by vladkras
add & in this string:
在此字符串中添加 &:
var the_data = 'select='+sv+'&text='+searchtext11;