简体   繁体   中英

pass value from one page to another using ajax

I wish to call data from another page to my existing page through 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. 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

You need to send data like 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. Now, you can check your data in register.php by printing POST array.

You can pass data in plain string like I shown in example also you can pass JSON object.

  • "key1=value1&key2=value2...."
  • { 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({
    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

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

Hope this will solve your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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