【问题标题】:pass value from one page to another using ajax使用ajax将值从一个页面传递到另一个页面
【发布时间】:2015-12-22 04:42:32
【问题描述】:

我希望通过 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 代码

【问题讨论】:

  • 成功回调中的 console.log(html) 是什么?
  • @Ludo 我无法检查 console.log(html),因为我没有得到任何结果
  • 你不能这样做。通过 ajax 将值传递给其他页面。你怎么能在其他页面得到它?
  • 你应该做data : {id:id}。然后在 register.php 变量 $_POST['id'] 上获取 id
  • @roullie 感谢您的建议帮助解决了这个问题,现在我得到了结果

标签: javascript php jquery ajax


【解决方案1】:

您需要发送数据,例如 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>

希望这能解决您的问题。

【讨论】:

    【解决方案2】:

    您应该以key/value 格式传递数据。现在,您可以通过打印POST 数组来检查register.php 中的数据。

    您可以像我在示例中显示的那样以纯字符串形式传递数据,也可以传递JSON 对象。

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

    【讨论】:

      【解决方案3】:

      检查传递的值方法

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

      【讨论】:

        【解决方案4】:

        改变你$.ajax()这样的功能:

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

        希望对你有帮助……

        【讨论】:

          【解决方案5】:

          数据.php

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

          希望这能解决您的问题。

          【讨论】:

            【解决方案6】:

            试试这个;

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2020-05-19
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-06-18
              相关资源
              最近更新 更多