-1

I want to pass id with ajax to a php file. I tried a lot but couldnot succed. I want to return to the page with id. Please help me.

I tried like this.

 <script>

      jQuery.ajax({
                var registration_date=$("#registration_date").val()                    
               var building_length_ft=$("#building_length_ft").val();
                var building_length_in=$("#building_length_in").val();
                var building_breadth_ft=$("#building_breadth_ft").val();
                 var reg_id=$_GET['id'].val();
                 var dataString = $('#a').serialize();

                $.ajax({
                    url:'registration_detail.php',
                    method:'POST',
                   data: dataString,
                   success:function(data){
                       alert(data);
                   }
                });
            });

    </script>

I tried below code as well.

    var reg_id=$_GET['id'].val();

                $.ajax({
                    url:'registration_detail.php',
                    method:'POST',
                    data:{
                   'reg_id=' + reg_id,
                    },
                   success:function(data){
                       alert(data);
                   }
                });
I tried below code as well.
var id = $('#id').val();
                $.ajax({
                    url:'registration_detail.php',
                    method:'POST',
                    data: { id: id},
                   success: function(response) {
            $('#result').html(response);
        }
                });
Prabina
  • 83
  • 1
  • 1
  • 7
  • You can't mix php and javascript that way. Php runs only on server...not in browser. Read up on how to access url query params using javascript – charlietfl Jul 10 '19 at 23:19
  • I tried other methods as well but couldnot solve. Please help me.@charlietfl – Prabina Jul 11 '19 at 00:42
  • https://developer.mozilla.org/en-US/docs/Web/API/URL/searchParams – charlietfl Jul 11 '19 at 00:45
  • I'm able to get id but couldn't pass to php file. I want to pass id through url in ajax call and return to the php file with id . eg:http://localhost/bps/registration_detail.php?id=62. I tried a lot.@charlietfl – Prabina Jul 11 '19 at 01:06
  • JUst concatenate it to the string url `url:'registration_detail.php?id=' +id,` – charlietfl Jul 11 '19 at 01:10
  • It doesnot work.@charlietfl – Prabina Jul 11 '19 at 03:28
  • Possible duplicate of [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – misorude Jul 11 '19 at 09:02

1 Answers1

0

You should fix your code:

<script>

      jQuery.ajax({
                var registration_date=$("#registration_date").val()                    
               var building_length_ft=$("#building_length_ft").val();
                var building_length_in=$("#building_length_in").val();
                var building_breadth_ft=$("#building_breadth_ft").val();
                // There is a PHP script here:
                 var reg_id=<?= $_GET['id'] ?>;
                 var dataString = $('#a').serialize();

                $.ajax({
                    url:'registration_detail.php',
                    method:'POST',
                   data: dataString,
                   success:function(data){
                       alert(data);
                   }
                });
            });

    </script>
Julyano Felipe
  • 288
  • 2
  • 22