I'm trying to have a number of forms with one field each and make the input enter in to the same array.
This is my code:
<?php
$parts = array();
for($i = 0; $i < "10"; $i++)
{
echo '<form action="index.php" method="post">';
echo '<input type="text" name="parts[]"><br>';
echo '<input type="submit">';
echo '</form>';
$parts[$i] = $_POST['holder'];
unset($_POST['holder']);
}
$arrlength = count($parts);
for($i = 0; $i < $arrlength; $i++) {
echo $parts[$i];
echo "<br>";
}
?>
As of right now the number I choose at random was 10 it's supposed to be any given number by the user, but this is just for test purposes.
The problem I'm having is that it only posts the last part, I've tried a bunch of different ways but none have been successful so far.
It sounds like you want to submit a form with multiple entries in an array?
You would need to do it something like:
echo '<form action="index.php" method="post">';
for($i = 0; $i < "10"; $i++)
{
echo '<input type="text" name="parts['.$x.']"><br>';
}
echo '<input type="submit">';
echo '</form>';
Then in the code that you are posting to
var_dump($_POST['parts']);
Related
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?
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.
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
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
?>
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?
Go to Album <?=$i?>
I have a simple table "Exams" with the columns id, title and a form with a variable amount of exam input fields.
But when one submit the form the last value will be saved triple times.
I suppose it's because of the $sql_insert statement with the same value.
How can i change the code that the different values are submitted in that $sql_insert statement?
echo '<form action="" method="post">';
for ($i = 1; $i <= $student['passed_exams']; ++$i) {
echo '<label>Exams '.$i.' :</label>';
echo '<input type="text" id="id['.$i.']" name="title" placeholder="passed Exam" />';
echo '<br />';
}
echo '<input type="submit" value=" Submit " name="submit" /></form>';
if (isset($_POST['submit'])) {
for ($i = 0; $i < $student['passed_exams']; ++$i) {
$sql_insert = "INSERT INTO exams (title) VALUES ('".$_POST['title']."')";
$dbConnection->query($sql_insert);
}
$dbConnection->close();
}
Php needs name as an array
echo '<form action="" method="post">';
for ($i = 1; $i <= $student['passed_exams']; ++$i) {
echo '<label>Exams '.$i.' :</label>';
echo '<input type="text" id="id['.$i.']" name="title['.$i.']" placeholder="passed Exam" />';
echo '<br />';
}
echo '<input type="submit" value=" Submit " name="submit" /></form>';
if (isset($_POST['submit'])) {
for ($i = 0; $i < $student['passed_exams']; ++$i) {
$sql_insert = "INSERT INTO exams (title) VALUES ('".$_POST['title'][$i]."')";
$dbConnection->query($sql_insert);
}
$dbConnection->close();
}
You need to make the title array first as
name="title['.$id.']"
Then you have to save it as
$_POST['title'][$id]
I have this php code:
for($i=0;$i<3;$i++) {
echo 'Question'.$i.'</br>';
echo 'Answer..</br>';
echo '<form action="" method="POST">';
echo '<textarea name="answer"></textarea>';
echo '</br><button name="answer_button'.$i.'"><b>Answer</b></button>';
echo '</form>';
}
Now I want to get the question number for which the answer_button is clicked.
Closest I could get was this:
for($i=0;$i<3;$i++) {
echo 'Question'.$i.'</br>';
echo 'Answer..</br>';
echo '<form action="" method="POST">';
echo '<textarea name="answer"></textarea>';
echo '</br><button name="answer_button"><b>Answer</b></button>';
echo '</form>';
if(isset($_POST['answer_button'])) {
echo $i;
break;
}
}
This gives me Question number but it will not print other questions in loop once the button is clicked.
Is there no solution without using javaScript?
This should work :
for($i=0;$i<3;$i++) {
echo 'Question'.$i.'</br>';
echo 'Answer..</br>';
echo '<form action="" method="POST">';
echo '<textarea name="answer"></textarea>';
echo '</br><button name="answer_button'.$i.'"><b>Answer</b></button>';
echo '</form>';
echo '</div>';
}
for($i=0;$i<3;$i++) {
if(isset($_POST["answer_button".$i])) {
echo $i;
}
}
Hope it helps.
Change the html markup to use an "array notation":
echo '<button name="answer_button['.$i.']" type="submit">Answer</button>';
That will cause php to populate an array when receiving back the form which you can examine:
<?php
// ...
if(isset($_POST['answer_button']) && is_array($_POST['answer_button'])) {
$id = array_shift(array_keys($_POST['answer_button']));
// ...
}
This allows to have multiple such buttons in a single form and detect which one has actually been clicked. It works, because $_POST will contain an array with a single element with key as in $id, which you can easily examine yourself with a var_dump($_POST); or similar.
I think this is what your looking for.
CODE
<?php
echo '<form action="" method="POST">';
for ($i = 0; $i < 3; $i++) {
echo 'Question: ' . $i . '<br>';
echo 'Answer..<br>';
echo "<textarea name='answer[$i]'> </textarea></br>";
echo "</br><button name='answer_button[$i]' value='BtnPushed'> <b>Answer</b></button><br>";
if (! empty($_POST['answer_button'][$i])) echo "Last Answer: {$_POST['answer'][$i]}<br>";
echo '<hr>';
}
echo '</form>';
Results
I have created a for loop that generates a form div repeatedly:
for ($i = 1; $i <= $noOfTanks; $i++) {
echo '<div class="';
echo $col;
echo '"><img id="tank" src="img/tank.svg" alt="Tank"></br>
<label for="tankName';
echo $i;
echo '">Tank Name ';
echo $i;
echo '</label>
<input type="text" id="tankName';
echo $i;
echo '" name="tankName';
echo $i;
echo '"></br>
<label class="rightT" for="tankVolume';
echo $i;
echo '">Tank Volume ';
echo $i;
echo '</label>
<input class="rightT" type="text" id="tankVolume';
echo $i;
echo '" name="tankVolume';
echo $i;
echo '"><p>L</p>
</div>';}
The code generated a number of inputs. I want to gather all this input values into two arrays tankName[], tankVolume[]. I am struggling to understand how to use $_POST to do this.
Thanks for any help.
Use array of inputs.
Example:
<input class="rightT" type="text" id="tankVolume1" name="tankVolume[]"/>
I have a loop that creates options in a drop down form.
How can I pass the variable $objectID[$i] from the loop where $i consistent with the selected value $i
echo '<form action="#" method="post"><select name="Restaurant">';
for ($i = 0; $i < count($restaurants); $i++){
$name[$i] = $restaurants[$i]->get("Name");
$city[$i] = $restaurants[$i]->get("City");
$objectID[$i] = $restaurants[$i]->getObjectID();
//echo '<input type="hidden" name="passRestaurant" value="' . $name[$i]. '" />'; // tried this, but it just messes up the format of the drop down
echo "<option>{$name[$i]} -" . " {$city[$i]}</option>";
}
echo '</select><br><br><input type="submit" name="submit" value="Next" />
</form>';
}
This prints the value that was selected, but I also want to print the $objectID[$i] at the same $i value:
if(isset($_POST['submit'])){
$selected_val = $_POST['Restaurant']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
}
why you just put $objectID[$i] in
echo "<option value=\"{$objectID[$i]}\">{$name[$i]} - {$city[$i]}</option>";
so you can get all the properties of Restaurant with $objectID