I’m working on a little project, but I’m stuck and don’t know what I’m doing wrong.
I use Ajax to call a php-script that updates a MYSQL-row.
The javascript:
function savecust()
{
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("custdetails").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","./ajax/savecust.php?id=" + document.getElementById('id').value,true); //More arguments should still be passed.
xmlhttp.send();
}
If I remove the document.getElementById('id').value the script works and does what is is supposed to do. (I get the response from the PHP, obviously
I load the script in the head of the page along with some other scripts:
<script src="js/custedit.js" type="text/javascript"></script>
This is how I call the script:
<input type='button' onclick='savecust()' value='Save' />
Any help would be greatly appreciated ;-).
Thanks!
EDIT
- The div where the content should go, exists.
- The PHP-scripts returns an error for now (because it doesn’t get all the required parameters)
- The content (and textfield with id: ‘id’) is loaded by another script. Is this a problem? If you look at the source in a browser the textfields are not there. They are however visible in the browser.
EDIT 2
So, I ran this test on top of my script:
var thisid = 'id';//should get info from <input type='text' name='id' />
var T = document.getElementById(thisid);
if (T) {
alert("element exists" + T.value);
} else {
alert("nothing to be found");
}
It is indeed unable to find the element. This pointed me in the right direction.
<input type='text' name='id' />is inserted by another Ajax-script. So I think the second script is unable to find it.
How do I solve this?
You cannot access an element property if it does not exist.
Perhaps a test: