从Javascript传递多个参数到PHP

从Javascript传递多个参数到PHP

本文介绍了从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); 


推荐答案

这很简单:

var url="myPhp.php?param1="+ param1 + "&param2=" + param2

但是你可以考虑使用jQuery。

However you might consider using jQuery.

因为它会变得更容易;)

As it would become even easier ;)

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

To have a complete ajax call you need only one method call without having to care about browser issues. So your code becomes a lot easier to read.

 $.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 );
   }
 });

这篇关于从Javascript传递多个参数到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 13:30