0

Basically the code is supposed to be simple yet it is not working! First page:

<?php
$i = 1;
$var;
while($i != 10)
{
    $var="
    <form id='upload' action='test2.php' method='POST'>
        <input type='hidden' name='test' value='{$i}'>
        <input class='buttom' name='album' id='submit' value='Go to album' type='submit'>
    </div>  ";
    $i = $i+1;
    echo $var;
}
?>

Second page:

<?php
echo $_POST['test'];
?>

when I run the code I always get the last value only (9) ... I need to have different value for each button .. can you help me?

6
  • 2
    why are you making 10 forms? And the trick is <input name="test[]"> Commented May 24, 2017 at 21:57
  • Actually in my application I need 10 forms .. this is only a simplified example to my problem Commented May 24, 2017 at 21:58
  • Are you trying to make multiple forms or multiple elements? Commented May 24, 2017 at 21:59
  • 10 forms? or 10 input fields? not the same thing Commented May 24, 2017 at 21:59
  • @Kalkhouri you should create single form with multiple input elements name as array. Try my below solution Commented May 24, 2017 at 22:00

4 Answers 4

1

You don't need multiple forms or hidden inputs to achieve this. You can just use buttons, and set their values to $i.

echo "<form id='upload' action='test2.php' method='POST'>";
for ($i = 0; $i < 10; $i++) {
    echo "<button type='submit' name='test' value='$i'>Go to album</button>";
}
echo '</form>';

In test2.php, $_POST['test'] will have the $i value of the button you clicked.

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

Comments

1

Create single form with multiple input elements name as an array to get multiple value's in single input

Try this:

<input type='hidden' name='test[]' value='{$i}'>

Now you will receive an array of test as $_POST['test'] with different values

2 Comments

How can I identify the array index in the second page? it should be sent when the button is clicked
you will get multiple input element value as an array when button is clicked and form get submitted, as $_POST['test']
1

The problem with the other proposed solution is that you will have 10 forms, but you won't be able to submit all of the items at once. You will only be able to submit the value of one of them.

I believe you're trying to create 10 input elements instead of 10 separate forms:

<?php
$i = 1;
$var;

$var .= "<form id='upload' action='test2.php' method='POST'>"
while($i != 10)
{
    $var .= "<input type='hidden' name='test[]' value='{$i}'>"
    $i = $i+1;
}

$var .= "<input class='buttom' name='album' id='submit' value='Go to album' type='submit'>
</div>"

echo $var
?>

Here's code that I would suggest instead of what you've got:

<?php
    $html = "<form id='upload' action='test2.php' method='POST'>";
    for ($i = 1; $i <= 10; $i++){
        $html .= "<input type='hidden' name='test[]' value='{$i}'>";
    }

    $html .= "<input class='buttom' name='album' id='submit' value='Go to album' type='submit'>"
    $html .= "</form>";
    echo $html
?>

6 Comments

Where in the other proposed solution you need to create 10 forms?
I'm not creating 10 forms in the proposed solution. I'm creating 10 <input> Elements because you cannot submit more than one form at a time.
Exactly, no one can submit more than 1 form at a time? My question is do you think as per my answer you need to create 10 forms? just need to change input name to an array test[]
@MaheshSinghChouhan In your answer you would have created 10 forms each with one input element. The input element test would be an array, but the issue is that the array would only contain one element upon submission of the form since there was only one input element in there. For it to be valid you would need to have multiple <input name='test[]'> for there to be more than one value in the array.
first line is create single form, read it, btw he is already using loop to create input's than he will always get 10 input by changing single line
|
0

It's a little more work than what Mahesh's answer would require

First, a question: do you really want 10 forms - or do you want one form with 10 questions. Keep in mind that $_POST will only contain values from the form which was submitted (read: one form).

I think you want something like

<form id="upload" action="test2.php" method="POST">
    <?php for ($i = 0; $i < 10; $i++) { ?>
        <input name="test[]" type="hidden" value="<?=$i?>">
    <?php } ?>
    <button type="submit">submit</button>
</form>

edit: given your response below, why not use query parameters?

<a href="test2.php?page=<?=$i?>">Go to Album <?=$i?></a>

3 Comments

What good is an array field when it's the only input in the form?
My application shows something like a search result where each result is a form that contains different information (from a database) and a button. When the user clicks on the button it takes him to a new page and transfers the required information ... did you get my point?
Why not use something like query parameters, then? see my edit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.