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

Post data serialize

Im trying to post data to a webpage, but belive Im having an issue with the values.

Im trying to submit a form like:-
                <script type="text/javascript">
                    $('#cmdSaveNewSubject').click(function() {
                        var valForumID = '<?php echo $_GET['forumID']; ?>';
                        var valSubject = $('#txtSubject').val();
                        var valBody = $("#txtBody").htmlarea('toHtmlString');
                        
                        $.post("_ShowForum.Subject.Save.php", 
                        { txtForumID: valForumID, txtSubject: valSubject, txtBody: valBody }, 
                        function(data) 
                            { 
                            $('#contentBody').html(data);
                            ajaxDo('_ShowForum.Subject.php?forumID=<?php echo $_GET['forumID']; ?>','#contentBody');
                            }
                        );
                    });

Open in new window



During tests I can see the values before the function posts:-
var valForumID = 56443
var valSubject = test
var valBody =bbb

I belive my issues are with the values, being passed, so I tried to serialize the data:-
{ txtForumID: valForumID, txtSubject: valSubject, txtBody: valBody }.serialize()

Open in new window


But my debugger says:-
Object doesn't support property or method 'serialize'

I cannot find any documentation on how to serialize data outside a form.

Anyone any suggestions?
0
tonelm54
Asked:
tonelm54
1 Solution
 
Chris StanyonCommented:
A few things of note. I'd wrap the function in  $(document).ready() just to be safe.

I've not seen the htmlarea() function before. This doesn't seem to be standard jQuery. If it's a form element, you can grab the contents with the val() function:

var valBody = $("#txtBody").val();

Can't tell from your code what the ajaxDo function is for, but it looks like it might make another ajax call and update the content of #contentBody, which you are doing in the previous line. May be what you're after, but it looks a little odd.

Here's a copy of your code, tested and working fine (without the htmlarea() and ajaxDo() functions)

$(document).ready(function() {
$('#cmdSaveNewSubject').click(function(e) {
	e.preventDefault();
	var valForumID = '<?php echo $_GET["forumID"]; ?>'; //notes the double quotes!
	var valSubject = $('#txtSubject').val();
	var valBody = $("#txtBody").val();

	$.post(
		"ajaxpost.php", 
		{ txtForumID: valForumID, txtSubject: valSubject, txtBody: valBody }, 
		function(data) { 
			$('#contentBody').html(data);
		}
	);
});
});

Open in new window

0
 
JonNormanCommented:
The $.param() function searializes an array. http://api.jquery.com/jQuery.param/

I suspect that this is not the problem, and that the problem would be fixed by just changing your data object to:
{ "txtForumID": valForumID, "txtSubject": valSubject, "txtBody": valBody } on your line 8, I have seen jQuery be funny about this in the past.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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