使用Ajax将值从javascript传递到php变量

时间:2018-03-03 15:20:32

标签: javascript php ajax

这是我的test.php页面。我需要将x变量上的数据传递给uid变量。 当我运行代码时,它也给了我成功警报。但也提出错误说,
注意:未定义的变量:第47行的C:\ wamp64 \ www \ FinalFinalFinalProject \ test.php中的uid调用堆栈#TimeMemoryFunctionLocation 10.0012235232 {main}()... \ test.php:0">

  <?php 
        $user = "anuradha";
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
        <link rel="stylesheet" type="text/css" href="StyleSheets/leave.css">
        <script type="text/javascript">
            $(document).ready(function(){
               $(".btn-info").click(function(){ // Click to only happen on announce links
                 var x = $(this).data('id');
                 $.ajax({
                type: "POST",
                url: 'test.php',
                data:  { x : x },
                success: function(data)
                {
                    alert("success!");
                }
            });

               });

            });
        </script>
    </head>
    <body>
        <button type="button" class="btn btn-info btn-md" data-toggle="modal" data-target="#myModal1" data-id="abc">Detail View</button>

        <!-- Modal -->
          <div class="modal fade" id="myModal1" role="dialog">
            <div class="modal-dialog">

              <!-- Modal content-->
              <div class="modal-content">
                <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal">&times;</button>
                  <h4 class="modal-title">Leave Application</h4>
                </div>
                <form method="post" action="">
                <div class="modal-body">
                    <?php  
                        if(isset($_POST['x']))
                        {
                            $uid = $_POST['x'];
                        }
                    ?>
                  <input type="text" class="form-control" id="username" value="<?php echo $uid; ?>">
                </div>
                <div class="modal-footer">
                  <button type="submit" class="btn btn-default">Update</button>
                </div>
                </form>
              </div>

            </div>
            </div>
            <!-- Modal -->
    </body>
    </html>

请帮帮我。

1 个答案:

答案 0 :(得分:2)

我看到您需要在输入中添加data-id值,那么您只能使用JQuery来完成此操作,为什么要这么做ajax

  <script type="text/javascript">
            $(document).ready(function(){
               $(".btn-info").click(function(){ // Click to only happen on announce links
                 var x = $(this).data('id');
                 $("#username").val(x);


               });

            });
        </script>