Page 1 of 1

Concating $_POST[variable] with another Variable

Posted: Tue Dec 14, 2010 10:57 am
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;
}

Re: Concating $_POST[variable] with another Variable

Posted: Tue Dec 14, 2010 11:05 am
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].

Re: Concating $_POST[variable] with another Variable

Posted: Tue Dec 14, 2010 11:09 am
by Hodge
Awesome. Thanks Jonah. I'll start making sure I do that all of the time going forward. :)

Re: Concating $_POST[variable] with another Variable

Posted: Tue Dec 14, 2010 11:14 am
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--;
}

Re: Concating $_POST[variable] with another Variable

Posted: Tue Dec 14, 2010 11:28 am
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>";

Re: Concating $_POST[variable] with another Variable

Posted: Tue Dec 14, 2010 11:41 am
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?

Re: Concating $_POST[variable] with another Variable

Posted: Tue Dec 14, 2010 11:54 am
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. :?