-1

I am trying to figure out how I can get the value of the ?thread on POST. So if ?thread=12; I want to return that value when the post is submitted and then save it in a variable.

Thread's Value

<?php foreach($threads as $thread) :?>
<td><a href = "?thread=<?php echo $thread['id'];?>">Thread Name</a></td>
<?php endforeach ; ?>

Form

<form action = "." method = "post">
<input type = "submit" value = "submit">
</form>
  • To retrieve GET variables you can use superglobal variable `$_GET`. So, `$thread = $_GET['thread'];` To retrieve POST variables use `$_POST` instead. – fusion3k Mar 15 '16 at 22:57

1 Answers1

0

If your URL looks like this:

http://example.com/test.php?thread=12

Then you can access the value of 'thread' in your test.php file via $_GET.

$value = $_GET[ 'thread' ];
// $value = 12

If the URL looks like this:

http://example.com/test.php

and you use a form like this to pass the thread value

<form action = "." method = "post">
    <input type = "hidden" name = "thread" value = "12">
    <input type = "submit" value = "submit">
</form>

Then you can access the value of 'thread' in your test.php file via $_POST.

$value = $_POST[ 'thread' ];
// $value = 12

I hope this clears up the confusion between $_POST and $_GET.

filkaris
  • 41
  • 6
  • So I created the input, but the problem is that I want my value to be dynamic, not just static. I tried doing this, – Aidan Leuck Mar 15 '16 at 23:26
  • The input name has to be the same with the array index, so in this case you would check $_POST[ 'threadID' ]. Also if you want to pass the value via the URL you should use $_GET[ 'threadID' ] – filkaris Mar 16 '16 at 00:18