0

I'm sorry that this is basic. When I use this PHP code it works fine:

$data = '{"reportID":1092480021}';

However, when I run my URL like this:

http://localhost:8000/new/reportget.php?type=1092480021

and use this PHP code:

$reportref = $_GET['type'];
$data = '{"reportID:".$reportref."}"';

I get the error

Error_description:reportID is required

I think it's an error with how I am joining my variable to the string but I can't understand where I am going wrong.

3
  • why did you put dots around $reportref? Commented Sep 26, 2018 at 10:58
  • $data = '{"reportID:"'.$reportref.'"}"'; Commented Sep 26, 2018 at 10:59
  • @FedericoklezCulloca I thought that's how you join them together Commented Sep 26, 2018 at 10:59

4 Answers 4

3

Your string is improperly quoted. To match the format in your first example use:

$data = '{"reportID":' . $reportref.'}';

Note there are no double quotes on the last curly.

Even better:

$reportref = 1092480021;
$data = [ 'reportId' => $reportref ];
var_dump(json_encode($data));

Output:

string(23) "{"reportId":1092480021}"
Sign up to request clarification or add additional context in comments.

Comments

2

For simple view and understanding, can you try out: $data = "{\"reportID\":$reportref}";

Think that should sort it out

Comments

1

Use it like this

data = '{"reportID:"'.$reportref.'"}"';

1 Comment

This gives me {"reportID:"1092480021"}" which isn't quite right but I should be able to tweak it. Thank you
1

It isn't working because you wrap all the value within single quote and when it come to concatenate the $reprtref you put directly .$reportref without closing the first single quote and after putting the value to concatenate you forget to open another single quote

'{"reportID:".$reportref."}"';

the correct value is

'{"reportID:"' . $reportref . '"}"';

and to match the way you specify your $data value It must be like this

'{"reportID":' . $reportref . '}';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.