通过POST使用ajax传递多个参数到php [英] Passing multiple parameters by POST using ajax to php

查看:139
本文介绍了通过POST使用ajax传递多个参数到php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过POST方法使用AJAX将多个参数传递给我的PHP文件,以便我可以查询MySQL数据库。

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.

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>

这是我成功访问下拉值和文本框值并将其存储在<$ c中的Javascript文件$ c> sv 和 searchtext11 变量。但问题是将两个值传递给PHP文件。问题似乎出现在 the_data 变量中,该变量在 xmlhttp.send(the_data)中传递;

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);

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

此PHP代码仅适用于

var the_data='select='+sv;

searchone.php

searchone.php

<?php   


if (isset($_POST['select'])) {
    $str = $_POST['select'];    // get data

    echo $str;
}
?>

如何同时获取我的PHP文件的下拉列表和文本框值,以便我可以使用这些变量。

How do i get both dropdown and textbox value to my PHP file so that i can form sql query using those variables.

推荐答案

您需要使用&符号,就像它是 GET 一样。此外,您应 编码您的文字 以确保它不包含任何可能具有特殊含义的字符

You need to use an ampersand, just as if it was a GET. Further, you should encode your text to make sure that it doesn't contain any characters that could have a special meaning

var the_data = ''
    + 'select=' + window.encodeURIComponent(sv)
    + '&text=' + window.encodeURIComponent(searchtext11);
//     ^                    ^^

您无需手动解码服务器端,因为你已经知道 POST 数据是 x-www-form-urlencoded

You don't need to decode manually server-side because you are already letting it know that POST data is x-www-form-urlencoded.

这篇关于通过POST使用ajax传递多个参数到php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