John Davidson

html - PHP how to send and receive multiple choice box

0 comments
Message:


I need suggestion.
I have an list of position from database, displayed in table and in the last table row is an option to select "TAK" - YES and "NIE" - NO, please tell me is it possible and if it, how to send and receive it all selected to "TAK" fields at file wypozyczalnia.php


 <form action='wypozyczalnia.php' method='post'>
<table width="1000px" align="center" border="1">
<tr>
<th width="250px">Tytuł</th>
<th width="250px">Autor</th>
<th width="300px">Opis</th>
<th width="200px">Dostępność</th>

</tr>
<?php
//sprawdzanie czy taka zmienna jest ustawiona w sesji, jest to dla nowo wchodzących na stronę
if(isset($_SESSION['blad'])) echo $_SESSION['blad'];
//wyświetlanie wyników gdy zmienna ile zwróciła przynajmniej 1 rząd
if ($ile>=1)
{
//pętle wyświetlająca wszystkie zwrócone z zapytania wpisy
for ($i = 1; $i <= $ile; $i++)
{
//pobranie rzędu jako tablicę asocjacyjną
$row = mysqli_fetch_assoc($rezultat);
//przypisanie każdej kolumny do odpowiedniej zmiennej
$title = $row['title'];
$author = $row['author'];
$description = $row['description'];
$available = $row['available'];

echo
"<tr>
<td> ".$title." </td>
<td> ".$author." </td>
<td> ".$description." </td>
<td>";
if($available==1)
{
echo "TAK";
}
else
{
echo "NIE";
}
"</td>
</tr>";
if(isset($_SESSION['zalogowany']) && ($_SESSION['zalogowany']==true) && $available==1)
{
echo
"<select name='idksiazki[]' >
<option name='NIE'>NIE</option>
<option name='TAK'>TAK</option>
</select>";
}
}
}?>
</table>
<?php
if(isset($_SESSION['zalogowany']) && ($_SESSION['zalogowany']==true))
{
echo "<br /> <input type='submit' name='zapis' value='wypożyczam'></form>";
}
?>
</form>


My Answer:
To send and receive the selected options to the "TAK" fields in the wypozyczalnia.php file, you can modify your form and PHP code as follows:

1. Update the select element to include a hidden input field to store the corresponding book ID:

html

'>


2. Modify the PHP code to handle the form submission and retrieve the selected options:

php
if(isset($_POST['zapis'])) {
$selectedOptions = $_POST['idksiazki'];
$bookIds = $_POST['book_ids'];

// Loop through the selected options and corresponding book IDs
for($i = 0; $i < count($selectedOptions); $i++) {
$selectedOption = $selectedOptions[$i];
$bookId = $bookIds[$i];

// Perform any necessary actions with the selected option and book ID
// For example, update the database with the selected option for the corresponding book ID
}
}


By making these changes, you can send and receive the selected options along with the corresponding book IDs to the wypozyczalnia.php file when the form is submitted. You can then process this data as needed in your PHP code.

Rate this post

5 of 5 based on 9629 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us