javascript return result by id.....

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
azhan
Forum Commoner
Posts: 68
Joined: Fri Jun 27, 2008 6:05 am

javascript return result by id.....

Post by azhan »

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
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Re: javascript return result by id.....

Post by arjan.top »

input.value = xmlhttp.responseText;

where input is DOM input element
azhan
Forum Commoner
Posts: 68
Joined: Fri Jun 27, 2008 6:05 am

Re: javascript return result by id.....

Post by azhan »

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
azhan
Forum Commoner
Posts: 68
Joined: Fri Jun 27, 2008 6:05 am

Re: javascript return result by id.....

Post by azhan »

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;
}
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: javascript return result by id.....

Post by kaszu »

What arjan said was that inputs doesn't have innerHTML, but has value property.

Code: Select all

document.getElementById("tot").value=xmlhttp.responseText;
Post Reply