Hey, I've got what has become an extremely frustrating problem with $_Post variables. I'll give examples of code rather than the actual segments to save time and confusion. On one page I'm doing this:
<? echo "<form name='form' action='page.php' method='post'>
<input type='hidden' name='slot' value=".$i.">
</form>";
?>
The $i is an index in a while loop (I'm echoing this simple form several times). The form itself is submitted with a bit a javascript.
All's well at this point, the form is submitted properly and takes me to another page, where I need to use that "slot" value to do some other junk. However, when I try to do this:
<? echo "<form name='another_form' action='another_page.php' method='post'>
<input type='hidden' name='slot_num' value=".$_POST['slot'].">
//SOME OTHER HIDDEN VARS
</form>";
?>
or this...
<? echo "<form name='another_form' action='another_page.php' method='post'>
<input type='hidden' name='slot_num' value=";
echo $_POST['slot'];
echo ">
//SOME OTHER HIDDEN VARS
</form>";
?>
or this...
<? //TOP OF PAGE
$slots = $_POST['slot'];
?>
<? //FURTHER DOWN
<? echo "<form name='another_form' action='another_page.php' method='post'>
<input type='hidden' name='slot_num' value=".$slots.">
//SOME OTHER HIDDEN VARS
</form>";
?>
...all I get is an Undefined index: slot etc.. etc... error, and source of the php document just has blank space. Funny thing is, if I simply do this:
echo $_POST['slot'];
at the top of the page, it prints out the value from the previous page just fine, however, if I view the source, it still shows an Undefined index error instead of the value. I KNOW the value is getting passed because it prints, but I can't use it for anything else because if I try to include it in my php code, it just displays an error and gives a blank value!
I've also tried using $HTTP_POST_VARS['slots'] with the same result... I am at wits end afte开发者_Go百科r several hours of experimentation... any advice?
check for emptiness:
if(empty($_POST['foo'])) {
$foo = "default foo";
} else {
$foo = $_POST['foo'];
}
print "My foo is '$foo'";
Edit:
Based on your comments and posted code, you seem to be trying to echo $_POST['slot']
when you should be echoing $_POST['slots']
... note the s
at the end.
Since $_POST
is a super global, it is available anywhere on your page, so your code should work.
I noticed that you mixed slot
and slots
as the index in you post (you wrote $HTTP_POST_VARS['slots']
as the last example and $_POST['slot']
everywhere else). Could that be the reason?
To check what $_POST
looks like, try this right about where you want to print the hidden value (though it should work the same anywhere on your page):
<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>
Also, your slot isn't being echoed with quote marks around it, so it should be:
<?php echo "<form name='another_form' action='another_page.php' method='post'>
<input type='hidden' name='slot_num' value='".$_POST['slots']."'>
//SOME OTHER HIDDEN VARS
</form>";
?>
Well I can't see anything wrong apart from a few syntax problems... okay so first of all, can you post me your javascript so I can see that.
Now instead of what you are doing with that, try this code:
?> <form name="another_form" action="another_page.php" method="post">
<input type="hidden" name="slot_num" value="<?php echo isset($_REQUEST['slot'])?$_REQUEST['slot']:'not found'; ?>">
</form>
<?
I am not fully sure what is wrong but i don't think the problem is in the code you've posted. it's probably sitting elsewhere so keep sending through code.
Try replacing...
<? //TOP OF PAGE
$slots = $_POST['slot'];
?>
with..
<?php //TOP OF PAGE
isset($_POST['slot']) : $slots = $_POST['slot'] ? $slots = '';
?>
Best of luck, hope that helps!
精彩评论