Post data serialize

Posted on 2012-09-02
Last Modified: 2012-09-10
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');
                        { txtForumID: valForumID, txtSubject: valSubject, txtBody: valBody }, 
                            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?
Question by:tonelm54
    LVL 42

    Expert Comment

    by:Chris Stanyon
    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) {
    	var valForumID = '<?php echo $_GET["forumID"]; ?>'; //notes the double quotes!
    	var valSubject = $('#txtSubject').val();
    	var valBody = $("#txtBody").val();
    		{ txtForumID: valForumID, txtSubject: valSubject, txtBody: valBody }, 
    		function(data) { 

    Open in new window

    LVL 13

    Accepted Solution

    The $.param() function searializes an array.

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now