将多个参数从 Javascript 传递给 PHP

New*_*Dev 3 javascript php

我正在尝试使用这个

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

function CallSomePHP()
{
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
    {
    alert ("Browser does not support HTTP Request");
    return;
    }
    var url="myPhp.php"; ***(Need to Pass multiple parameter to php from here)***
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
}

function stateChanged()
{
    if (xmlhttp.readyState==4)
    {
        alert(xmlhttp.responseText); 
Run Code Online (Sandbox Code Playgroud)

jan*_*mon 5

这真的很容易:

var url="myPhp.php?param1="+ param1 + "&param2=" + param2
Run Code Online (Sandbox Code Playgroud)

但是,您可能会考虑使用 jQuery。

因为它会变得更容易;)

要进行完整的 ajax 调用,您只需要一个方法调用,而不必关心浏览器问题。所以你的代码变得更容易阅读。

 $.ajax({
   // you can use post and get:
   type: "POST",
   // your url
   url: "some.php",
   // your arguments
   data: {name : "John", location : "Boston"},
   // callback for a server message:
   success: function( msg ){
     alert( "Data Saved: " + msg );
   },
   // callback for a server error message or a ajax error
   error: function( msg )
   {
     alert( "Data was not saved: " + msg );
   }
 });
Run Code Online (Sandbox Code Playgroud)


And*_*y E 5

您将它们添加到 URL 字符串中,因此:

var url="myPhp.php?a=1&b=2&c=3";
Run Code Online (Sandbox Code Playgroud)

然后你可以在 PHP 中从 $_GET 数组访问它们:

$Param1 = $_GET['a']; // = 1
$Param2 = $_GET['b']; // = 2
$Param3 = $_GET['c']; // = 3
Run Code Online (Sandbox Code Playgroud)