Pass Additional Variable in Option Selected form - php

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

Related

Multiple Forms, Wrong Value Submitted

I have PHP code creating multiple forms on a single page and the submission of any of these forms should trigger an API call based on which form was submitted.
if(isset($_POST['pickApples'])) {
echo '<h1>Picking Apple ' . $_POST['appleId'] . '</h1>';
// Function that calls API
pickApple($_POST['appleId']);
}
// $listOfApples is a list of 10 apples each with a unique ID
foreach ($listOfApples as $apple) {
echo '<form method="post" action="mypage.php">';
echo '<input type="hidden" name="appleId" value="' . $apple->{"id"} . '">';
echo '<button type="submit" class="my-button-class" value="click" name="pickApples">Pick Me</button>';
echo '</form>';
}
No matter which of the form buttons I click, the value in $_POST['appleId'] is the ID of the first apple in the list. I don't have much experience with PHP or HTML forms, is my approach completely off?
I am seeing some small mistakes as
echo '<input type="hidden" name="appleId" value"' . $apple->{"id"} . '">';
You should fix it,instead of foreach yor can use for loop to display forms,
as I haven't $listofapples,I used 10,you can query for row numbers than place row numbers value as $listofapples,then echo your unique id to input value,
Finally it's working as you wished
Check it & let me know if it's the perfect fit for you
<?php
if(isset($_POST['pickApples'])) {
echo '<h1>Picking Apple ' . $_POST['appleId'] . '</h1>';
// Function that calls API
pickApple($_POST['appleId']);
}
$listOfApples = 10;
// $listOfApples is a list of 10 apples each with a unique ID
$sn=0;
for ($i = 1; $i <= $listOfApples; $i++) {
echo '<form method="post" action="hy.php">';
echo '<input type="hidden" name="appleId" value=" ' .++$sn. '"/>';
echo '<button type="submit" class="my-button-class" value="click" name="pickApples">Pick Me</button>';
echo '</form>';
}
?>

php form saves only the last input field value

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]

how to get the value of the checked row

I need to get the value of the selected option if the checkbox is checked. For example, I checked the checkbox in the third row. In another page I want to get the value of the third row selected option. Please don't use the $a to get the data.
<form method = "post" action = "">
<table>
<?php
for($a = 0;$a <=3; $a++){
echo '<tr><td>';
echo '<input type = "checkbox" name = "chckbx">';
echo '</td><td>';
echo '<select name = "slct1">';
echo '<option>';
echo $a;
echo '</option>';
echo '</select>';
echo '</td></tr>';
}
?>
</table>

How to pass 2 radio selection value with a single Submit??

echo '<form method="post" action="mod_slots.php">';
$i = 1;
while(cond)
{
echo '<input type="radio" name="change1" value="'.$i.'" />';
$i++;
}
$j = 1;
while(cond)
{
echo '<input type="radio" name="change1" value="'.$j.'" />';
$j++;
}
}
echo '<input type="submit" value="Change Selected" />';
echo '</form>';
The above code passes only change1 value to the next page! But I want a single submit button, which will send both value!!??
My implementation may be wrong!
Radios with same name could only have one selection, if you want to divide them, just use different name.
Either you can pass them by giving their names as array like
echo '<input type="radio" name="change[]" value="'.$i.'" />';
and after submit you can print them like
print_r($_POST['change']);

PHP $_POST from dynamically generated form

I am creating a form based on a user selection of $node_number ... so the form looks like:
function createForm($node_number) {
echo "<form id=\"form\" name=\"form\" action=\"molecule_display.php\" method=\"post\">";
for ($n = 1; $n <= $node_number; $n++) {
echo "<fieldset class=\"step\">
<input id=\"node_title" . $n . "\" name=\"node_title" . $n . "\" />
<input id=\"node_comment" . $n . "\" name=\"node_comment" . $n . "\" type=\"textarea\" />
</fieldset>";
}
echo "<input type=\"hidden\" name=\"node_number\" value=\"" . $node_number . "\">
<button id=\"submit_node\" type=\"submit\">Submit</button>"
echo "</form>";
}
Which will create $node_number of versions of that form element. My question is how to dynamically name the form elements to be able to manage them easier when I am processing them. The way I'm doing it right now, by adding the $n iterator to the name attribute is not ideal I think.
I understand that I can declare the name="" attribute as an array like name[]="" ... in terms of giving each sub-element of the larger form a unique name.
I'm guessing I want a multi-dimensional array of the individual form segment ... just not sure how to best handle those within a form and within the $_POST variable.
Does anyone have any suggestions?
I think you can do it this way:
function createForm($node_number) {
echo '<form id="form" name="form" action="molecule_display.php" method="post">';
for ($n = 1; $n <= $node_number; $n++) {
echo '<fieldset class="step">
<input id="node_title'.$n.'" name="nodes['.$n.'][node_title]" />
<input id="node_comment'.$n.'" name="nodes['.$n.'][node_comment]" type="textarea" />
<button id="submit_node" type="submit">Submit</button></p>
</fieldset>';
}
echo '</form>';
}
And then get $_POST['nodes'] which will be multidimensional array, which you can iterate with foreach. You will get $_POST['nodes'][1] = array('node_title'=>... , 'node_comment'=>...); and so on.
If you use the array like you were saying in your post you should be able to access them pretty easily.
function createForm($node_number) {
echo "<form id=\"form\" name=\"form\" action=\"molecule_display.php\" method=\"post\">";
for ($n = 1; $n <= $node_number; $n++) {
echo "<fieldset class=\"step\">
<input id=\"node_title_" . $n . "\" name=\"node_title[" . $n . "]\" />
<input id=\"node_comment_" . $n . "\" name=\"node_comment[" . $n . "]\" type=\"textarea\" />
<button name=\"submit_node[" . $n . "]\" type=\"submit\">Submit</button></p>
</fieldset>";
}
echo "</form>";
}
I also changed the submit_node to a name and gave it an array value because an ID must be unique, which will cause errors if you are referencing it somewhere.
You could loop through the results like this:
foreach ($_POST['node_title'] as $key => $response) {
$title = $response;
$comment = (!empty($_POST['node_comment'][$key])) ? $_POST['node_comment'][$key] : "";
// Save title / comment here.
}
Since every form has its own submit button, nothing stops you from using name="node_title" in all of them. If you now add <input type="hidden" name="index" value="$n"> and read this first, your logic becomes very easy.

Categories