0

This seems like a very trivial question, but I'm new to php, so please bear with me.

This is the test php script I wrote:

<?php
$str = $_POST['str'];
echo('string entered: '.$str);

I post a request by typing this into the url:

myurl.com/test.php?str=Hello

Now, the response I'm getting is:

string entered:

What am I doing wrong here?
Thanks!

3 Answers 3

1

Parameters in URL are GET, so you need to access them like

$param = $_GET['str'];
Sign up to request clarification or add additional context in comments.

Comments

1

You're submitting a GET request. To access the parameter, use $_GET['str']

Comments

1

You're passing the variable as a GET parameter, rather than POST. If you want to access the variable this way, you will need to try this:

<?php
$str = $_GET['str'];
echo ('string entered: '.$str);

4 Comments

How would I pass the variable as post parameter?
You will need to pass it with an HTML form, or you can set the value of an existing POST value just like any other array, which is explained here: stackoverflow.com/questions/3418044/….
Alright and I can't do that through the url directly, can I?
Correct. POST is designed to be hidden from the browser, so if you're setting or modifying a value in the parameter list in the URL, you are modifying a GET variable.

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.