user710502
user710502

Reputation: 11471

how top use GET and POST simultaneusly

I have a page in php that gets from the url such as this code example

<form action="welcome.php" method="get">
//some interesting code here
</form>  

The issue now is that when i do the GET depending on its value I need to do a POST,

How can I use action="get" and action="post" in the same page?, I am kind of new to PHP so I am not sure if I can use two tags ("i dont think so but please correct me if i am wrong").

PS: I am getting to the same page "welcome.php" and posting to itself again, and depending on the value I am going to show different content.

Thank you

Upvotes: 0

Views: 130

Answers (3)

Sampson
Sampson

Reputation: 268344

You may access both through $_REQUEST if necessary. So no matter what your method type is, $_REQUEST will contain all of your submitted values. This way you don't need to determine whether it was by post, or by get that the data was submitted.

Upvotes: 1

Mike Purcell
Mike Purcell

Reputation: 19989

You will have to trap the $_GET value as a hidden var, so when the post is submitted again, your $_GET value will be available:

if (array_key_exists('myVar', $_GET)) {
    echo '<input type="hidden" name="myVar" value="' . $_GET['myVar'] .'" />
}

// Now when you resubmit the form you should have access to 'myVar'

echo $_REQUEST['myVar'];

You could also add the get value to the session super global:

if (array_key_exists('myVar', $_GET)) {
    $_SESSION['myVar'] = $_GET['myVar'];
}

// Now when you resubmit the form you should have access to 'myVar'

echo $_SESSION['myVar'];

Upvotes: 0

ceejayoz
ceejayoz

Reputation: 180024

Your question is a little unclear, but if I'm understanding it correctly, you can certainly pass GET variables via the form's action:

<form action="welcome.php?these=are&amp;get=variables" method="post">
//some interesting code here
</form>  

Upvotes: 2

Related Questions