-1

What should happen:

  • When button clicked, it calls report.php and sends "main" as type
  • Type "main" is picked up in report.php and used as $typename
  • $data varaible is populated with the contents of main.json

What does happen:

>     [25-Sep-2018 13:56:56] WARNING: [pool www] child 11 said into stderr: "NOTICE: PHP message: PHP Notice:  Undefined index: type in
> /var/www/html/report.php on line 27"
>     [25-Sep-2018 13:56:56] WARNING: [pool www] child 11 said into stderr: "NOTICE: PHP message: PHP Warning:  file_get_contents(.json):
> failed to open stream: No such file or directory in
> /var/www/html/report.php on line 28"
>     2018/09/25 13:57:00 [error] 8#8: *5 FastCGI sent in stderr: "PHP message: PHP Notice:  Undefined index: type in
> /var/www/html/report.php on line 27

index.php

<script>
$(document).ready(function(){
    $("button").click(function(){
        $.ajax({
            url: 'report.php',
            type: "POST",
            dataType:'json',
            data: ({type: main}),
            success: function(result){
                $("#output").html(result);
            }
        }); 
    });
});
</script>

report.php

$typename = $_POST['type'];
echo $typename;
$data = file_get_contents("$typename.json");

main.json

{
    "reportDescription":{ <....>
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Jimmy
  • 12,087
  • 28
  • 102
  • 192
  • 1
    Where is the javascript variable `main` defined? Because I don't see it anywhere, meaning you haven't showed us all the code, or I found your problem. – GrumpyCrouton Sep 25 '18 at 14:04
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – GrumpyCrouton Sep 25 '18 at 14:05
  • Second @GrumpyCrouton, also don't see you feeding the resulting JSON back to the client anywhere in your PHP code. – Jared Smith Sep 25 '18 at 14:05
  • @GrumpyCrouton I just want it to have the value main (not a variable etc). I'm a bit new, are you saying it should be ```data: ({type: 'main'}),``` – Jimmy Sep 25 '18 at 14:06
  • The parens are unnecessary, and yes. – Jared Smith Sep 25 '18 at 14:07
  • @Jimmy So it should be the string `"main"`? So yes, it should be `data: ({type: 'main'}),` or `data: ({type: "main"}),` – GrumpyCrouton Sep 25 '18 at 14:08

2 Answers2

4

The problem is that you are trying to send an undefined variable main to your php file. This variable doesn't exist, so you get an error. Strings must be wrapped with single or double quotes.

You should change data: ({type: main}), to

data: {type: 'main'},

(The perenthesis are not needed)

To send the string to your PHP file.

Now in your PHP file, you need to output $data.

$typename = $_POST['type'];
echo $typename;
$data = file_get_contents("$typename.json");
echo $data;
GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
0

You should change data: {type: 'main'}, as answered before.

But the php code in that answer could be improved. This code could lead to security issues, particularly to a Server Side Request Forgery. You need to set a clear boundary between the user supplied data and your application code, you need to validate the code to avoid some evil uses of file_get_contents:

  • One can request a random website by http or use another protocol like ssh to connect to other server reachable from your web server

  • Someone can scan your directories and read operating system files

So, beware what you do with user input, you need to validate it. I would use the following code.

  // strip all slashes and directories, use only the filename
  $typename = basename($_POST['type']);
  $filename = __DIR__.'/'.basename($_POST['type']).".json";

  if (!file_exist($filename)){
     echo "ERROR";
  }
  // do not output anything till validated
  // echo $typename;
  // use a header to tell the user that this is a json file
  header('Content-Type: application/json');
  $data = file_get_contents($filename);
  echo $data; 
David Lemon
  • 1,560
  • 10
  • 21