2015-01-09 65 views
1

如何將表單值和用戶ID傳遞給ajax?如何通過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']; 

****衷心感激阿南德帕特爾,蒂娜。兩者都是我想要的相同答案! :D同樣感謝epascarello!我沒有嘗試這個,但我認爲也是如此。 :)

回答

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

試試這個...

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

,或者您可以使用序列化和param

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; ?>" />