1

Maybe this is a stupid question, but I was wondering if I could get information passed with a form into a for-loop.

The problem is as follows: The passing of the information works, though he passes only for the last loop. For example if I click the submit button for loop 2 ($i = 2). The command $_POST['titel'] will only remember the information in last loop ($i = $numact-1) and not loop 2 ($i = 2).

for example if titel[0] = test0, titel[1] = test1, titel[2] = test2. and I click the submit button below titel[0] he passes the information from titel[2]. Is there an easy way to get around this?

I have the following code (For the sake of simplicity I shortened it);

<?php 
for ($i = 0; $i <= $numact-1; $i++) {

    echo "<tr><td width='150'>
        <input type='text' name='titel' value='$titel[$i]' />
        </td></tr>
        <tr><td><input type='submit' name='submitreg' value='Toon activiteit'/>
        </td></tr>";                        
}
?>
Vahid Hallaji
  • 7,159
  • 5
  • 42
  • 51
Mark
  • 13
  • 2

3 Answers3

0

You're using the same parameter name over and over again (name=titel - you had a type and probably meant to write as 'title') so when the form submits - only one value will be passed.

You can easily fix that by passing the parameters as:

...
echo "<tr><td width='150'>
<input type='text' name='titel$i' value='$titel[$i]' />
</td></tr>
<tr><td><input type='submit' name='submitreg' value='Toon activiteit'/>
</td></tr>"; 
...

and reading it on the other side as title0, title1 etc.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • Hey, thank you guys for the quick responses. That is indeed my problem that I had the same name for all the loops. The only problem that remains is that I do not know, on which loop button they pressed. so for example if they pressed loop button 0 they get the title test0 and if they pressed loop button 2 they get test2! – Mark Sep 10 '13 at 17:03
  • Hehe yes, I know that this is not a neat way to do it, that is why I posted this question! I was wondering for a solution, or maybe a suggestion for something else. – Mark Sep 10 '13 at 17:16
  • @Mark in that case, you'll have to create different forms for each parameter, you'll want to POST only that one parameter when you submit the form - otherwise clicking on each one of the submit buttons will post ALL the parameters... – Nir Alfasi Sep 10 '13 at 17:17
  • @Mark another (better) way to implement it will be by using a checkbox/radio button. This way you can submit ALL the parameters and on the other side look only for the parameter that is `checked`/`selected` – Nir Alfasi Sep 10 '13 at 17:19
  • That ought to do it, Thank you very much! I doubt that this is the most efficient way to do this. But there is not a fixed number of activities in the database that has to be made into a new page. that is why I choose for a for loop with a maximum of the number of found rows. – Mark Sep 10 '13 at 17:23
  • In case u wondered, this is the quite simple but somewhat effective code I used to check for which value is used! for ($i = 0; $i <= $numact-1; $i++) { $titels[$i] = stripslashes(strip_tags(@$_POST[$i])); $sub[$i] = stripslashes(strip_tags(@$_POST[$i])); if($sub[$i]){ echo $titels[$i];}else{} } – Mark Sep 10 '13 at 17:29
  • In addition, I would suggest to remove the "suppress errors" `@` from your code, in general it's a bad practice and might cost you in missing relevant errors/warnings. – Nir Alfasi Sep 10 '13 at 17:31
0

As seen in this post, you can pass in an array simply by putting brackets around the index. PHP will reconstruct the array for you.

Also, you probably want your submit at the end of your table, not after each row. Your code should look something like this:

<?php 
for ($i = 0; $i <= $numact-1; $i++) {
    echo "<tr><td width='150'>
    <input type='text' name='titel['.$i.']' value='$titel[$i]' />
    </td></tr><tr><td><input type='submit' name='submitreg' value='Toon activiteit'/>
</td></tr>";
}
?>

The advantage of this approach is that it's more extensible, should you choose to use different values. However, Edson Junior's approach is slightly simpler.

Community
  • 1
  • 1
Steven Liao
  • 3,577
  • 3
  • 19
  • 25
  • 1
    Hey, no it is important that each activity has is own submit button, so you can press on the show activity button below the activity. (It is a list of 20 activity's below each other) – Mark Sep 10 '13 at 17:09
0

You can change the name attribute to name=titel[] so PHP will automatically transform it to an array. It is not necessary to specify the index inside title[].

For example:

<input type="text" name="titel[]" value="x" />
<input type="text" name="titel[]" value="y" />
<input type="text" name="titel[]" value="z" />

When you submit the form, PHP will transform it to

"title" = array (
    0 => "x",
    1 => "y",
    2 => "z"
);

In case you need to check what button was clicked, you could change the inputs type="submit" to <input type="button" name="submitreg" class="submitreg" id="whatever_you_want_but_has_to_be_unique" />. Outside the loop, add another input wich will have the value of what button was pressed.

<input type="hidden" id="buttonPressed" name="buttonPressed" value="" />

Then add this code (you need to import jQuery library to the page so it understands the code below, wich is jQuery, here is the Docs.

<script type="text/javascript">
$(function(){
    $("button.submitreg").live("click", function(){
        var buttonPressed = $(this).attr("id");
        ("input#buttonPressed").val(buttonPressed);
    });
}
</script>

All you need to do is get the buttonPressed value and you'll know what button was pressed.

Edson Horacio Junior
  • 3,033
  • 2
  • 29
  • 50
  • Yeah, thank you, that worked for me, like earlier suggestions said. But how do I know for example if they pushed on the button for value 0 => x or 2 => z. So is there a way to get a _post[$variable] which declares the loop button they pressed on? – Mark Sep 10 '13 at 17:11