0

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!

3
  • Try to See what is wrong in Error Console in Mozilla Commented Nov 16, 2009 at 11:53
  • Not sure if it would be a solution for you, but couldn't you just have the form point to the same file, and handle the different categories with a switch($_POST['category']? Commented Nov 16, 2009 at 12:06
  • If the alert won't show, then use that to narrow down your problem. Put an alert at the start of validateFrom function. Does that show? Remove the "if" clause. Does your existing alert show? Remove the lines one by one until either the existing alert shows or the two alerts are adjacent to one another. When you remove the line that causes your existing alert to show, you know what line is causing your problem. Commented Nov 16, 2009 at 12:35

3 Answers 3

2

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?

Sign up to request clarification or add additional context in comments.

1 Comment

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

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.

1 Comment

wont work either! same problem! putting an alert box wont even show up... hmm
0

Give it simple like

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

and to Submit the form

document.annonsera.submit()

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.