Reputation: 75
I have a simple Javascript function that send asyncronous POST request to 2 different php script. Here's a simple code:
<script type="text/javascript">
function send(oForm, what)
{
var ios3 = oForm.ios3.checked;
var ios4 = oForm.ios4.checked;
var ios5 = oForm.ios5.checked;
var id = oForm.id.value;
if (what == confirm)
{
if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}// code for IE7+, Firefox, Chrome, Opera, Safari
else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}// code for IE6, IE5
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var output = xmlhttp.responseText;
if (output != ""){document.getElementById("debug").innerHTML=output; document.getElementById(id).style.display = 'none';}
}
}
xmlhttp.open("POST","ajax/confirm.php",true);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send('id=' + encodeURIComponent(id) + '&ios3=' + encodeURIComponent(ios3) + '&ios4=' + encodeURIComponent(ios4) + '&ios5=' + encodeURIComponent(ios5));
}
if (what == remove)
{
if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}// code for IE7+, Firefox, Chrome, Opera, Safari
else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}// code for IE6, IE5
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var output = xmlhttp.responseText;
if (output != ""){document.getElementById("debug").innerHTML=output; document.getElementById(id).style.display = 'none';}
}
}
xmlhttp.open("POST","ajax/confirm.php",true);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send('id=' + encodeURIComponent(id) + '&ios3=' + encodeURIComponent(ios3) + '&ios4=' + encodeURIComponent(ios4) + '&ios5=' + encodeURIComponent(ios5));
}
}
and I call this function with these simple input button (without the "\" I have added these for php echo)
<input type=\"button\" id=\"submit\" value=\"Confirm\" onclick=\"send(this.form, confirm)\" />
<input type=\"button\" id=\"remove\" value=\"Remove\" onclick=\"send(this.form, remove)\" />
Can someone explain me why it works only for the 1st button?
Upvotes: 0
Views: 459
Reputation: 1344
'Probably because confirm
is interpreted as the javascript function confirm()
. And it actually returns something. And I assume remove
is undefined in this case. Try passing a string as second argument and comparing the string to another string. So it would be like
<input type=\"button\" id=\"submit\" value=\"Confirm\" onclick=\"send(this.form, 'confirm')\" />
<input type=\"button\" id=\"remove\" value=\"Remove\" onclick=\"send(this.form, 'remove')\" />
And
if (what == "confirm")
{
...
}
if (what == "remove")
{
...
}
Upvotes: 1