0

I am sending 2 variables from HTML form to php json decoding from api url but I got empty values.

<form action="2.php" method="post">
Word: <input type="text" name="q">
<input type="hidden" name="langpair" value="en|it">
<input type="submit">
</form>

to php file

$json = file_get_contents('https://api.mymemory.translated.net/get?q=<? echo $_POST["q"]; ?>;&langpair=<? echo $_POST["langpair"]; ?>');
$obj = json_decode($json);

echo $obj->responseData->translatedText;

I am getting empty page!

2 Answers 2

2

For security reasons, you should not pass the variables directly to the url, but if you want to do it should be like this:

$json = file_get_contents('https://api.mymemory.translated.net/get?q='.urlencode($_POST["q"]).'&langpair='.urlencode($_POST["langpair"]));
Sign up to request clarification or add additional context in comments.

Comments

1

It is because of your URL! Change it for this :

file_get_contents('https://api.mymemory.translated.net/get?q=' . $_POST["q"]. '&langpair=' . $_POST["langpair"]);

1 Comment

Unfortunately this syntax did not work, please see the other answer of @EmilianoT thank you anyway.

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.