views:

36

answers:

4

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 after several hours of experimentation... any advice?

+1  A: 

check for emptiness:

if(empty($_POST['foo'])) {
  $foo = "default foo";
} else {
  $foo = $_POST['foo'];
}

print "My foo is '$foo'";
Paulo Scardine
+1 for the advice.A more concise version:$foo = empty($_POST['foo'])?'default foo':$_POST['foo'];
Sebastián Grignoli
A: 

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.

Jason
Here is the javacript: function sub(i){ document.forms["upgrades"+i].submit(); } It is initiated by through onClick on another div...
A: 

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>";
?>
Peter Ajtai
As ordered, that print_r yields: Array( [slots] => 2) Which is spot on. But if I try to include $slots in the php code, it still comes in blank...
@user446882 - Ok, so your posted code would not work. You have `$_POST['slot']`... try `$_POST['slots']`
Peter Ajtai
WOW. I am a f$%^ing idiot. That definitely worked--but I also had to employ several of others' suggestions as well, finally it's all up and running. THANK YOU, THANK YOU, THANK YOU, to all of you who have been so kind and helpful--StackOverflow has always been my favorite place to come for help, and this re-enforces that. Again, I can't THANK YOU all enough.
@user446882 - We've all been there. A single letter is easy to miss ;)
Peter Ajtai
A: 

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!

Mikey1980