I want to use javascript to compute values without pressing on the submit button. Using the javascript onkeyup event.
Is it possible to send 2 or more values from an html form to a javascript function?
This is the code for the loading of the necessary details on a table:
echo "<input type=\"hidden\" name=\"ids[]\" value=\"$id\">";
echo "<input type=\"hidden\" name=\"qoh[]\" value=\"$qtyhand\">";
echo "<td><input type=\"text\" name=\"qbuys[]\" id=\"qbuys\" value=\"$qtytbuy\" onKeyUp=\"proc(this.value)\"></td>";
I tried (which I only made up), but didn’t work, what’s the proper way of doing this,is it possible?:
onKeyUp=\"proc(this.value && document.cartform.qbuys[].value);\"
And this is the javascript that acts as a mediator between the form and the php file that computes the subtotal:
function proc(str,str2)
{
if (str=="")
{
document.getElementById("compz").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("compz").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","compz.php?qbuys="+str&prodid="+str2,true);
xmlhttp.send();
}
Although I’m not really sure if it works, since I’m only using 1 parameter for the function before.
First of all, I would recommend using jQuery instead of rolling your own AJAX interface. It’s tried and tested and used by the giants of the Internet. Here’s a simple example from the jQuery documentation adapted to your structure:
That line would load any content returned by “some_page.php” into your “compz” DIV. So let’s re-write your function:
That way your PHP remains unchanged.