-1

I am trying to send id from the current URL to the select.php page but it is displaying id not defined and is not able to fetch any data. I want to fetch for index suporder from the SQL table for to display data that are for the fetched suporder id.

     <script>  
        $(document).ready(function(){  
            function fetch_data()
            {
                $.ajax({
                    type:'post',
                    url:"select.php" + id ,
                    method:"POST",
                    dataType:"json",
                    success:function(data)

The (select.php) file to fetch the data from the AJAX query.

But this is not able to get id from the URL.

     <?php
       $connect = new PDO("mysql:host=localhost;dbname=mta_bd", "root", "");
        $query = 'SELECT * FROM all_stores where suporder='.$_GET['id'];
        $statement = $connect->prepare($query);
        if($statement->execute())
        {
         while($row = $statement->fetch(PDO::FETCH_ASSOC)){
          $data[] = $row;
         }
            echo json_encode($data);
        }
        ?>

SQL db for fetching from index as suporder for id

Fetching id from the prev URL like this

echo '<td align="right"> 
      <a type="button" class="btn btn-primary bg-gradient-primary" href="issue.php?id='.$row['suporder'] . '"><i class="fas fa-fw fa-list-alt"></i> Enter Data</a>

Error msg after the changing to console error at line url:"select.php?id=" + id, :
console error at line   url:"select.php?id=" + id,

   
            url:"select.php?id=" + id,
            method:"POST",
            dataType:"json",
        
            success:function(data)

image here:
image here

image for the displayed page:
image for the displayed page

Dharman
  • 21,838
  • 18
  • 57
  • 107
himtom
  • 1
  • 2
  • 1
    `url:"select.php" + id ` What is `id` here? Why append it to the filename? – brombeer May 09 '21 at 16:42
  • 1
    You didn't start a query string or name the URL parameter. Try `url:"select.php?id=" + id`. But it's unclear where `id` is actually defined or populated in the JavaScript - where did that variable originate? Also there isn't much point sending a POST if the only data you're sending is a single query parameter. – ADyson May 09 '21 at 16:43
  • id refers to the get id tag used in the previous url to fetch this page with linked id url(http://localhost/mta_bd/pages/issue.php?action=edit%20&%20id=244) – himtom May 09 '21 at 16:43
  • `$_GET['id']` will not be available when you send a `POST` request – brombeer May 09 '21 at 16:44
  • 1
    Also your code is vulnerable to SQL injection. Again there's really no point in using prepared statements if you don't also use parameters - it doesn't protect you. See https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php for examples of how to do it correctly. – ADyson May 09 '21 at 16:44
  • 1
    @brombeer _$_GET['id'] will not be available when you send a POST request_ ... actually it will, if it's on the query string. But the URL isn't constructed correctly, that's the issue - see earlier comments – ADyson May 09 '21 at 16:45
  • `%20` in your url is a space character, so it would be " id", not `id` I think – brombeer May 09 '21 at 16:45
  • @ADyson Yep, it will if `id` is something like `?id=12`, if it's only an `id` the url would result in `select.php12`. That's why I was asking what `id` actually is – brombeer May 09 '21 at 16:46
  • @brombeer I agree - see my first comment. But your statement about it being unavailable due to it being a POST request isn't correct, that's what I was referring to. – ADyson May 09 '21 at 16:48
  • @ADyson Agreed ;) – brombeer May 09 '21 at 16:51
  • i am still not able to fetch the results – himtom May 09 '21 at 16:51
  • You'll need to provide more relevant info then such as error messages, status codes, unexpected output, etc. What actually **does** happen when you run the AJAX code? Knowing that is a lot more useful than knowing what doesn't happen – ADyson May 09 '21 at 16:59
  • @ADyson updated, can u have a look – himtom May 09 '21 at 17:00
  • Well like the very first comment says, the `id` variable is not defined anywhere in your code. You haven't shown any code which puts a value into that variable. We already pointed that out an hour ago... did you not try to fix it yet? It's unclear to us where you expect its value to come from, so it's hard to help you. – ADyson May 09 '21 at 17:34

1 Answers1

2

You can easily retrieve the id from current url using URLSearchParams() then use it to create a data object for the ajax

const params = new URLSearchParams(location.search);
const data ={ id: params.get('id')}

$.getJSON('select.php', data, function(response){
   // do stuff with response
})
charlietfl
  • 164,229
  • 13
  • 110
  • 143