Forum Moderators: coopster

Message Too Old, No Replies

$_POST['$variable'] doesn't work?

         

RoaCH

9:26 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



I have a form that is created using an array of item numbers and assigns the name to each form field a different item number. On the form processing page I need to be able to call the posted values using the same item numbers.

My syntax looks like this:
$_POST['$_SESSION['item'][$index]']
But it seem to be calling a null value every time.

Is there some simple syntax I am missing?

Thank you!

ChadSEO

9:36 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



RoaCH,

Welcome to WebmasterWorld!

I tested this on my site a little bit, and was able to get it to work using the following syntax:

$_POST[$_SESSION['item'][$index]]

Note the lack of single quotes around the $_SESSION variable. Hopefully that will work for you as well.

Chad

RoaCH

9:54 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



Hmmm... unfortunately that is still giving me to same result.

On my page, I have a text field with the name set to the value of $_SESSION['item'][12]

Earlier in the script, $_SESSION['item'][12] was set to "KBK4884"

When I type "123" into the text field and submit the form, the syntax

echo $_POST['KBK4884'];

will return the value "123" but

echo $_POST[$_SESSION['item'][12]];

returns no value.

I'm stumped :/

ChadSEO

10:06 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



RoaCH,

Strange, the code that I tested, and worked, was:

$_SESSION['item'][1] = "test2";
echo "Found: " . $_POST[$_SESSION['item'][1]] . "<br>\n";

This successfully printed the "test2" value from the form. If you echo just $_SESSION['item'][12], does it print the correct value?

Chad

RoaCH

10:30 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



Here is my code:

<?php
session_start();
$_SESSION['item'] = array();
$_SESSION['item'][1] = "test2";
echo "Found: " . $_POST[$_SESSION['item'][1]] . "<br>\n";
echo $_SESSION['item'][1] . "<br>";
?>
<form method="POST" action="<?php $_SELF?>">
<input type="text" name="<?php $_SESSION['item'][1]?>">
<input type="submit" value="submit">
</form>

Here is what I see after submitting "123" into the form www21.brinkster.com/dousto/test2.bmp

ChadSEO

10:46 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



RoaCH,

If you do a view source on your page, do you get:

<input type="text" name="test2">

Or

<input type="text" name="">

I think the problem lies in the line following line:

<input type="text" name="<?php echo $_SESSION['item'][1];?>">

I've bolded the part I added, I think that should solve your problem.

Chad

RoaCH

11:01 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



That is very strange. I do get the source:

Found: <br>
test2<br><form method="POST" action="">
<input type="text" name="">
<input type="submit" value="submit">
</form>

Both before and after I changed that line to look like yours. Any ideas as to what else might cause that to happen?

RoaCH

11:07 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



I think I might have something!

I tried this code:

<?php echo "<input type=\"text\" name=\"".$_SESSION['item'][1]."\">";?>

and it worked!

Thank you for taking your time to help me out. I still don't know why that method works. I didn't know there was a difference between them. Thanks again!