简体   繁体   English

使用ajax将值从一页传递到另一页

[英]pass value from one page to another using ajax

I wish to call data from another page to my existing page through ajax. 我希望通过ajax将数据从另一个页面调用到现有页面。 For this i have the following code 为此,我有以下代码

<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>

Till console.log (id) code is working, i am getting value inside id but i am not able to run the register.php page. 直到console.log(id)代码正常工作,我在id内获取了价值,但我无法运行register.php页面。 I wish to carry the id to register.php page, run some code there and print its result under #msg, can anyone please tell how i can correct my ajax code 我希望携带id到register.php页面,在那里运行一些代码并在#msg下打印其结果,任何人都可以告诉我如何纠正我的Ajax代码

You need to send data like data : {id: id} 您需要发送类似data的数据:{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>

Hope this will solve your issue. 希望这能解决您的问题。

You should pass data in key/value format. 您应该以key/value格式传递数据。 Now, you can check your data in register.php by printing POST array. 现在,您可以通过打印POST数组来检查register.php的数据。

You can pass data in plain string like I shown in example also you can pass JSON object. 您可以像示例中所示的那样以纯字符串形式传递数据,也可以传递JSON对象。

  • "key1=value1&key2=value2...." “键1 =值&键2 =值...。”
  • { k1:v1, k2:v2,......} {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> 

Check the values passed method 检查传递的值方法

<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>

Change you $.ajax() function like this: 像这样更改$.ajax()函数:

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

I hope it helps you... 希望对您有帮助...

Try this; 尝试这个;

     <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": "abc@gmail.com"
              },
              success: function(ccc){

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

data.php data.php

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

Hope this will solve your issue. 希望这能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM