Jay
Jay

Reputation: 23

Insert Variable into file_get_contents from HTML form

I've used the answer given here (how do i insert a variable? from a form into a file_get_contents link) however, It is still not working for me.

But this is not working, I do not know why?

Upvotes: 1

Views: 185

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

You need to place/assign your POST array and variable first, not after:
(Kind of like putting the horse after the wagon, as it were).

<?php
$code = $_POST['search'];
$json = file_get_contents("http://myurl.com/user=". $code);
$obj = json_decode($json);
?>

Because as it stands, you'd be getting an "Undefined index/variable..." notice, had you been checking for errors.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

Upvotes: 1

Related Questions