Concating $_POST[variable] with another Variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Hodge
Forum Newbie
Posts: 6
Joined: Wed Dec 08, 2010 1:17 pm

Concating $_POST[variable] with another Variable

Post by Hodge »

Hi Folks, does anyone know what the correct syntax is to concatenate a $_POST variable with another variable? In the below example I have a counter variable running; I pair it with the "part" textfield to create dynamic textfields. Like part0, part1, part3 and so on. However in order to retain the value as buttons are clicked to add new fields I need to combine the $_POST[part] variable with the same counter variable. I've tried different methods like $_POST[part.$partRowNumber] or $_POST[part]$partRowNumber but that doesn't appear to be working. However, I can grab the value if I implicitly ask for $_POST[part1]; that works fine so I figure my syntax must be out of whack.

Code

while ($counter >= 0)
{

echo "<input type ='text' name = 'part".$partRowNumber."' size ='20' maxlength='60' value = '$_POST[part]' />";///in this last bit I need to combine $partRowNumber with $_POST[part] to define which textfield I want
$partRowNumber = $partRowNumber + 1;
$counter = $counter - 1;
}
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Concating $_POST[variable] with another Variable

Post by Jonah Bron »

Code: Select all

echo '<input type="text" name="part' . $partRowNumber . '" size="20" maxlength="60" value="' . $_POST['part' . $partRowNumber] . '" />';
That will work. You should always put indices of arrays in quotes, like this: $_GET['index']; not like this: $_GET[index].
Hodge
Forum Newbie
Posts: 6
Joined: Wed Dec 08, 2010 1:17 pm

Re: Concating $_POST[variable] with another Variable

Post by Hodge »

Awesome. Thanks Jonah. I'll start making sure I do that all of the time going forward. :)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Concating $_POST[variable] with another Variable

Post by AbraCadaver »

Many have tried to go down this road and it is needlessly complex. Use arrays, much simpler (not sure what you're doing with the loop or the session):

Code: Select all

$counter = 3;
while ($counter >= 0)
{
   echo "<input type ='text' name = 'part[$counter]' size='20' maxlength='60' value='" . $_POST['part'][$counter] . "' />";
   $SESSION[$counter]++;
   $counter--;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Hodge
Forum Newbie
Posts: 6
Joined: Wed Dec 08, 2010 1:17 pm

Re: Concating $_POST[variable] with another Variable

Post by Hodge »

I got it working using Jonah's syntax recommendation but I think I see what you're saying Abra (thanks for your comment btw) and I think I kinda sorta might be doing that in a round about way; I'm transferring the value from a Session variable that changes and is kind of like and index. I didn't want to have to increment or decrement that directly since as add or remove buttons were clicked the value needed to move. Like you said though, most of this is probably needlessly complex; I am still learning so its bound to be way to verbose.

Code: Select all

if (isset ($_POST ['addRow']))///adds row
		{
			$_SESSION['storedRepairWork'] = $_POST['repairWork'];///Storing Notes to Session Variable In case of Error
			echo "Add session session a = ".$_SESSION ['a'];
			$_POST['submitted'] = 1;
			///Start Parts Table
			$counter = $_SESSION['a'];
			$partRowNumber = 0 ;
			$addController = 1;///flips true to Control Form Header if Adding Rows form starts here; if not the submit button was clicked and for starts in submit block
			echo '<form name = "submitMonkey" action = "updateRepair.php" method = "post">';
			echo $tableSetup;//Setup Initial Table parameters	
			echo "<tr><td><b>Part Number</b></td><td><b>Part Cost</b></td><td><b>Notes</b></td><td><b>Hours</b></td></tr>";
			echo "<tr><td colspan = '1' valign = 'top' align = 'right'>"; //Setup Column
			
			
			while ($counter >= 0)
				{
					
					///echo $_POST[part.$partRowNumber];
					echo "<input type ='text' name = 'part".$partRowNumber."' size ='20' maxlength='60' value = '".$_POST['part'.$partRowNumber]."' />";
					$partRowNumber = $partRowNumber + 1;
					$counter = $counter - 1;
				}
			
			echo "</td>";
			
			
			///Start Parts Cost Table havent updated this yet to reflect the Jonah's Syntax
			$counter = $_SESSION['a'];
			
			echo "<td colspan = '1' valign = 'top' align = 'right'>"; //Setup Column
			$SESSION_partCostRowNumber = 0 ;
			while ($counter >= 0)
				{
					echo "<CENTER><input type ='text' name = 'cost".$SESSION_partCostRowNumber."' size ='20' maxlength='60' value = '1' /></CENTER>";
					$SESSION_partCostRowNumber = $SESSION_partCostRowNumber + 1;
					$counter = $counter - 1;
				}
			
			
			echo "</td>";
Hodge
Forum Newbie
Posts: 6
Joined: Wed Dec 08, 2010 1:17 pm

Re: Concating $_POST[variable] with another Variable

Post by Hodge »

AbraCadaver wrote:Many have tried to go down this road and it is needlessly complex. Use arrays, much simpler (not sure what you're doing with the loop or the session):

Code: Select all

$counter = 3;
while ($counter >= 0)
{
   echo "<input type ='text' name = 'part[$counter]' size='20' maxlength='60' value='" . $_POST['part'][$counter] . "' />";
   $SESSION[$counter]++;
   $counter--;
}


Actually Abra; I don't get this. Other than incrementing it, where are you using the $_SESSION[counter] array? Is [$counter] shorthand for it? I'm guessing so huh?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Concating $_POST[variable] with another Variable

Post by AbraCadaver »

Hodge wrote:Actually Abra; I don't get this. Other than incrementing it, where are you using the $_SESSION[counter] array? Is [$counter] shorthand for it? I'm guessing so huh?
I just changed your existing code, I have no idea what you're doing with the session stuff. $counter is your var, in my example I set it to 3 and you have it decrementing each iteration, so you would have four elements in the session array: $_SESSION[3], $_SESSION[2], $_SESSION[1], $_SESSION[0], but I don't know why. :?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply