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
2010-10-03 12:17:34
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
2010-10-03 12:21:48
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
2010-10-03 12:28:15
If you want specific solution then please post you html form and values to be replaced.
Alpesh
2010-10-03 12:29:07
i figure out! i can add some hidden input to form without affecting UI,Thanks any way!
mehran
2010-10-03 12:29:30
A:
I used fallowing code
$("form").submit(function()
{
$("form").append($("<input type='hidden'/>").attr("Id","someId").val("SomeValue");
});
but it doesn't work!
mehran
2010-10-03 12:47:11
I found problem, its was using id, i change .attr("Id","someId") to .attr("Name","someId") and it works.
mehran
2010-10-03 12:56:00
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
2010-10-03 15:34:13