Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post serialized form data and extra variables using JQuery

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?

like image 400
Schodemeiss Avatar asked Jan 26 '10 12:01

Schodemeiss


4 Answers

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

    $.post("pagedetail.php", serialized,
    function(data) {
        alert(data); 
});
like image 97
Kemo Avatar answered Oct 29 '22 22:10

Kemo


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

like image 20
demmy Avatar answered Oct 30 '22 00:10

demmy


Try this for the second parameter to $.post:

 { form: $("#PageDetailForm").serialize(), thePage: thePage }
like image 1
karim79 Avatar answered Oct 30 '22 00:10

karim79


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);
like image 1
kubanek Avatar answered Oct 29 '22 22:10

kubanek