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!

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174

3 Answers3

1

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

Ryan
  • 14,392
  • 8
  • 62
  • 102
1

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

$param = $_GET['str'];
walther
  • 13,466
  • 5
  • 41
  • 67
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);
hRdCoder
  • 579
  • 7
  • 30
  • How would I pass the variable as post parameter? – LinusGeffarth Jun 13 '17 at 19:09
  • 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: https://stackoverflow.com/questions/3418044/setting-post-variable-without-using-form. – hRdCoder Jun 13 '17 at 19:14
  • Alright and I can't do that through the url directly, can I? – LinusGeffarth Jun 13 '17 at 19:15
  • 1
    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. – hRdCoder Jun 13 '17 at 19:17