Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

$_POST with $var

Status
Not open for further replies.

arios2mx

Technical User
Dec 23, 2002
60
MX
Hi!

I have the following

I got 6 variables with POST:
$quantity=5
$serial1=111
$serial2=222
$serial3=333
$serial4=abc
$serial5=qwe
.
.
.
$counter=1;
while($counter<$quantity)
{
$number=1;
echo $_POST['serial($number)'];
$number +=1;
$counter +=1;
}

I wanted to get
111
222
333
abc
qwe
$counter=5;

// I think that my problem is around ($number) inside echo &quot;$_POST.....&quot;; I have tried several combinations .. NO LUCK ,

Thanks
 
I havn't got my manual with me but try:

$_POST[&quot;serial&quot;+$number];

This should generate strings of serial1,serial2 etc.
Justslot it into your code

regards
 
maybee not, perhaps he can confirm. I think he wants to emulate:

echo $_post['serial1'];
echo $_post['serial2'];
.
.
echo $_post['serial6'];

mind you with only six why bother with the loop ?
 
Ok, if you have register globals set on your server then you could use the variable variables.
Code:
$counter=1;
while($counter<$quantity)
    {
    $varName = &quot;serial&quot; . $counter;
    echo ${$varName};
    $counter++;
    }

Or

Code:
$counter=1;
while($counter<$quantity)
    {
    $varName = &quot;serial&quot; . $counter;
    echo $_POST[$varName];
    $counter++;
    }

You were also resetting the variable $number back to 1 everytime you went around the while loop.
 
Keep in mind that with the correct formatting of the POST-method form which submits the information, it is possible for the submitted data to appear in an array. The secret is nameing the form elements well.

For example, if you have a form of the form:

Code:
<html><body><form action=&quot;somescript.php&quot; method=&quot;post&quot;>
<input type=&quot;test&quot; name=&quot;foo[]&quot;>
<input type=&quot;test&quot; name=&quot;foo[]&quot;>
<input type=&quot;test&quot; name=&quot;foo[]&quot;>
<input type=&quot;test&quot; name=&quot;foo[]&quot;>
<input type=&quot;submit&quot;>
</form></body></html>

Then when the form is filled out and submitted, $_POST['foo'] will itself be an array in somescript.php. This can make traversal of multiple strings a lot easier.

You can also name the form elements as:

Code:
<input type=&quot;test&quot; name=&quot;foo[1]&quot;>
<input type=&quot;test&quot; name=&quot;foo[2]&quot;>
<input type=&quot;test&quot; name=&quot;foo[3]&quot;>
<input type=&quot;test&quot; name=&quot;foo[4]&quot;>

And have specific array elements contain specific numbers.

Turning on register_globals is contraindicated for security reasons. See: for more information.


Want the best answers? Ask the best questions: TANSTAAFL!!
 
Thanks Westbury!


The second example that you wrote works!

I think Sleipnir gave me some idea how things should be done, I am going to test his example on another section of code.

Thank you very much.

I am doing a database with several components some of them have a serial number, some of them doesn't so If I don't want to use something else than PHP this is the only method I found to save this information according to the quantity of products, that's why I use a loop


Alex
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top