views:

101

answers:

3

Hi. Im trying to set the action of a form with javascript!

How come it wont work on this code: (what happens is that the page gets submitted to itself, as in 'action="#"'

    function validateForm() {
    var nr_of_pics=document.getElementById("annonsera_nr_pics").value;
    var name = document.getElementById("annonsera_name");
    var tel = document.getElementById("annonsera_tel");
    var email = document.getElementById("annonsera_email");
    var area = document.getElementById("annonsera_area");
    var community = document.getElementById("annonsera_area_community");
    var category = document.getElementById("annonsera_category");
    var subcats = document.getElementById("annonsera_subcats").getElementsByTagName("select");
    var headline = document.getElementById("annonsera_headline");
    var description = document.getElementById("annonsera_des");
    var price = document.getElementById("annonsera_price");
    if (nameValid(name) && telValid(tel) && emailValid(email) && areaValid(area) && communityValid(community) && categoryValid(category) && subcatsValid(subcats) && headlineValid(headline) && descriptionValid(description) && priceValid(price)){
     var form = document.getElementById("annonsera").action;
     form = "bincgi/verify_"+category+".php";
     alert (form);
     return true;
     } 
    return false;
}

and the form:

<form name="annonsera" id="annonsera" method="post" enctype="multipart/form-data" onSubmit="return validateForm();">

BY the way, the alert box wont show up either! ALSO, setting the form action manually in HTML works fine, and the form is validated properly!

A: 

Give it simple like

document.annonsera.action = "bincgi/verify_"+category+".php"

and to Submit the form

document.annonsera.submit()

RSK
does document.annonsera.submit() really have to be in there, as the code returns true anyways?
Camran
If you want to submit form. document.annonsera.submit() this.
RSK
A: 

Try this:

document.getElementById("annonsera").action = "bincgi/verify_"+category+".php";

The problem with your code is that you first read the action attribute into a variable:

var form = document.getElementById("annonsera").action;

and then you set the form variable to a new string but this won't update the value of the DOM element.

Darin Dimitrov
wont work either! same problem! putting an alert box wont even show up... hmm
Camran
+2  A: 

var form = document.getElementById("annonsera").action;
form = "bincgi/verify_"+category+".php";

These lines aren't doing what you seem the think they're doing.

The first line is creating a variable called 'form', and copying the form's current action into that variable as a string. The second line then sets the variable to a new value, but the form's action isn't being changed because the variable only contained a copy of the form's action.

This would be what you're after:

var formElement = document.getElementById("annonsera");
formElement.action = "bincgi/verify_"+category+".php";

However, I don't know why your alert box isn't showing up at all. Are you certain that all the validity methods are actually being passed?

Chris
yes, I have tested them, and they are all valid! the alert box wont show! very strange
Camran