• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 788
  • Last Modified:

jQuery: Post serialized form data with two extra fields

Using jQuery, I want to post all the data contained in #testform as well as two additional values.

This does NOT work:
$.post("test.php", xyz: "1", qrs: "2",  $("#testform").serialize());

Open in new window

0
hankknight
Asked:
hankknight
  • 5
1 Solution
 
Greg AlexanderLead DeveloperCommented:
Whats an example value of #testform
0
 
leakim971PluritechnicianCommented:
For example :
$.post("test.php", $("#testform").serialize() + "xyz=1&qrs=2" );

Open in new window

0
 
leakim971PluritechnicianCommented:
I miss an "&" :
$.post("test.php", $("#testform").serialize() + "&xyz=1&qrs=2" );

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
leakim971PluritechnicianCommented:
Test page :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
<script language="javascript">
	$(document).ready(function() {
		alert( $("#testform").serialize() + "&outside1=" + $("[name='outside1']").val() + "&outside2=" + $("[name='outside2']").val()  );
	});
</script>
</head>
<body>
<form id="testform">
<input name="inside" value="in"/>
</form>
<input name="outside1" value="out1" />
<input name="outside2" value="out2" />
</body>
</html>

Open in new window

0
 
leakim971PluritechnicianCommented:
You may need to encode the values :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
<script language="javascript">
	$(document).ready(function() {
		alert( $("#testform").serialize() + "&outside1=" + encodeURIComponent($("[name='outside1']").val()) + "&outside2=" + encodeURIComponent($("[name='outside2']").val()) );
	});
</script>
</head>
<body>
<form id="testform">
<input name="inside" value="in"/>
</form>
<input name="outside1" value="out1" />
<input name="outside2" value="out2" />
</body>
</html>

Open in new window

0
 
leakim971PluritechnicianCommented:
Thanks for the points!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now