9

I am trying to use this piece of code to serialize a form AND send an extra variable not found in the form, at the same time. The following line of code is what I expected, but sadly does not work.

var thePage = theFilename();
$.post("pagedetail.php", { $("#PageDetailForm").serialize(), thePage: thePage },
    function(data) {
        alert(data); 
});

Any ideas?

5 Answers 5

6
    var serialized = $('#PageDetailForm').serialize();
    serialized.thePage = thePage;

    $.post("pagedetail.php", serialized,
    function(data) {
        alert(data); 
});
Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't seem to add thePage onto the end of the form values. My Print_R in PHP only shows that the form fields have been submitted :(. Close though... Any other thoughts???
Oh... I found this to work: var serialized = $('#PageDetailForm').serialize() + "&thePage=" + thePage; Thanks for your help though!!!
then try adding an <input type="hidden" value="thePageValue" />, it'll be serialized with the rest. @Andy Barlow
2

what you can do is to add the extra data to an hidden input and catch it in the pagedetail.php page .

eg lats say your form

   <form id='PageDetailForm'>
   <input type="hidden" name="value"  id="value" value="the value u wamnt to add goes here" />
             ....other inputs
</form>

after this just do your normal $.post

$.post("#pagedetail.php",$("#PageDetailForm").serialize(),function(data){

    $("#ans").html(data);

// in the pagedetail.php

$echo $_POST['value'];

hope dis help if ur still confused hola me @dplumptre

Comments

1

Try this for the second parameter to $.post:

 { form: $("#PageDetailForm").serialize(), thePage: thePage }

1 Comment

Sadly, this splits up the data into two arrays. One with Form, with one sting in it, and another with thePage. Nearly there though :)
1

Hopefully you still need this :). Try the serializeArray() method and then push some additional data in the resulting array, so you don't have splitted arrays etc.:

var postData = $('#form-id').serializeArray();
var additionalData = $('#additionalDataID').val();
postData.push({name: 'additionalName', value: additionalData});

and finally:

$.post(URL, postData);

Comments

0

Try sortable('toArray'):

var thePage = theFilename();

$.post("pagedetail.php", { pageDetailForm: $("#PageDetailForm").sortable('toArray'), thePage: thePage },
    function(data) {
        alert(data); 
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.