My Problem - For Loops / Forms

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
GlennCurtis2009
Forum Newbie
Posts: 11
Joined: Wed Jul 08, 2009 7:43 am

My Problem - For Loops / Forms

Post by GlennCurtis2009 »

Hi guys, hope you can help becuase this problem is doing my head in!!


I have a form of 20 different inputs, text and price, this form is linked to another php page which i wish to look up how many inputs have been used, and then print them. Below is the code that is on the second php page.

$Used is on the form on the 1st page and the user enters how many fields they are going to use.

$Form_In / $Form_Pr - these are the inputs on the form, In is the text input, and Pr would be price.

Each Form_In / Form_Pr is set with a number after it, e.g. Form_In1, Form_In2, Form_Pr1, Form_Pr2, ect.

I hope someone can help, as with this for loop i am trying to get the number after the In/Pr to be printed automaticlly, so that i dont need to reprint all 20 fields when only 3 are used.

But this dont work, it will only print the number contain within, $i - not add the number to the $From_In - please what am i doing wrong?


Thanks Glenn Curtis.

Code: Select all

 
<form id="form1" name="form1" method="post" action="XX">
 
<?php
$Used = $Form_Used;
 
print ("Number entered in on used: $Used<br><br>");
 
for ($i=1; $i<=$Used; $i++) {
 print ("I is : $i<br><br>");   
print ("$Form_In$i");
}
 
?>
<br /><br /><input name="Submit" type="submit" value="Submit" />
</form>
 
 
 
SvanteH
Forum Commoner
Posts: 50
Joined: Wed Jul 08, 2009 12:25 am

Re: My Problem - For Loops / Forms

Post by SvanteH »

What doesn't work? When I do e.g

Code: Select all

<form id="form1" name="form1" method="post" action="XX">
 
<?php
$Used = 10;
$Form_In = "<input type=\"text\" name=";
 
print ("Number entered in on used: $Used<br><br>");
 
for ($i=1; $i<=$Used; $i++) {
 print ("I is : $i<br>");   
 echo ("$Form_In\"$i\">");
}
 
?>
<br /><br /><input name="Submit" type="submit" value="Submit" />
</form>
Then it works like a charm.
GlennCurtis2009
Forum Newbie
Posts: 11
Joined: Wed Jul 08, 2009 7:43 am

Re: My Problem - For Loops / Forms

Post by GlennCurtis2009 »

Ok, i have been working on it and can now sort of do what i need it to do, see code below.

But when $new is printed it will display Form_In(i) - what ever i is when its been counted, e.g. 1,2,3 ect.

But Form_In1 is a verabile in its own right, it should contain data from the frist php forms, which would have a name of Form_In1,2,3 ect all the way up to 20.

So how do i print the value of $Form_In while iam within this for loop - if i but another $ it just prints a dollar sign next to the name but not the content of that verabile


Code: Select all

 
 
$Used = $Form_Used;
 
print ("Number entered in on used: $Used<br><br>");
 
for ($i=1; $i<=$Used; $i++) {
$new = "Form_In$i";
 
print ("I is : $i<br><br>");    
 print ("$new");
 
}
 
 
 
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: My Problem - For Loops / Forms

Post by superdezign »

Why not use an array? Forms can have arrays.

Code: Select all

<form>
<input type="text" name="input[0]" />
<input type="text" name="input[1]" />
...
</form>

Code: Select all

echo '<form>';
for ($i = 0; $i < $max; $i++) {
    echo '<input type="text" name="input[' . $i . ']" />';
}
echo '</form>';
GlennCurtis2009 wrote:So how do i print the value of $Form_In while iam within this for loop - if i but another $ it just prints a dollar sign next to the name but not the content of that verabile
You have to use the $_POST array.

Code: Select all

echo $_POST['Form_In1'];
 
$i = 1;
echo $_POST['Form_In' . $i];
GlennCurtis2009
Forum Newbie
Posts: 11
Joined: Wed Jul 08, 2009 7:43 am

Re: My Problem - For Loops / Forms

Post by GlennCurtis2009 »

Thanks Mate!!!

You know what, i just did not think about arrays!!!!


Thanks Glenn.
Post Reply