admin管理员组

文章数量:1332394

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

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

Share Improve this question asked Dec 22, 2015 at 4:42 user5404107user5404107 7
  • what is console.log(html) in your success callback ? – Ludovic C Commented Dec 22, 2015 at 4:43
  • @Ludo i am not able to check console.log(html) as i am not getting any result – user5404107 Commented Dec 22, 2015 at 4:45
  • You can't do this like this. Passing value to Other page by ajax. how can you get it in other page? – Parth Trivedi Commented Dec 22, 2015 at 4:45
  • 5 you should do data : {id:id}. then get id on register.php the variable $_POST['id'] – roullie Commented Dec 22, 2015 at 4:45
  • 1 @roullie thanks your advice has helped in solving the issue, now i am getting the result – user5404107 Commented Dec 22, 2015 at 4:49
 |  Show 2 more ments

6 Answers 6

Reset to default 2

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

data.php

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

Hope this will solve your issue.

Try this;

     <script src="//ajax.googleapis./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);
              }
          });

本文标签: javascriptpass value from one page to another using ajaxStack Overflow