views:

257

answers:

3
+1  Q: 

changing form post

I want to change form post data before send. i use some java script code to get some user inputs and want to attach those to request post data. I wanna a normal post that cause to display result as a an HTML page. its may be useful that i linked JQuery and can use its functions. Thanks very much!

+1  A: 

You can use onSubmit event as shown below -

<form action="test.php" name="testform" onSubmit="return TestDataCheck()" >
<input type="text" value="hello" name="testinput" />
<form>

Define a function as shown below -

<script type="text/javascript">
function TestDataCheck(){

//Here do whatever you want to do with the form or it's values.
//I am changing the value of input field in the form for your reference in same way you
//can change any elements value.

$('input[name="testinput"]').val("bye");

return true.

}
</script>
Alpesh
The question is that, how can change the form data, i should replace the comment with what? The question is about the code that i can write in TestDataCheck!
mehran
For your reference i have changed the value of the input field in the form in my example. In same way you can change any field's value.
Alpesh
If you want specific solution then please post you html form and values to be replaced.
Alpesh
i figure out! i can add some hidden input to form without affecting UI,Thanks any way!
mehran
A: 

I used fallowing code

$("form").submit(function()
{
   $("form").append($("<input type='hidden'/>").attr("Id","someId").val("SomeValue");
});

but it doesn't work!

mehran
I found problem, its was using id, i change .attr("Id","someId") to .attr("Name","someId") and it works.
mehran
A: 

html

<form action="test.php" name="testform" id="testForm">
<input type="text" value="hello" name="testinput" />
<form>

javascript

//handle form submit
   jQuery("#testForm").submit(function(e){

      //prevent default action
      e.preventDefault; 

     //get form
     var _data=jQuery("#testForm").serialize();

     //add custom props
     _data['customProp1']='customValue1';
     _data['customProp2']='customValue2';

     //now just submit form or call ajax

    });
Praveen Prasad