1

Here is my test.php page. I need pass the data on the x variable to uid variable. When i run the code it gives me success alert also. But also giving an error saying,

Notice: Undefined variable: uid in C:\wamp64\www\FinalFinalFinalProject\test.php on line 47 Call Stack #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>

Please help me.

15
  • It gives me the success alert also Commented Mar 3, 2018 at 15:23
  • 1
    I'm pretty sure you have more code. Because the code you are showing, has no 'uid' as an index in a variable. It shows 'x' as an index... and $uid as a variable... so please show more of the php, or copy paste the 'exact' error text you are given. Commented Mar 3, 2018 at 15:24
  • i am pretty sure that .btn-info has no data attribute id Commented Mar 3, 2018 at 15:31
  • @A.Madhushani please post the HTML code of the button Commented Mar 3, 2018 at 15:33
  • 1
    In your PHP, immediately before this line ` if(isset($_POST['x'])), add this $uid = '';` - the next line is the issue, and the variable is not defined, so it's complaining. The only time you define the variable is if $_POST is set, which means that sometimes $uid is NOT defined. Commented Mar 3, 2018 at 15:59

1 Answer 1

1

As I see you need data-id value inside the input , well you can do this using JQuery only , why you are doing 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>
Sign up to request clarification or add additional context in comments.

Comments