Click to See Complete Forum and Search --> : form is not an object
piersk
07-28-2003, 05:15 AM
First of all, let me attempt to explain what it is I'm trying to do:
I want to be able to add company details to a database by using Javascript to store the values of a form in a cookie, then open a pop-up window for the user to enter the details in, then when "submit" is clicked, i want the previous form to refresh and the company list to be updated, and all the previously entered details in the form to still be there once the form has been refreshed (using the values stored in the cookie).
The writing of the cookie seems to go ahead as planned (i.e. there are no errors and the cookie appears on my computer), but when I try to write the cookie back, I get the errorparent.document.forms.0 is null or not an object
This is the first thing that confuses me, since the line in my code that is supposed to write to the form is:parent.document.forms[0].elements = array and the line used to write the cookie is:array = document.forms[0].elements; (array being the value that is then written to the cookie).
Can anyone help me?
You need to either use a loop, or write in each element by using document.forms[0].elements[0].value = "text";
[edit - If that sounds a bit cryptic and you need me to explain it differnently, let me know... I was on my way out the door when I wrote that... ]
piersk
07-28-2003, 09:14 AM
Is it possible to store an object (i.e. document.forms[0].elements) as a value in a cookie to be read later?
Khalid Ali
07-28-2003, 09:19 AM
no you can not store an object in a cookie(since cookies are just text fiels)
piersk
07-28-2003, 09:19 AM
what about an array?
Khalid Ali
07-28-2003, 09:36 AM
yes in an array you can store objects..
piersk
07-28-2003, 09:38 AM
Sorry, didn't make myself clear. Can you store an array in a cookie?
Khalid Ali
07-28-2003, 09:55 AM
not as an object,it has to be a string....
piersk
07-28-2003, 09:58 AM
Ok, so let me get my head round this:
Can I use a for loop to get all the values from a form (using document.forms[0].elements[i].value) and put this into an array (since all the values are strings), then store this array in a cookie, and then later, read this cookie and use another for loop to get the values back into the element value properties of the form?
Khalid Ali
07-28-2003, 10:11 AM
put only string values in the cookies.
you can run a loop through the form element and create a string may be something like this
str+=document.forms[0].elements[x].value;
and then dump this str variable in the cookie
Something like this should help you out:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function setVals(frm) {
str = "";
for (i=0; i<frm.elements.length-1; i++) {
str += frm.elements[i].value+"<break>";
}
expdate = new Date()
expdate.setFullYear(expdate.getFullYear() + 1);
document.cookie = "formvals=" + str + "; expires-" + expdate.toGMTString();
}
function loadVals() {
allcookies = document.cookie;
if (allcookies) {
pos = allcookies.indexOf("formvals=");
if (pos != -1) {
start = pos + 9;
end = allcookies.indexOf(";", start);
if (end == -1) {
end = allcookies.length;
}
value = allcookies.substring(start, end);
value = value.split(/\<break\>/);
for (i=0; i<value.length; i++) {
alert (value[i]); //swap this out with the code to fill out your form
}
}
}
}
</script>
</head>
<body onload="loadVals();">
<form name="myform" action="">
<p><input type="text" name="inputone">
<input type="text" name="inputtwo">
<input type="button" value="Set Values" onclick="setVals(this.form);"></p>
</form>
<p><a href="#" onclick="location.reload(true); return false;">Reload</a></p>
</body>
</html>BTW, are you the piersk from the PHP Builder forums?
piersk
07-28-2003, 11:13 AM
Yeah, I am. My PHP might be ok, but my javascript is most certainly not:D
Lol... Well, I started with javascript, and moved on to PHP. So, my JS is better than my PHP (but I'm working to change that :D...)
Go PHP!
piersk
07-28-2003, 11:17 AM
Amen to that bro!