By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,262 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,262 IT Pros & Developers. It's quick & easy.

$_POST not always set?

P: n/a
Is it possible to click on a button, and not have it's value and name stored
in _POST?

I have this script with a button on it. When you click the button, the page
posts to itself. The first time you click this Save Changes button, _POST
is empty. After clicking the button, and the page is loaded again, if you
click Save Changes once more the _POST is properly set. What's up with
that?

$studentCode; // Will hold the current student code, if applicable

....

foreach(array_keys($_POST) as $key)
{
$$key = $_POST[$key];
print "$key is ${$key}<br />";
echo "It is: " . $_POST["Submit"] . "<br>";
}
if ($_POST["Submit"] == "Add Student") // Save button was hit
addRecord();
elseif ($_POST["Submit"] == "Save Changes") // Save changes button was hit
updateStudent();
echo "We're saving changes<br>";
elseif ($_GET["action"] != "add")
getOldData($record);

echo "<table summary=\"\" cellpadding=\"5\">";

function printLabelCell()
{
echo "\n<tr><td align=\"right\">";
}

if (isset($_GET["studentCode"]))
$studentCode = $_GET["studentCode"];
elseif (isset($_POST["studentCode"]))
$studentCode = $_POST["studentCode"];

if ($studentCode != "")
$postType = "post?studentCode=" . $studentCode;
else
$postType = "post";

echo '<form action="addEditStudent.php" method="' . $postType . '">';

// ... print controls in the cells ...

echo "<tr><td colspan=\"2\" align=\"center\">";

echo '<br>';

if ($_GET["action"] == "add" or isset($_POST["firstName"]))
echo '<input type="Submit" name="Submit" value="Add Student">&nbsp;
&nbsp; &nbsp; ';
else
echo '<input type="Submit" name="Submit" value="Save Changes">';

echo '&nbsp; &nbsp; &nbsp; <input type="Reset" name="Reset" value="Clear
Data"><br><br>';

echo "</td></tr></table>";

echo "</form>";

function DisplayTextbox($vname, $size, $value)
{
echo "</td><td><Input type=\"text\" name = \"" . $vname . "\" Size = \""
.. $size . "\" value=\"" . $value . "\"></td></tr>";
}

mysql_close($link);
....

<Ade
--
Adrian Parker. Ordained priest. <ad***********@sympatico.ca>

"A society that views graphic violence as entertainment ...should not be
surprised when senseless violence shatters the dreams of it's youngest and
brightest..." - Ensign (March 2004)
Jul 17 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a

"Adrian Parker" <ad***********@NOSPAMsympatico.ca> wrote in message
news:ZH*******************@news20.bellglobal.com.. .
Is it possible to click on a button, and not have it's value and name stored in _POST?

I have this script with a button on it. When you click the button, the page posts to itself. The first time you click this Save Changes button, _POST
is empty. After clicking the button, and the page is loaded again, if you
click Save Changes once more the _POST is properly set. What's up with
that?
Methinks my problem comes about when I try to force data into _POST.

method="post?action=....

Is there a solution to this?
Adrian

$studentCode; // Will hold the current student code, if applicable

...

foreach(array_keys($_POST) as $key)
{
$$key = $_POST[$key];
print "$key is ${$key}<br />";
echo "It is: " . $_POST["Submit"] . "<br>";
}
if ($_POST["Submit"] == "Add Student") // Save button was hit
addRecord();
elseif ($_POST["Submit"] == "Save Changes") // Save changes button was hit updateStudent();
echo "We're saving changes<br>";
elseif ($_GET["action"] != "add")
getOldData($record);

echo "<table summary=\"\" cellpadding=\"5\">";

function printLabelCell()
{
echo "\n<tr><td align=\"right\">";
}

if (isset($_GET["studentCode"]))
$studentCode = $_GET["studentCode"];
elseif (isset($_POST["studentCode"]))
$studentCode = $_POST["studentCode"];

if ($studentCode != "")
$postType = "post?studentCode=" . $studentCode;
else
$postType = "post";

echo '<form action="addEditStudent.php" method="' . $postType . '">';

// ... print controls in the cells ...

echo "<tr><td colspan=\"2\" align=\"center\">";

echo '<br>';

if ($_GET["action"] == "add" or isset($_POST["firstName"]))
echo '<input type="Submit" name="Submit" value="Add Student">&nbsp;
&nbsp; &nbsp; ';
else
echo '<input type="Submit" name="Submit" value="Save Changes">';

