如何将表单值和用户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'];
****致敬Anand Patel,Deena。两者都是我想要的答案! :D还要感谢epascarello!我没试过这个,但我觉得还行。 :)
答案 0 :(得分:1)
试试这个......
var userid = <?php echo $user->id; ?>
$.ajax({
type : 'POST',
url : '../insert_subs.php',
data : $('#form').serialize() + "&userid=userid"
});
答案 1 :(得分:1)
试试这个
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);
}
});
})
答案 2 :(得分:1)
尝试使用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的隐藏字段并使用serialize!
<input name="userid" type="hidden" value="<?php echo $user->id; ?>" />