0

I am working on a code where I am trying get the data from a mysql table using a filename through ajax GET request. The table "SAMPLE" contains img_name and x_value. I tried the following code:

index.php:

 var full = document.getElementById("img01").src;
 var file = full.replace(/^.*[\\\/]/, '');

 $.ajax({
   type:'GET',
   url: 'getfile.php',
   data:{
     'file':file,
     'xvalue':xvalue,
   },

   success: function(data){
     console.log(x);
   }
 })

getfile.php:

<?php

  $db = mysqli_connect('localhost', 'root', '', 'project_focus');

  $imagefile=$_GET['file'];
  $xvalue=$_GET['xvalue'];
  $updatename = "SELECT x_value FROM sample WHERE image_name='$imagefile'";
   mysqli_query($db,$updatename);
 ?>

In the above code, I tried to get the x_value from the table by using the filename in the variable "file" in index.php. But when I tried to print in the console it produces an error 'Uncaught ReferenceError: xvalue is not defined'. The expected result I would like to get x_value from the table using the filename that is in the variable "var file" .

I think I made some mistake with the Get request. Can someone help me to solve this problem

Joskaa
  • 151
  • 1
  • 2
  • 9
  • From where are you getting `x` value here i.e :`'xvalue':x` ? – Swati Nov 16 '20 at 04:13
  • @Swati I wanted to get the x value from the table. – Joskaa Nov 16 '20 at 04:16
  • I see so what is `x` here `'xvalue':x,` which you are passing as value for `xvalue` it must be null remove that and see as your are getting that value from backend .Also ,can you show full error which you are getting ? – Swati Nov 16 '20 at 04:19
  • @Swati I have edited the code. – Joskaa Nov 16 '20 at 04:22
  • @Swati What Im trying to do -> using the filename that is in variable "file" I tried to retrieve the x_value present in the table for that filename – Joskaa Nov 16 '20 at 04:24
  • That error simply mean `xvalue` variable is not declare anywhere in your code and doesn't have any value simply remove that .Also , use preparestatement to fetch result from database .Here is an [example](https://stackoverflow.com/a/767520/10606400).After fetching you just need to do `echo $row["yourxvaluecolumname"]` at your php page.So, whatever will be there inside `echo` will come as response inside `success` function of ajax. So ,inside success function do `alert(data)`to see if value return or not.Let me know if that works or not. – Swati Nov 16 '20 at 04:48
  • @Swati I did not understand your answer, Is it possible you can give it as an answer ?. – Joskaa Nov 16 '20 at 05:02
  • @Swati I will explain my problem again , I have a variable "file" which stores the name of an image file. With the help the of the image filename I am trying to get the value of X that is present in the table for that image file. But i dont know if i used the ajax properly. – Joskaa Nov 16 '20 at 05:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224613/discussion-between-swati-and-joskaa). – Swati Nov 16 '20 at 05:06

0 Answers0