idish
idish

Reputation: 3270

the function is not working

Alright, so I'm making a form validation everything is good in this JS, but now I'm facing a problem in the output, I am trying to display all the chosen data. So I used the action attribute and called the following function:

function funcs()
    {
    var favor = document.reg.favor[selectedIndex].value; //Select input
    var fname = document.reg.fname.value; // text input
    var lname = document.reg.lname.value; // text input
    var email = document.reg.email.value; // text input
    var pass = document.password.value; //text input
    for(i=0;i<document.reg.rad.length;i++)
        {
            if(document.reg.rad[i].checked == true)
            {
                var rad = document.reg.rad[i].value; // Radio input
            }
        }
    if(document.reg.bike.checked == true)
        {
            var bike =  document.reg.bike.value; //CheckBox input
        }
    if(document.reg.car.checked == true)
        {
            var car = document.reg.car.value; //CheckBox input
        }
    document.write('<head><link type="text/css" rel="stylesheet" href="registrationtable.css"/></head><body>');
    document.write("<div class = 'team'>");
    document.write('<table>');
    document.write("<tr><td> שם  פרטי: </td><td>" + fname + "</td></tr> <tr><td> שם משפחה: " + lname + "</td></tr> <tr><td> אימייל: " + email + "</td></tr> <tr><td> סיסמא: " +pass +"</td></tr>");
    document.write("<tr><td> השחקן האהוב עליך הוא " + favor +"</td></tr>"); 
    document.write("</table>");
    document.write("</div></body>");
    }

Here's the form header:

<form name ="reg" action ="Javascript:funcs()" onsubmit ="return checkValidation()">

I'd like to clear that all the other javascript code is working perfectly, it must be something with this function. When I'm pressing the send button, it won't do anything. Anyone knows whats the problem? Thanks in advanced.

Upvotes: 0

Views: 121

Answers (3)

aquinas
aquinas

Reputation: 23796

As I suspected. One problem is this line:

var favor = document.reg.favor[selectedIndex].value;

It should be

var favor = document.reg.favor[document.reg.favor.selectedIndex].value;

And your second problem is this:

var pass = document.password.value;

Should be:

var pass = document.reg.password.value;

See updated fiddle: http://jsfiddle.net/x7SBy/1/

Finally, you should use Firefox and download Firebug. It is invaluable for debugging JS problems like this.

Edit: There are other problems with your JS that I won't get into in detail, but in general you don't want to use document.reg.password, because of issues like this. You should really use document.getElementById. FYI.

Upvotes: 2

aziz punjani
aziz punjani

Reputation: 25796

You can't shouldn't have a javascript function in your action attribute, it needs to be a URI. You can just call the funcs onsubmit if validation succeeded.

As Aquinas has shown that calling a javascript function in the action attribute is in fact possible, it is advised that you not put js code in the action attribute.

Upvotes: 4

shanabus
shanabus

Reputation: 13115

It looks like you are trying to validate a form, then if valid call the funcs function to alter HTML on the page.

Maybe something like this:

<form name="reg" action="" onsubmit="checkValidation()">

Then a checkValidation function to pause form submission and if valid, call the funcs function:

function checkValidation(e) {
    e.preventDefault();

    if (checkValidation()) {
        funcs();
    }
}

But if this is the case, your funcs function should not be writing <head> tags and such. Maybe you could just add HTML to the body instead of trying to lay a new HTML document into the DOM with javascript.

Alternate solution:

function checkValidation() {
   ... do your validation

   return true; // or false if invalid
}

Then use a real HTML page/resource in your action tag of the form.

Upvotes: 0

Related Questions