DunnoHowToCode DunnoHowToCode - 5 months ago 5
HTML Question

$_GET is working but not for $_POST for url query

I have the following button which opens a new tab/window and redirecting to test.php page. I wanted to post the data of $a to test.php

<input type="button" onclick="window.open('test.php?var=<?php echo $a;?>','_blank');" value="test"/>



test.php


$b = $_GET['var'];


$_GET
in the above case will be able to retrieve the data posted from the url query, however if I change it to
$b = $_POST['var'];
I get undefined index error for var.
I read from other posts that I should not use GET for submission of sensitive data. So my question is: How can I modify the code such that I can post the data of variable
var
to test.php using
$_POST
?

Answer

GET requests include parameters in the URL. If you have a page 'foo.com/bar.php' you can pass it GET parameters in the URL, 'foo.com/bar.php?var=myvar'.

You can then retrieve the parameter using $_GET:

$var = $_GET['var'];

In a POST request, parameters are included in the request body, not in the URL. So if you want to send var using POST you need to use AJAX or submit a form with method="POST".

In your case you're using GET and trying to get the value from $_POST. That's not possible

Comments