echo '&nbsp; &nbsp; &nbsp; <input type="Reset" name="Reset" value="Clear
Data"><br><br>';

echo "</td></tr></table>";

echo "</form>";

function DisplayTextbox($vname, $size, $value)
{
echo "</td><td><Input type=\"text\" name = \"" . $vname . "\" Size = \"" . $size . "\" value=\"" . $value . "\"></td></tr>";
}

mysql_close($link);
...

<Ade
--
Adrian Parker. Ordained priest. <ad***********@sympatico.ca>

"A society that views graphic violence as entertainment ...should not be
surprised when senseless violence shatters the dreams of it's youngest and
brightest..." - Ensign (March 2004)

Jul 17 '05 #2

P: n/a
In article <ZH*******************@news20.bellglobal.com>,
"Adrian Parker" <ad***********@NOSPAMsympatico.ca> wrote:
if ($studentCode != "")
$postType = "post?studentCode=" . $studentCode;
else
$postType = "post";

echo '<form action="addEditStudent.php" method="' . $postType . '">';

As far as I know, this isn't correct. method is either "post" or "get".
If the method is "post", you put your variables into $_POST like this:

echo '<form action="addEditStudent.php" method="post">';
echo '<input type="hidden" name="studentCode" value="' . $studentCode .
'">';
echo '</form>';

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #3

P: n/a
Jan Pieter Kunst wrote:
In article <ZH*******************@news20.bellglobal.com>,
"Adrian Parker" <ad***********@NOSPAMsympatico.ca> wrote:
if ($studentCode != "")
$postType = "post?studentCode=" . $studentCode;
else
$postType = "post";

echo '<form action="addEditStudent.php" method="' . $postType . '">';

As far as I know, this isn't correct. method is either "post" or "get".
If the method is "post", you put your variables into $_POST like this:

echo '<form action="addEditStudent.php" method="post">';
echo '<input type="hidden" name="studentCode" value="' . $studentCode .
'">';
echo '</form>';


You can do a POST and pass parameters in the URL, then treat them as if
they were a GET:

<form action="whatever.php?course=17" method="post">
<input type="hidden" name="studentCode" value="542"/>
<input type="submit"/>
</form>

The script that receives the POST from this form can do

## course is available in the $_GET array,
## but this was NOT a GET request!
if ($_GET['course'] != '17') {
exit('Error: course must be 17.');
}

if ($_POST['studentCode'] != '542') {
exit('Error: studentCode must be 542.');
}
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #4

P: n/a
On 29 Apr 2004 20:46:56 GMT
Pedro Graca <he****@hotpop.com> wrote:
Jan Pieter Kunst wrote:
In article <ZH*******************@news20.bellglobal.com>,
"Adrian Parker" <ad***********@NOSPAMsympatico.ca> wrote:
if ($studentCode != "")
$postType = "post?studentCode=" . $studentCode;
else
$postType = "post";

echo '<form action="addEditStudent.php" method="' . $postType

. '">';
As far as I know, this isn't correct. method is either "post" or
"get". If the method is "post", you put your variables into $_POST
like this:

echo '<form action="addEditStudent.php" method="post">';
echo '<input type="hidden" name="studentCode" value="' .
$studentCode . '">';
echo '</form>';


You can do a POST and pass parameters in the URL, then treat them as
if they were a GET:

<form action="whatever.php?course=17" method="post">
<input type="hidden" name="studentCode" value="542"/>
<input type="submit"/>
</form>

The script that receives the POST from this form can do

## course is available in the $_GET array,
## but this was NOT a GET request!
if ($_GET['course'] != '17') {
exit('Error: course must be 17.');
}

if ($_POST['studentCode'] != '542') {
exit('Error: studentCode must be 542.');
}


You can also do

$_FOO = array_merge($_GET, $_POST);

and use $_FOO instead of $_GET or $_POST. Just remember that the keys
from $_POST will overwrite any equal key on $_GET.

Jul 17 '05 #5

P: n/a
Kelly Thompson wrote:
You can also do

$_FOO = array_merge($_GET, $_POST);

and use $_FOO instead of $_GET or $_POST. Just remember that the keys
from $_POST will overwrite any equal key on $_GET.


Or use the $_REQUEST array, which inludes $_GET, $_POST, and $_COOKIE :)

--------

<?php // xx.php
setcookie('all', 'cookie');
setcookie('COOKIE', 'cookie');
?>

<form action="xx2.php?all=get&GET=get" method="post">
<input type="hidden" name="all" value="post"/>
<input type="hidden" name="POST" value="post"/>
<input type="submit"/>
</form>

