admin管理员组

文章数量:1287858

Ok so i have a form that has three input boxes, at the moment they're text, but they will be changed to password. When the submit button is pressed i want the for to send the form data to an Ajax script.

I have this working with just one parameter the button looks like this:

<input type="submit" name="submit" value="Submit" 
    onClick="return pass(this.form.oldPass.value;">

Now i've tried adding the 'this.form' part three times because thats how many parameters the function would take but it just doesn't seem to do anything:

<input type="submit" name="submit" value="Submit" 
    onClick="return pass(this.form.oldPass.value,
                         this.form.newPass.value, 
                         this.form.newPassCheck);">

Is this the right way to go about passing form data to a script? is there another way around it or am i just doing something wrong here?

thanks for the help, if you need anymore code i'll add it.

Ok so i have a form that has three input boxes, at the moment they're text, but they will be changed to password. When the submit button is pressed i want the for to send the form data to an Ajax script.

I have this working with just one parameter the button looks like this:

<input type="submit" name="submit" value="Submit" 
    onClick="return pass(this.form.oldPass.value;">

Now i've tried adding the 'this.form' part three times because thats how many parameters the function would take but it just doesn't seem to do anything:

<input type="submit" name="submit" value="Submit" 
    onClick="return pass(this.form.oldPass.value,
                         this.form.newPass.value, 
                         this.form.newPassCheck);">

Is this the right way to go about passing form data to a script? is there another way around it or am i just doing something wrong here?

thanks for the help, if you need anymore code i'll add it.

Share Improve this question asked Jan 19, 2012 at 10:10 ragebunnyragebunny 1,76010 gold badges37 silver badges55 bronze badges 2
  • 2 Please show function pass and do not call code onclick of a submit button. Instead call it onsubmit of the form - and preferably added onload via a script in the head. If you do NOT need to submit the form, then you need to return false in the onsubmit event – mplungjan Commented Jan 19, 2012 at 10:19
  • 1 With jQuery, you just need to do a $.post("test.php", $("#testform").serialize()); – mplungjan Commented Jan 19, 2012 at 10:22
Add a ment  | 

4 Answers 4

Reset to default 3

try this one. (you need jquery to do this.)

<form>
 old pass <input type="password" id="old_pass" />
 new pass <input type="password" id="new_pass" />
 new pass confirmation <input type="password" id="new_pass_confirm"/>
</form>

in your .js file , write this:

$(document).ready(function(){
    var old_pass = $('#old_pass').val();
    var new_pass = $('#new_pass').val();
    var new_pass_confirm = $('#new_pass_confirm').val();

    //then call your method with those vars as parameter
    anyMethod(old_pass, new_pass, new_pass_confirm);

});

//this is your function
function anyMethod(old_pass, new_pass, new_pass_confirm)
{
  //put your validation code or ajax call here...
}

Add id for each of your input boxes and you can get value in function, like:


function pass() {
 var old = document.getElementById("yourOldPassId").value;
 var new = document.getElementById("yourNewPassId").value;
 var newConfirm  = document.getElementById("yourNewConfirmPassId").value;
}

Did you mean something like this

Give the inputs some ids and get the data from there.

Something like:

<input type bla bla id="whateverId0" />

For each input.

The submit should have something like onclick="whateverFunction()"

The js should look like:

     function whateverFunction()
     {
          val0 = document.getElementById('whateverId0');
          // For all 3 values.

          // Do whatever you want with them.
     }

I believe that you should add an onSubmit listener to your form. There are good js frameworks out there like jquery which are easy to use. That way you can make a listener which runs a function when you submit your form. In that function you can extract the data to json for example and ajax it to your server.

info about jquery

jquery ajax

how to extract form data

本文标签: Sending multiple parameters to javascript from a HTML form buttonStack Overflow