0

My HTML form:

<form action="lambda.php" method="post">
<label><input type="number" name="intfield" id="intfield"/></label>
<input type="submit" value="Go!"/>
</form>

Part of PHP code:

$hello = intval($_POST["intfield"]);

$client = LambdaClient::factory(array(
            'version' => "latest",
            'credentials' => array(
                'key' => 'blurred',
                'secret' => 'blurred'
            ),
            'region' => 'us-west-2'
        ));

$response = $client->invoke([
    'FunctionName' => 'helloworld2',
    'InvocationType' => 'RequestResponse',
    'Payload' => '{"key1":"$hello"}',
        ]);

echo($response['Payload']->__toString());
echo $hello;

Basically I want to type a number into the form of the HTML which is then given to the PHP code. The PHP file should send the number to a function in Lambda (Amazon Web Services).

My Lambda function and the PHP is fine. It works fine if I hardcode the number in the PHP like that:

'Payload' => '{"key1":"7"}',

But obviously I want to use it with a variable. The last echo in the PHP code shows the proper number though. Can you find a mistake in my PHP code? Thanks!

8
  • than check r u getting values or not print_r($_POST) Commented Oct 27, 2016 at 15:51
  • It says: Array ( [intfield] => 2 ) Commented Oct 27, 2016 at 15:53
  • and i think '{"key1":"$hello"}' print as a string not with value. Commented Oct 27, 2016 at 15:53
  • 1
    try like that: 'Payload' => json_encode(array('key1'=>$hello)), Commented Oct 27, 2016 at 15:54
  • 1
    Nevermind. Your answer worked. Thanks! Commented Oct 27, 2016 at 16:00

2 Answers 2

1

The variable $hello treated as a string in your code because of string quotes:

You can test like:

$hello = intval(1); // initialize a variable.

Than make an array:

$response = [
'FunctionName' => 'helloworld2',
'InvocationType' => 'RequestResponse',
'Payload' => '{"key1":"$hello"}',
];

print_r($response);

Result:

Array
(
    [FunctionName] => helloworld2
    [InvocationType] => RequestResponse
    [Payload] => {"key1":"$hello"}
)

Above mentioned result tell you each and everything whats wrong in your code now when i fixed the quotes issue and using json_encode() for your desired output:

<?php
$hello = intval(1);
$response = [
'FunctionName' => 'helloworld2',
'InvocationType' => 'RequestResponse',
'Payload' => json_encode(array('key1'=>$hello)),
];
echo "<pre>";
print_r($response);
?>

It gives me correct result:

Array
(
    [FunctionName] => helloworld2
    [InvocationType] => RequestResponse
    [Payload] => {"key1":1}
)
Sign up to request clarification or add additional context in comments.

Comments

0

Single quote strings in PHP does not expand variables. Thus you are passing the string '$hello' and not the value of $hello to the service. By changing is to "{\"key1\":\"{$hello}\"}" it will pass the value of the variable.

Always use single quotes to make constant strings. Use double quotes if you need escapes like "\n" or expansion like "test: {$test}".

Always validate your strings passed by a client so you don't make security issues.

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.