--------

<?php // xx2.php
header('Content-Type: text/plain');

echo '$_GET: '; print_r($_GET);
echo '$_POST: '; print_r($_POST);
echo '$_COOKIE: '; print_r($_COOKIE);
echo '$_REQUEST: '; print_r($_REQUEST);
?>

--------

Result is:

$_GET: Array
(
[all] => get
[GET] => get
)
$_POST: Array
(
[all] => post
[post] => post
)
$_COOKIE: Array
(
[COOKIE] => cookie
[all] => cookie
)
$_REQUEST: Array
(
[all] => cookie
[GET] => get
[post] => post
[COOKIE] => cookie
)

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #6

P: n/a
Adrian Parker a écrit le 29/04/2004 :
[...]
if ($studentCode != "")
$postType = "post?studentCode=" . $studentCode;
else
$postType = "post";

echo '<form action="addEditStudent.php" method="' . $postType . '">';


Not checked but consider puting the studentCode= in the target URL of
the form and not in the method.
Try this :

if ($studentCode != "") {
$postUrl = "addEditStudent.php?studentCode=".$studentCode ;
} else {
$postUrl = "addEditStudent.php";
}
echo '<form action="'.$postUrl.'" method="POST">';
Jul 17 '05 #7

P: n/a
Pedro Graca <he****@hotpop.com> wrote in news:c6rtun$fklfl$1@ID-
203069.news.uni-berlin.de:
Or use the $_REQUEST array, which inludes $_GET, $_POST, and $_COOKIE :)


That depends on a setting in php.ini, so I wouldn't recommend it to newbies
or to anyone who doesn't have complete control over their server. Using it
mindlessly can result in code that mysteriously breaks when moved to
another system.
Jul 17 '05 #8

P: n/a
On 29 Apr 2004 23:05:36 GMT
Eric Bohlman <eb******@earthlink.net> wrote:
Pedro Graca <he****@hotpop.com> wrote in news:c6rtun$fklfl$1@ID-
203069.news.uni-berlin.de:
Or use the $_REQUEST array, which inludes $_GET, $_POST, and
$_COOKIE :)


That depends on a setting in php.ini, so I wouldn't recommend it to
newbies or to anyone who doesn't have complete control over their
server. Using it mindlessly can result in code that mysteriously
breaks when moved to another system.


Which setting?
Jul 17 '05 #9

P: n/a
Kelly Thompson wrote:
On 29 Apr 2004 23:05:36 GMT
Eric Bohlman <eb******@earthlink.net> wrote:
Pedro Graca <he****@hotpop.com> wrote in news:c6rtun$fklfl$1@ID-
203069.news.uni-berlin.de:
> Or use the $_REQUEST array, which inludes $_GET, $_POST, and
> $_COOKIE :)
That depends on a setting in php.ini, so I wouldn't recommend it to
newbies or to anyone who doesn't have complete control over their
server. Using it mindlessly can result in code that mysteriously
breaks when moved to another system.


Thank you for pointing that out, Eric.
Which setting?


gpc_order
In my php.ini it is

gpc_order = "GPC"

so if there is a $_GET['x'], a $_POST['x'], and a $_CCOKIE['x']
on my configuration $_REQUEST['x'] will be $_COOKIE['x']

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #10

P: n/a
On 30 Apr 2004 00:27:22 GMT
Pedro Graca <he****@hotpop.com> wrote:
Kelly Thompson wrote:
On 29 Apr 2004 23:05:36 GMT
Eric Bohlman <eb******@earthlink.net> wrote:
Pedro Graca <he****@hotpop.com> wrote in news:c6rtun$fklfl$1@ID-
203069.news.uni-berlin.de:

> Or use the $_REQUEST array, which inludes $_GET, $_POST, and
> $_COOKIE :)

That depends on a setting in php.ini, so I wouldn't recommend it

to> newbies or to anyone who doesn't have complete control over
their> server. Using it mindlessly can result in code that
mysteriously> breaks when moved to another system.


Thank you for pointing that out, Eric.
Which setting?


gpc_order

In my php.ini it is

gpc_order = "GPC"

so if there is a $_GET['x'], a $_POST['x'], and a $_CCOKIE['x']
on my configuration $_REQUEST['x'] will be $_COOKIE['x']


Then one more reason to do it yourself with array_merge().
Jul 17 '05 #11

P: n/a
I often run into situations where there is nothing stored in $_POST.
For me, whenever I run into this, it turns out that the version of PHP
on the server I'm on is pre- version 4.1.
Jul 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.