0

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?

6
  • "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"... I don't think I understand. It sounds like you are saying that the browser shows the correct number but the source code contains a PHP error message in its place, which is unlikely. Commented Sep 14, 2010 at 0:24
  • Another unrelated comment: use of short tags (<?) is usually cautioned against; see <a href="stackoverflow.com/questions/200640/… question</a>. Commented Sep 14, 2010 at 0:26
  • Try putting var_dump($_POST); at the top of the page. That way you can see what is in your $_POST. Maybe that will give you a hint about what is going wrong here. Commented Sep 14, 2010 at 0:30
  • Hammerite -- that's EXACTLY what I'm saying. For instance, viewing the source of the original page shows "<input type='hidden' name='slot' value=1>". When I echo the $_POST['slot'], the browser prints 1, and the view source shows the Undefined index error... Commented Sep 14, 2010 at 0:33
  • captaintokyo: var_dump($POST) yields the following: array(1) { ["slots"]=> string(1) "1" } Commented Sep 14, 2010 at 0:36

4 Answers 4

1

check for emptiness:

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

print "My foo is '$foo'";
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the advice. A more concise version: $foo = empty($_POST['foo'])?'default foo':$_POST['foo'];
0

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>";
?>

4 Comments

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']
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 ;)
0

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.

1 Comment

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

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.