Hey guys...
I was able to do real time two integer multiplication using ajax which is combination of php and javascript where by the result will return at the same page without refreshing or pressing a button.
the result returned by javascript declaring the id like ->> document.getElementById("tot").innerHTML=xmlhttp.responseText;
so...when i put <div id='tot'></div> back in my php page...it succesfully return the correct multiplication result...
the problem here is....i wanted to display the result in input field like this
<input type="varchar" name="num1" id="tot" />
but the result doesnt display in input field in the form...but in <div> tag it can be displayed...
does anyone know this?
thanks
azhan
javascript return result by id.....
Moderator: General Moderators
Re: javascript return result by id.....
input.value = xmlhttp.responseText;
where input is DOM input element
where input is DOM input element
Re: javascript return result by id.....
thanks arjan....
but im not quite there...
do i have to replace
document.getElementById("tot").innerHTML=xmlhttp.responseText;
with
document.input.value = xmlhttp.responseText; ?
but how do i assign it to particular input field without using id?
thanks
but im not quite there...
do i have to replace
document.getElementById("tot").innerHTML=xmlhttp.responseText;
with
document.input.value = xmlhttp.responseText; ?
but how do i assign it to particular input field without using id?
thanks
Re: javascript return result by id.....
here is my code...
/////////////php page
<select name="medlist1" size ="1" onchange="showUser2(this.value)" id="medlist1">
<option selected="selected" value="">Choose</option>
<?php
$select = mysql_query("SELECT id,name FROM ubat ORDER BY name");
while ( $get = mysql_fetch_array($select)) {
$name = $get['name'];
$id = $get['id'];
echo "<option value='$id'>$name</option>\n";
}
?>
</select>
<input type="varchar" name="num1" id="tot" > ///expected result to be return here
////////////////// ajax code
var xmlhttp;
function showUser2(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="getharga.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged2;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged2()
{
if (xmlhttp.readyState==4)
{
document.getElementById(tot).value=xmlhttp.responseText;
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
/////////////php page
<select name="medlist1" size ="1" onchange="showUser2(this.value)" id="medlist1">
<option selected="selected" value="">Choose</option>
<?php
$select = mysql_query("SELECT id,name FROM ubat ORDER BY name");
while ( $get = mysql_fetch_array($select)) {
$name = $get['name'];
$id = $get['id'];
echo "<option value='$id'>$name</option>\n";
}
?>
</select>
<input type="varchar" name="num1" id="tot" > ///expected result to be return here
////////////////// ajax code
var xmlhttp;
function showUser2(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="getharga.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged2;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged2()
{
if (xmlhttp.readyState==4)
{
document.getElementById(tot).value=xmlhttp.responseText;
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
Re: javascript return result by id.....
What arjan said was that inputs doesn't have innerHTML, but has value property.
Code: Select all
document.getElementById("tot").value=xmlhttp.responseText;