Page 1 of 1

Problem with $_POST

Posted: Mon Mar 27, 2006 9:41 am
by mattcooper
Hi all,

I'm trying to make a dynamically generated form sticky - successful in every way, except for the textfields generated with the following code:

Code: Select all

$el_num =  $_POST['el_menu'];
	$i = 1;
	$x = 5;
		while($i <= $el_num){
		echo "External link # $i<br><input type=\"text\" name=\"el_name_$i\" value=\""; if(isset($_POST['el_name_{$i}'])){ echo $_POST['el_name_{$i}'];}else{ echo "Label"; } echo "\" onFocus=\"MM_setTextOfTextfield('el_name_{$i}','','')\"> Example: Sponsor $i&nbsp;&nbsp;<input type\"text\" name=\"el_url_$i\" value=\""; if(isset($_POST['el_url_{$i}'])){ echo $_POST['el_url_{$i}'];}else{ echo "URL"; } echo "\" onFocus=\"MM_setTextOfTextfield('el_url_{$i}','','')\"><a href=\"help.php?context=el_urls\" target=\"_blank\"><img src=\"images/help.gif\" border=\"0\"></a><br>";
		$i++;
		}
	}
}
The test always returns false and displays the defualt text for the textfields instead of the posted value. If I echo out "el_name_2" elsewhere i the page, it's fine. But when I try to do it using the "$i" variable, it doesn't work - even though "$i" is storing the numbers correctly.

Can anyone see what I've done wrong?

Thanks in advance!

Posted: Mon Mar 27, 2006 10:13 am
by hawleyjr
Variables can only be parsed inside double quotes...

Posted: Mon Mar 27, 2006 10:23 am
by mattcooper
That's what I thought, but how is it then that the other aspects of this script work fine? Everything is spot-on apart from the bit in question...

What's the fix for this? Sorry, but I just can't figure it out!!

Thanks

Posted: Mon Mar 27, 2006 10:49 am
by hawleyjr
It's working fine in the first string because you are parsing variables in " double quotes.

The post vars:

Code: Select all

$_POST['el_name_{$i}']
Are being parsed in single quotes...

Posted: Mon Mar 27, 2006 11:36 am
by mattcooper
I see that... but can't figure out the fix!!

Thanks for your time...

Posted: Mon Mar 27, 2006 12:16 pm
by feyd
use double quotes.

Code: Select all

$_POST["el_name_{$i}"]

Posted: Mon Mar 27, 2006 1:13 pm
by mattcooper
Thank you, Feyd. I use single quotes habitually in array parantheses, it didn't occur to me to do that! And it's only Monday... :D

Posted: Mon Mar 27, 2006 1:22 pm
by hawleyjr
mattcooper wrote:Thank you, Feyd. I use single quotes habitually in array parantheses, it didn't occur to me to do that! And it's only Monday... :D
:? :? :?
hawleyjr wrote:Variables can only be parsed inside double quotes...
hawleyjr wrote:It's working fine in the first string because you are parsing variables in " double quotes.

The post vars:

Code: Select all

$_POST['el_name_{$i}']
Are being parsed in single quotes...

Posted: Tue Mar 28, 2006 5:15 am
by mattcooper
hawleyjr wrote:It's working fine in the first string because you are parsing variables in " double quotes.

The post vars:

Code: Select all

$_POST['el_name_{$i}']
Are being parsed in single quotes...
Far too cryptic for me when my head is fried at the end of a working day... sorry fella.