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

Problem with delivering POST variable

P: n/a
Hello,

I use PHP 4.3.10 and want to deliver a post variable from a PHP page to
itself.
In my page mypage.php I have a hidden field named "dbaction" and a form
button. When clicking the form button a javascript function is called where
the dbaction hidden field is set to the value "change" (=actn) and then the
form gets submitted.
function doAction(actn) {
document.forms[0].dbaction.value = actn;
document.forms[0].submit();
}
actn is delivered properly here!

At the recall of mypage.php (after submit) I expect the hidden field
dbaction as POST variable know. I have the statement
if (isset($HTTP_POST_VARS['dbaction']))
to ask whether dbaction exists or not. At the first call of mypage.php it
doesn't exist because there was no post request but after the form submit
dbaction should exist as post variable (I have defined method=post in my
<form> tag).
What now happens is that after the post request dbaction is set but always
is an empty string instead of having the value "change".
What's going on here? Why does the post parameter not get delivered as it is
set before? Why do I get an empty string as delivered value?
By the way I have the php property "register_globals" set off which is
highly recommended due to security reasons and I want to let it turned off.

Any help to this would be much appreciated.
Thanks in advance.

Nice greetings from
Thomas
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Thomas Hoheneder wrote:
the form gets submitted.
function doAction(actn) {
document.forms[0].dbaction.value = actn;
document.forms[0].submit();
}
<snip>
What now happens is that after the post request dbaction is set but always
is an empty string instead of having the value "change".


<snip>

got to be something silly. Start with this reality chec. Hardcode the
doAction(actn) to set

document.forms[0].dbaction.value = 'realitycheck';

If it still comes back empty, do a var_dump($_POST) and see what did come
back, and find your mistake that way.

If it does show up as 'realitycheck', you are not passing what you think to
doAction().

Hope this helps.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #2

P: n/a
> document.forms[0].dbaction.value = 'realitycheck';

If it still comes back empty, do a var_dump($_POST) and see what did come
back, and find your mistake that way.


Now I have set a hard coded value ("realitycheck") like above and after that
the line
alert(document.forms[0].dbaction.value);
to get an alert box with the value of dbaction. It always shows -
correctly - "realitycheck". Furthermore I have given my hidden field a
default value:
<input type="hidden" name="dbaction" value="defaultvalue">
And I have also built in var_dump($_POST). But after recall of mypage.php
always the default value "defaultvalue" is shown in the var_dump($_POST) and
not the exprected value "realitycheck".
So it still doesn't work (field gets delivered but with wrong value). Do you
have any idea how to fix this?
I can't believe that I cannot deliver field values by php which are set by
javascript.
Thanks in advance.

Nice greetings from
Thomas
Jul 17 '05 #3

P: n/a
Thomas Hoheneder wrote:
document.forms[0].dbaction.value = 'realitycheck';

If it still comes back empty, do a var_dump($_POST) and see what did come
back, and find your mistake that way.


Now I have set a hard coded value ("realitycheck") like above and after
that the line
alert(document.forms[0].dbaction.value);
to get an alert box with the value of dbaction. It always shows -
correctly - "realitycheck". Furthermore I have given my hidden field a
default value:
<input type="hidden" name="dbaction" value="defaultvalue">
And I have also built in var_dump($_POST). But after recall of mypage.php
always the default value "defaultvalue" is shown in the var_dump($_POST)
and not the exprected value "realitycheck".
So it still doesn't work (field gets delivered but with wrong value). Do
you have any idea how to fix this?
I can't believe that I cannot deliver field values by php which are set by
javascript.
Thanks in advance.

Nice greetings from
Thomas


Continuing with reality check mode, we should probably strip this down to
the bare essentials. Create a php file that has only this:

----------- FILE:test.php ----------------
<script>
function doAction() {
ob("var1").value = "ran_doAction";
ob("reality").submit();
}

function ob(oname) {
if (document.getElementById)
return document.getElementById(oname);
else if (document.all)
return document.all[name];
}
</script>
<form name="reality" id="reality" method="post" action="test.php">
<input type="hidden" name="var1" id="var1" value="default">
<input type="hidden" name="var2" id="var2" value="leavemealone">
<button type="button" onclick="doAction();">Test me</button>
</form>
<?php
if(isset($_POST["var1"])) { var_dump($_POST); }
?>
----------- END FILE:test.php ----------------

Notice every object has both an id and a name, this is for compatibility,
and the function "ob" allows you to quickly refer to anything on the page.
Don't worry about the doctype declaration, <body> or <html> or any of that
stuff.

Bring this up and click the button a few times. Once you get this working,
compare it to your own code and find the differences.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.