0

Im new here, Im new in programming as well and I seldom code anything, but recently I've always wanted to improve my work place and I just want to create a simple form for my colleagues.

To make things short, I've created the form, what I need is just generating all the result of the form into a textarea.

Now my problem is, I've made several type of "Event" in the form that have different set of choices, and what I want is the result of that set of "Event" generated as well with the basic information.

Well, here example of my code; Begin Javascript

function generateresult() {

name = document.FORM.namefromtextarea.value;
phone = document.FORM.phonefromtextarea.value;
address = document.FORM.addressfromtextarea.value;

name2 = "Name: " + name + "\n";
phone2 = "Phone: " + phone + "\n";
address2 = "Address: " + address + "\n";

//problem type 1

lostitem = document.FORM.lostitemfromtextarea.value;
when = document.FORM.whenfromtextarea.value;
where = document.FORM.wherefromtextarea.value;

lostitem2 = "Lost Item?: " + lostitem + "\n";
when2 = "When?: " + when + "\n";
where2 = "Where?: " + where + "\n";

//problem type 2

lostperson = document.FORM.lostpersonfromtextarea.value;
personage = document.FORM.personagefromtextarea.value;
personcloth = document.FORM.personclothfromtextarea.value;

lostperson2 = "Person Name?: " + lostperson + "\n";
personage2 = "Age?: " + personage + "\n";
personcloth2 = "Wearing?: " + personcloth + "\n";

if (document.FORM.problemtype.value="Lost Item")
{
eventtype = type1;
}
else if (document.FORM.problemtype.value="Lost Person")
{
eventtype = type2;
}

type1 = lostitem2 + when2 + where2 ;

type2 = lostperson2 + personage2 + personcloth2 ;

document.FORM.generateresulttext.value = name2 + phone2 + address2 + eventtype ;}

End of javascript

And if a user clicked option that have value "Lost Person", the result for generated text will be taken from event "type2"

So I've managed to get the result for name, phone, and address, but as for lost item result, the value of the result became "undefined".

So... how exactly can I code this? I am not even sure if Im really doing the right script / method for my simple form... Thanks in advance

sissi_luaty
  • 2,839
  • 21
  • 28
SBJ
  • 5
  • 2
  • 1
    have you tried using 2 forms? I think that way you can make a single event correspond to a particular form, makes it a lot easier. – seeker Mar 05 '13 at 19:16
  • `var` is _not_ optional. You just created a bunch of problematic global variables that _anyone_ can access and modify. – elclanrs Mar 05 '13 at 19:18
  • well I planned to add more event type depending on future event, so I want to stick with 1 page of form that have multiple choices and generate result based on several event. That way my colleagues dont have to open several new pages for different kind of even and only stick with 1 page that have all the event. – SBJ Mar 05 '13 at 19:24

1 Answers1

0

It looks to me like you might be trying to create a variable that takes data from an inexistant element. You might want to put the code you have inside another function.

function generateresult() {

name = document.FORM.namefromtextarea.value;
phone = document.FORM.phonefromtextarea.value;
address = document.FORM.addressfromtextarea.value;

name2 = "Name: " + name + "\n";
phone2 = "Phone: " + phone + "\n";
address2 = "Address: " + address + "\n";

//problem type 1
function firstType(){

    lostitem = document.FORM.lostitemfromtextarea.value;
    when = document.FORM.whenfromtextarea.value;
    where = document.FORM.wherefromtextarea.value;

    lostitem2 = "Lost Item?: " + lostitem + "\n";
    when2 = "When?: " + when + "\n";
    where2 = "Where?: " + where + "\n";

    document.FORM.generateresulttext.value = name2 + phone2 + address2 + lostitem2 + when2 + where2 ;
}

//problem type 2
function secondType(){

    lostperson = document.FORM.lostpersonfromtextarea.value;
    personage = document.FORM.personagefromtextarea.value;
    personcloth = document.FORM.personclothfromtextarea.value;

    lostperson2 = "Person Name?: " + lostperson + "\n";
    personage2 = "Age?: " + personage + "\n";
    personcloth2 = "Wearing?: " + personcloth + "\n";

    document.FORM.generateresulttext.value = name2 + phone2 + address2 + lostperson2 + personage2 + personcloth2 ;
}

if (document.FORM.problemtype.value="Lost Item")
{
    firstType();
}
else if (document.FORM.problemtype.value="Lost Person")
{
    secondType();
}

}

In the future, you should NOT put numbers in a variable's name. You should also NEVER declare variables like you're doing here. When you want to create a variable, you type var variableName = variableValue. You also NEVER use words like where or when for a variable name, but instead name it to something like lostWhere or lostWhen.

Andrew
  • 546
  • 4
  • 17