如何通过AJAX传递表单值和个体值?

How to pass form value and individual value throught AJAX?

如何将表单值和用户 ID 传递给 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);
        }
    });
});

在insert_subs.php中:

$userid = $_GET['userid'];

**** 感谢 Deena 的 Anand Patel。两者都是我想要的相同答案! :D 还要感谢 epascarello!我没有尝试过这个,但我认为它也能正常工作。 :)

试试这个...

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

试试这个

        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);
                }
            });
        })

尝试使用 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);
}

或者你可以使用 serializeObject

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

或者你可以使用序列化和参数

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

或者最好的解决方案是在表单中添加一个带有用户 ID 的隐藏字段并使用序列化!

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