2010-11-15 94 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