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.

  • It gives me the success alert also –  Mar 03 '18 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. – IncredibleHat Mar 03 '18 at 15:24
  • i am pretty sure that `.btn-info` has no data attribute `id` – messerbill Mar 03 '18 at 15:31
  • @A.Madhushani please post the HTML code of the button – messerbill Mar 03 '18 at 15:33
  • Notice: Undefined variable: uid in C:\wamp64\www\FinalFinalFinalProject\test.php on line 47 Call Stack #TimeMemoryFunctionLocation 10.0012235232{main}( )...\test.php:0 "> –  Mar 03 '18 at 15:37
  • btn-info has data-id. –  Mar 03 '18 at 15:39
  • Ok, undefined variable is different than undefined index. It pays to be precise about issues when coding. Also, since you have more code, and are not showing the code that is actually throwing your error or problem... its guesswork from our side. – IncredibleHat Mar 03 '18 at 15:42
  • I have edited the post. Now it sharing the whole code in test.php –  Mar 03 '18 at 15:55
  • 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. – random_user_name Mar 03 '18 at 15:59
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – random_user_name Mar 03 '18 at 15:59
  • thanks. now the error is not showing. But data is also not coming to the $uid variable. It only shows an empty text box. –  Mar 03 '18 at 16:01
  • `data: {"x": x}` – winlans Mar 03 '18 at 16:04
  • nothing happens. still giving only success message & empty text box.. –  Mar 03 '18 at 16:08
  • How is it supposed to work? Inside your `success` callback you will receive rendered `test.php` file that will include data passed in your AJAX request. It won't automatically change the page that you see in the browser. What are you trying to achieve? – Mateusz Woźniak Mar 03 '18 at 16:33
  • i need to pass the employee id to modal when user click a button. Also i need to show employee details on the modal based on the employee id. That what i need to achieve. It's really appreciate if someone can help me. –  Mar 03 '18 at 18:03

1 Answers1

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>
DEEPAK
  • 1,364
  • 10
  • 24