To $_POST or not to $_POST ... Help me please.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sillywilly
Forum Newbie
Posts: 19
Joined: Thu May 02, 2002 5:11 pm

To $_POST or not to $_POST ... Help me please.

Post by sillywilly »

I have the following:

Code: Select all

<?php
//Section 1
function S1Q1()&#123;
global $PHP_SELF;
global $S1Q1;
global $S1Q1;
?>
<form name="form1" method="post" action="<?php echo $PHP_SELF; ?>">
Section 1<hr> <br> 
Do you play computer games? <br>
Yes 
<input type="radio" name="S1Q1" value="Yes">
No 
<input type="radio" name="S1Q1" value="No">
<br><br> 
Why dont you play computer games? (More than one can be selected)
<br>
They are boring<br>
<input type="checkbox" name="S1Q2" value="S1Q2(A)">
<br>
They are too expensive<br>
<input type="checkbox" name="S1Q2" value="S1Q2(B)">
<br>
I have better things to do with my time<br>
<input type="checkbox" name="S1Q2" value="S1Q2(C)">
<input type="hidden" name="stage" value="2">
 
<br>
<input type="submit" name="Submit" value="Submit">
</form>
</form>
<?php
&#125;
?>
?>
<?php 
//Section 2
Function S2Q1()&#123;
global $question1;
global $S1Q1;
global $S1Q2;
global $PHP_SELF;
global $_POST;
?>
<form name="form1" method="post" action="<?php echo $PHP_SELF; ?>">
Section 2 <input type="text" name="question2">
<input type="hidden" name="S1Q1" value="<?php echo htmlspecialchars($S1Q1); ?>">
<input type="hidden" name="S1Q2" value="<?php echo htmlspecialchars($S1Q2); ?>">
<?php
while (list($k, $v) = each($_POST))
&#123;
print("<input type=hidden name="$k" value="".htmlspecialchars($v)."">
"); 
&#125;
?>
<input type="submit" name="Submit" value="Submit">
<input type="hidden" name="stage" value="3">
</form>
<?php
&#125;
?>
?>
<?php
<?php
//Section 3
Function S3Q1()&#123;
global $question1;
global $S1Q1;
global $S1Q2;
global $PHP_SELF;
global $HTTP_POST_VARS;
?>
Section 3<hr><br>
3. Why don't you play computer games? (More than one can be selected)
<br>
<form name="form1" method="post" action="<?php echo $PHP_SELF; ?>">
I don't have a computer/console<br>
<input type="checkbox" name="S1Q3" value="S1Q3(A)"><br>
Playing interactive games is unsociable<br>
<input type="checkbox" name="S1Q3" value="S1Q3(B)"><br>
Playing interactive games is boring<br>
<input type="checkbox" name="S1Q3" value="S1Q3(C)"><br>
Playing interactive games is geeky<br>
<input type="checkbox" name="S1Q3" value="S1Q3(D)"><br>
They're expensive<br>
<input type="checkbox" name="S1Q3" value="S1Q3(E)"><br>
I have never had the opportunity<br>
<input type="checkbox" name="S1Q3" value="S1Q3(F)"><br>
I don't have the time<br>
<input type="checkbox" name="S1Q3" value="S1Q3(G)"><br>
Other<br>
<input type="checkbox" name="S1Q3" value="S1Q3(H)"><br>
<?php
$pr= current($HTTP_POST_VARS);
do 
&#123;
print("$pr");
&#125;
while($pr=next($HTTP_POST_VARS))
?>
<input type="submit" name="Submit" value="Submit">
<input type="hidden" name="stage" value="3">
</form>
<?php
&#125;
?>
?>
<?php
<?php
// Determine the sections to diplay
// Determine the sections to diplay
switch ($stage) &#123;
    case "2":
if ($S1Q1 == "No")&#123;
S3Q1();
S3Q1();
&#125;
else &#123;
S2Q1();
&#125;
    break;
    case "3":
print ("You have said you do not play computer games");       
    break;
    case "done":
        Print("Thank You for your answers");
    break;
    default:
S1Q1();
&#125;
?>
But i cannot get the following to work:

Code: Select all

<?php
while (list($k, $v) = each($_POST))
{
print("<input type=hidden name="$k" value="".htmlspecialchars($v)."">
"); 
}
?>
I have tried putting $_POST in the switch like this:

Code: Select all

switch ($_POST&#1111;'stage']) {
But this stops the swicth from working.

Please could someone give me a hand as i am very new to PHP, only started learning a few days ago.

:0)
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

You don't need to do global on $_POST, it is automatically global.

Do:

Code: Select all

echo "<pre>";
print_r($_POST);
echo "</pre>";
At the top of the script to see what $_POST really contains.
sillywilly
Forum Newbie
Posts: 19
Joined: Thu May 02, 2002 5:11 pm

Post by sillywilly »

I have just found out that my web hosting people have not got the latest version of PHP installed, thus the version i have installed does not support $_POST.

Is there a work around so that all the variables passed will be printed as hidden form elements in the next form without using $_POST.

The version installed is 4.0.6.
samscripts
Forum Commoner
Posts: 57
Joined: Tue Apr 23, 2002 4:34 pm
Location: London, UK

Post by samscripts »

use $HTTP_POST_VARS instead of $_POST. You will need to declare it global though.
User avatar
chiefmonkey
Forum Commoner
Posts: 25
Joined: Sat Apr 20, 2002 5:21 am
Location: Glasgow UK

Post by chiefmonkey »

samscripts wrote:use $HTTP_POST_VARS instead of $_POST. You will need to declare it global though.
eh,
If it's an older version of PHP then does it need to be declared as a global variable, I thought it was only 4.1.0 and above that $HTTP_POST_VARS was no longer global by default

George
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

It is a global variable in the traditional sense of the word. You still need to declare it global in the function to use it though.
sillywilly
Forum Newbie
Posts: 19
Joined: Thu May 02, 2002 5:11 pm

Post by sillywilly »

Ok... i have the following code:

Code: Select all

<?php
while (list($k, $v) = each($HTTP_POST_VARS))
&#123;
print("<input type=hidden name="$k" value="".htmlspecialchars($v)."">
");
&#125;
?>
Which prints out all the varaibles passed to it...how would i adapt this code to exclude certain passed variables from displaying, like for example:

<input type="submit" name="Submit" value="Submit">
<input type="hidden" name="stage" value="3">
grigori
Forum Newbie
Posts: 1
Joined: Mon Jul 04, 2011 2:28 am

Re: To $_POST or not to $_POST ... Help me please.

Post by grigori »

I learned php and it like a c++. I had some knowledge about c++ )
vision correctionкосоглазие
Radeon 6990
Post Reply