Click to See Complete Forum and Search --> : forms and javascript


nuthead
06-26-2003, 04:51 AM
Im needing to write a script that will take form entries from an onsubmit() request then return them to another field as one list.
I'm ok with most of this but as i'm going to dynamically write the script using PHP i will need to check if the fields (names 1,2,3,4,5 and so on) to see if the field contains any data and if it does it needs to be added to a new variable preceeded by a comma.

I hope this makes sense!

pyro
06-26-2003, 08:27 AM
something along these lines:

<html>
<head>

<script type="text/javascript">
function checkForm(frm) {
msg = "";
if (frm.field1.value != "") {
msg += frm.field1.value+", ";
}
if (frm.field2.value != "") {
msg += frm.field2.value+", ";
}
frm.field3.value = msg.substr(0, msg.length-2);
}
</script>


</head>

<body>
<form name="form1" onsubmit="checkForm(this);">
<input type="text" name="field1">
<input type="text" name="field2">
<input type="text" name="field3">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

nuthead
06-26-2003, 08:36 AM
is there anything yo don't know pyro??! :p

pyro
06-26-2003, 08:47 AM
lol... :D Yeah, lots. I just help where I can. ;)

nuthead
06-26-2003, 08:55 AM
ok, another question for you...

whats the meaning of life? :p

pyro
06-26-2003, 08:57 AM
lol... give me a few thousand more years, and then ask again. :D

nuthead
06-26-2003, 09:00 AM
ok, i'll make an entry on outlook to remind me

Vladdy
06-26-2003, 09:13 AM
Originally posted by pyro
something along these lines:
function checkForm(frm) {
msg = "";
if (frm.field1.value != "") {
msg += frm.field1.value+", ";
}
if (frm.field2.value != "") {
msg += frm.field2.value+", ";
}
frm.field3.value = msg.substr(0, msg.length-2);
}

Ouch,
...first, what you gonna do with a form that has 50 fields ???
...second, your empty test will fail when a space is entered...
here is what you wanna do:

function form2String(formName)
{ forms = document.getElementsByTagName('form');
if(forms[formName]) var form = forms[formName];
else return null;
var fstr='';
var empty=/^\s*$/;
for(i=0; i<form.elements.length; i++)
if(!empty.test(form.elements[i].value))
fstr+= i>0?', ':'' + form.elements[i].value;
return fstr;
}

nuthead
06-26-2003, 09:15 AM
its ok, im getting my script to make as many is needed (using php) and the field is only going to have numbers in it so its ok.

SlankenOgen
06-26-2003, 09:31 AM
The meaning of life is simple-

var Food = 1000000;
var Wise = false;
var die = false;

while(Food > 0)
{

if(!Wise){
Food--;
}

else{
die = true;
}
}
die = true;

nuthead
06-26-2003, 09:39 AM
aaah, that makes total sence!

congratulations!

cyberade
06-26-2003, 09:58 AM
To discover the true meaning of life, the universe and everything

http://www.bbc.co.uk/cult/hitchhikers/guide/answer.shtml

;)