I have this javascript code that sends the value of what is entered in the an input bar to a php file that fetches rows from a database according to the value of what is entered into the input field. I need to edit this code so that the javascript not only sends the value of what is being entered into the input bar to the php, but also another javascript variable.
The code is as follows:
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";
}
}开发者_StackOverflow社区
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;
}
I need the var url to be equivalent to "petition_send.php?petition_ID=cake&"
but, for some reason, the code doesn't work when I write it like that.
Any thoughts?
if jquery is an option you can use something like
$("#mybutton").click(function(){
$("#livesearch1").load("petition_send.php",
{ "q" : $("#cake").text(),
"sid" : Math.random() }).css("border","0px");
});
which is a lot simpler
Using jQuery:
$('#livesearch1').load('petition_send.php',
{ q: $('#cake').value(), sid: Math.random() },
function() { $('#livesearch1').addClass('with-border'); }
);
Are you trying to add another parameter (petition_ID) to the current query string and set the value of petition_ID to the value of 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);
}
精彩评论