mmm
mmm

Reputation: 93

How to pass form value and individual value throught AJAX?

How to pass a form value and also user id into ajax?

javascript:

jQuery("#DoneBtn").click(function(){
    var data = $("#insertsubs_form").serialize();
    <?php 
        $db = JFactory::getDBO();
        $user = JFactory::getUser();
    ?>
    var userid = <?php echo $user->id; ?>
    $.ajax({
        data: {
            data:data,
            userid : userid
        },
        type: "post",
        url: "../insert_subs.php",
        success: function(data){
                alert(data);
        }
    });
});

in insert_subs.php:

$userid = $_GET['userid'];

**** APPRECIATED Anand Patel, Deena. Both are the same answers that I want! :D Also thanks to epascarello! I didnt try this but I think is working as well. :)

Upvotes: 1

Views: 655

Answers (3)

epascarello
epascarello

Reputation: 207531

Try using serializeArray:

var data = $('#insertsubs_form').serializeArray();
data.push({name: 'userid', value: userid });
$.ajax({
    data: data,
    type: "post",
    url: "../insert_subs.php",
    success: function(data){
    alert(data);
}

or you can use serializeObject

var data = $('#insertsubs_form').serializeObject();
$.extend(data, {'userid': userid });
$.ajax({
    data: data,
    type: "post",
    url: "../insert_subs.php",
    success: function(data){
    alert(data);
}

or you can use serialize and param

var data = $('#form').serialize() + $.param({"userid":userid});
$.ajax({
    data: data,
    type: "post",
    url: "../insert_subs.php",
    success: function(data){
    alert(data);
}

or the best solution just add a hidden field in the form with the user id and use serialize!

<input name="userid" type="hidden" value="<?php echo $user->id; ?>" />

Upvotes: 1

Anand Patel
Anand Patel

Reputation: 3943

try this

        jQuery("#DoneBtn").click(function(){

            <?php 
                $db = JFactory::getDBO();
                $user = JFactory::getUser();
            ?>
            var userid = <?php echo $user->id; ?>;

            var data = $("#insertsubs_form").serialize() + "&userid=" + userid;


            $.ajax({
                data: data,
                type: "post",
                url: "../insert_subs.php",
                success: function(data){
                    alert(data);
                }
            });
        })

Upvotes: 1

Deenadhayalan Manoharan
Deenadhayalan Manoharan

Reputation: 5444

Try this...

var userid = <?php echo $user->id; ?>
$.ajax({
    type : 'POST',
    url : '../insert_subs.php',
    data : $('#form').serialize() + "&userid=userid"
});

Upvotes: 1

Related Questions