2010-11-15 95 views
-1

我有这个JavaScript代码,它将输入栏中输入的值发送到一个php文件,该文件根据输入字段中输入内容的值从数据库中提取行。我需要编辑此代码,以便JavaScript不仅将正在输入栏中输入内容的值发送给php,还会发送另一个JavaScript变量。修改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等同于 “petition_send.php petition_ID =蛋糕&?”

,但是由于某种原因,没有按代码”当我这样写时,它就会起作用。

有什么想法?

+0

写它像什么?举一个例子。 – Hamish 2010-11-15 19:29:17

+0

如果你使用像jquery yui mootools等ajax库,它会容易得多...它们是一个选项吗? – generalhenry 2010-11-15 19:30:01

+2

你的SEMICOLONS是什么?!?!?! (分号插入#ftf) – 2010-11-15 19:30:14

回答

0

如果jQuery是一个选项,你可以使用类似

$("#mybutton").click(function(){ 
    $("#livesearch1").load("petition_send.php", 
     { "q" : $("#cake").text(), 
     "sid" : Math.random() }).css("border","0px"); 
}); 

这是很多简单

0

使用jQuery:

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

你是不是想添加其他参数(petition_ID)到当前查询字符串并将petition_ID的值设置为var cake的值?

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); 
} 
+0

Jacob Relkin是对的。缺少分号。 – 2010-11-15 19:48:11

+0

谢谢!这很好! – Lance 2010-11-15 19:59:15