Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
修改javascript以获得两个查询字符串_Javascript_Ajax - Fatal编程技术网

修改javascript以获得两个查询字符串

修改javascript以获得两个查询字符串,javascript,ajax,Javascript,Ajax,我有一个javascript代码,它将输入栏中输入的值发送到一个php文件,该文件根据输入字段中输入的值从数据库中获取行。我需要编辑此代码,以便javascript不仅将输入栏中输入的值发送到php,还发送另一个javascript变量 代码如下: function showFriends(str) { var cake = document.getElementById("cake"); if(str.length == 0) { document.

我有一个javascript代码,它将输入栏中输入的值发送到一个php文件,该文件根据输入字段中输入的值从数据库中获取行。我需要编辑此代码,以便javascript不仅将输入栏中输入的值发送到php,还发送另一个javascript变量

代码如下:

function showFriends(str)
{
    var cake = document.getElementById("cake");

    if(str.length == 0)
    { 
        document.getElementById("livesearch1").
        innerHTML = "";
        document.getElementById("livesearch1").
        style.border="0px";
        return
    }

    xmlHttp=GetXmlHttpObject()


    var url="petition_send.php"
    url=url+"?q="+str
    url=url+"&sid="+Math.random()
    xmlHttp.onreadystatechange=stateChanged 
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
} 

function stateChanged() 
{ 
    if(xmlHttp.readyState == 4 || xmlHttp.readyState=="complete")
    { 
        document.getElementById("livesearch1").
        innerHTML=xmlHttp.responseText;
        document.getElementById("livesearch1").
        style.border="1px solid #A5ACB2";
    } 
}

function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
    {
    //Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
    //Internet Explorer
    try
    {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    }
        return xmlHttp;
    }
我需要var url等效于“呈请发送.php?呈请ID=cake&”

但是,出于某种原因,当我这样编写代码时,代码不起作用


有什么想法吗?

如果jquery是一个选项,您可以使用

$("#mybutton").click(function(){
   $("#livesearch1").load("petition_send.php", 
      { "q" : $("#cake").text(),
        "sid" : Math.random() }).css("border","0px");
});
使用jQuery时,哪一个要简单得多

$('#livesearch1').load('petition_send.php', 
  { q: $('#cake').value(), sid: Math.random() },
  function() { $('#livesearch1').addClass('with-border'); }
);

您是否正在尝试向当前查询字符串添加另一个参数(呈请ID),并将呈请ID的值设置为var值

function showFriends(str)
{
    var cake = document.getElementById("cake");

    if(str.length == 0)
    { 
        document.getElementById("livesearch1").innerHTML = "";
        document.getElementById("livesearch1").style.border="0px";
        return;
    }

    xmlHttp=GetXmlHttpObject();


    var url="petition_send.php";
    url=url+"?q="+str;
    // additional param
    url=url+"&petition_ID="+cake;
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
} 

写得像什么?提供一个例子。如果您使用像jquery、yui、mootools等这样的ajax库,编写起来会容易得多。。。它们是选项吗?你的分号在哪里?!?!?!(分号插入#ftf)@Jacob Relkin,AFAIK有一个地方JavaScript的分号自动插入很有用:code golf。