2015-12-22 98 views
1

我希望通過ajax從其他頁面調用數據到我現有的頁面。爲此,我有以下代碼使用ajax將值從一個頁面傳遞到另一個頁面

<script> 
$(document).ready(function() 
    { 
     $(".link").on("click", function(e) 
      { 
       e.preventDefault(); 
       var id = $(this).data("id"); 
       console.log (id); // i am getting value in this id 
       $.ajax({ 
       type : "post", 
       url : "register.php", 
       data : id, 
       cache : false, 
       success : function(html) 
       { 
        $('#msg').html(html); 
       }}); 
    }); 
}); 
</script> 

<a class='link' data-id="$idd" >TEXT</a> 

直到執行console.log(ID)代碼工作,我得到的內部ID值,但我不能夠運行register.php頁面。我希望攜帶id到register.php頁面,在那裏運行一些代碼並在#msg下打印它的結果,誰能告訴我如何糾正我的ajax代碼

+0

什麼是您的成功回調中的console.log(html)? – Ludo

+0

@盧多我無法檢查console.log(html),因爲我沒有得到任何結果 – pp1989

+0

你不能這樣做。通過ajax將值傳遞給其他頁面。你怎麼能在其他頁面獲得它? –

回答

0

您應該以key/value格式傳遞數據。現在,您可以通過打印POST數組來檢查register.php中的數據。

您可以傳遞純數據串中的數據,就像我在示例中所示的那樣,您也可以通過JSON對象。

  • 「鍵1 =值&鍵2 =值......」
  • {K1:V1,K2:V2,......}

    <script> 
    $(document).ready(function() { 
        $(".link").on("click", function(e) { 
         e.preventDefault(); 
         var id = $(this).data("id"); 
         console.log (id); // i am getting value in this id 
         $.ajax({ 
          type : "post", 
          url : "register.php", 
          data : "id="+id, //you can pass value like this. 
          cache : false, 
          success : function(html) { 
           $('#msg').html(html); 
          } 
         }); 
        }); 
    }); 
    </script> 
    
2

你需要發送數據,如數據:{id:id}

<script> 
$(document).ready(function() 
    { 
     $(".link").on("click", function(e) 
      { 
       e.preventDefault(); 
       var id = $(this).data("id"); 
       console.log (id); // i am getting value in this id 
       $.ajax({ 
       type : "post", 
       url : "register.php", 
       data : {id: id}, 
       cache : false, 
       success : function(html) 
       { alert(html); 
        $('#msg').html(html); 
       }}); 
    }); 
}); 
</script> 

希望這將解決您的問題。

0

檢查的值傳遞方法

<script> 
$(document).ready(function() 
    { 
     $(".link").on("click", function(e) 
      { 
       e.preventDefault(); 
       var id = $(this).data("id"); 
       //console.log (id); 
       $.ajax({ 
       type : "post", 
       url : "register.php", 
       //data : id, 
       data:{'id':id},//values to be passed similarly 
       cache : false, 
       success : function(html) 
       { 
        $('#msg').html(html); 
       }}); 
    }); 
}); 
</script> 
0

更改您$.ajax()功能是這樣的:

$.ajax({ 
    type: 'POST', 
    url: 'register.php', 
    data: { 
     id: id 
    }, 
    success: function(response) 
    { 
     $('#msg').html(response); 
    } 
}); 

我希望它可以幫助你......

0

試試這個;

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 

    $.ajax({ 
       type:'GET', 
       url:'data.php', 
       data: { 
        "id": 123, 
        "name": "abc", 
        "email": "[email protected]" 
       }, 
       success: function(ccc){ 

        alert(ccc); 
        $("#result").html(ccc); 
       } 
      }); 
0

data.php

echo $id = $_GET['id']; 
echo $name = $_GET['name']; 
echo $email = $_GET['email']; 

希望這將解決您的問題。

相關問題