-3

Possible Duplicate:
How to loop through dynamic form inputs and insert into an array

I have a php script and a form. The php script makes an xml file but what i need is for someone to enter a number and that would set that amount of textboxes that would be for someone to write data for that xml file.
So i need it to write <input type="text" name="a #"> however many times the user enters. Also the name needs to be a number but it counts by one ex:<input type="text" name="1"> <input type="text" name="2">... Thanks

2
  • 1
    That's a basic application of a for loop. What have you tried, what are you struggling with? Commented Dec 28, 2011 at 2:32
  • Note the "a #" isn't necessary for processing the input. Commented Dec 28, 2011 at 2:50

3 Answers 3

1
<?php
session_start();
if(isset($_POST['quantity']){
// code here to check isnum and min/max
    $count = $_POST['quantity'];
    for ($i=1; $i<=$count; $i++){
        @$s.= "<input type=text name=".$i."><br>";
    }
?>

now just echo out $s in your html

Sign up to request clarification or add additional context in comments.

3 Comments

Why are you suppressing errors on @$s?
Thanks so much. Sorry not to good with php yet
i'm doing this: <?php // code here to check isnum and min/max $count = $_POST['quantity']; for ($i=1; $i<=$count; $i++){ @$s.= "<input type=text name=".$i."><br>"; } echo $s; ?> and nothing is happening even though im givig $_GET['quantity'] 40
1

This?

<form method="get" action="">
    <div><input type="text" name="num_inputs" value="1" placeholder="Number of inputs"/></div>
</form>

<?php $num_inputs = isset($_GET['num_inputs']) ? $_GET['num_inputs'] : 1; ?>

<form method="post" action="">
    <?php for ($i = 0; $i < $num_inputs; $i++) : ?>
        <div><input type="text" name="inputs[]"/></div>
    <?php endfor ?>
</form>

Edit: yes, an array is much better than input_x. Updated my answer.

3 Comments

Well no, I don't. Template example code written in 30 seconds doesn't necessarily represent ones normal coding standards. You can check code.google.com/p/sleek-php/ if you want to know how I normally write.
I don't check anything but what I see here before me. "30 seconds" matters how? You posted this 34 minutes ago (as I comment). Post how you "normally" do so. If you take shortcuts to make your post seem more immediate and timely, at least edit it to reflect your good sense; you have about five minutes or so following a post before it's "recorded".
Absolutely! Formatting for readability matters quite a bit on a site where pretty much the whole point is, um, readability by humans. ;)
0

I think what you want is an array of form fields.

You want something like this:

<?php
$number_of_textboxes = 5; // you'd get this from a $_GET parameter

echo str_repeat('<input type="text" name="mybox[]" />', $number_of_textboxes);

?>

This will print five text boxes:

<input type="text" name="mybox[]" />

Then, when you reference these boxes' values, you do so like thus:

<?php
    foreach ($_POST['mybox'] as $i) {
       echo $i;
    }

?>

That is, by using "mybox[]" as the name of each input field, you create an array of textboxes, which you can then iterate through.

4 Comments

I wonder if a template technique would be more advantageous than mixing markup within PHP?
i hate templating. Sorry but not used to it yet.
@evan.stoddard - This is a perfect "templating" use case. It's more modular than inline code injection. In the long run, while more cumbersome to get used to, it's more practical in practice, and something you need to learn and get used to. Don't wait until you have to.
Ik but right now i need to get this out asap

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.