Get value from multiple Select form, created dinamically

mluci12
Posts: 39

Get value from multiple Select form, created dinamically

Code: Select all

<?php require('layout.php'); $servername = ""; $username = ""; $password = ""; $dbname = ""; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo '<form action="" method="post">'; $q=$conn->query("SELECT DISTINCT id,categorie FROM categorii ORDER BY categorie"); foreach($q as $cat){ echo '<a href="'.$cat['categorie'].'">'.$cat['categorie'].'</a>'; echo '<ul class="sub-menu">'; $linkq=$conn->query("SELECT intrebare,tip1,tip2,tip3 FROM intrebari WHERE categorie='" . $cat['id'] . "'"); foreach($linkq as $link){ echo '<input type="checkbox" name="tv[]" value="'.$link['intrebare'].'"> '.$link['intrebare'].' <br> <select name="'.$link['intrebare'].'"> <option value="'.$link['tip1'].'">'.$link['tip1'].'</option> <option value="'.$link['tip2'].'">'.$link['tip2'].'</option> <option value="'.$link['tip3'].'">'.$link['tip3'].'</option> </select><br>'; } echo '</ul></li>'; } echo ' <input type="submit" name="Submit" value="Submit"> </form>'; ?> <?php $sDbHost = ''; $sDbName = ''; $sDbUser = ''; $sDbPwd = ''; $dbConn = mysql_connect ($sDbHost, $sDbUser, $sDbPwd) or die ('MySQL connect failed. ' . mysql_error()); mysql_select_db($sDbName,$dbConn) or die('Cannot select database. ' . mysql_error()); session_start(); $checkbox1 = $_POST['tv']; if($_POST["Submit"]=="Submit") { for ($i=0; $i<sizeof($checkbox1);$i++) { $query2="INSERT INTO chestionar (intrebari) VALUES ('".$checkbox1[$i]."')"; mysql_query($query2) or die ('Error updating database'); echo "Record is inserted."; } } ?>
and the page is madustech.com/chestionare
I create a select option for all checkbox generated from database.How can i get value from select option and insert in database?

Admin
Hi,
Try put a fixed name to that <select>, with "[]" to can have multiple values from the same name into an array.

Code: Select all

<select name="intrb[]">
Then, where you want to get the selected options and to perform Insert, use like this:

Code: Select all

if(isset($_POST['intrb']) && count($_POST['intrb'])>0){ $sql="INSERT INTO table_name (col_name) VALUES ('". implode("'),('", $_POST['intrb']) ."')"; //Here execute the $sql with the php extension query you use, PDO or MySQLi. }

mluci12
Hello!
i want to save in database name of question.
The database :

Code: Select all

Id---answer----question
În anwer i want to save selected value from database, and in question the row name selected from database

Admin
Try this in your code from first post:

Code: Select all

if(isset($_POST['tv']) && count($_POST['tv'])>0){ $nri = count($_POST['tv']); for($i=0; $i<$nri;$i++){ $question = $_POST['tv'][$i]; $answer = isset($_POST[$question]) ? $_POST[$question] :''; $sql="INSERT INTO table_name (answer, question) VALUES ('". $answer ."', '". $question ."')"; //Here execute the $sql with the php extension query you use, PDO or MySQLi. } }

mluci12
What name is necessary to put at the select option ?

Admin
At the <select> element let the name as you set it in your code ($link['intrebare']).
Then, according to your code that creates the checkbox and <select> elements; this code reprsents the checkbox value which is also the name of the select associated to that checkbox.

Code: Select all

$question = $_POST['tv'][$i];
And this code gets the selected option.

Code: Select all

$answer = isset($_POST[$question]) ? $_POST[$question] :'';
- Or, better set the name of select like this:

Code: Select all

<select name="intrb[]">
And change define $answer with this code:

Code: Select all

$answer = isset($_POST['intrb'][$i]) ? $_POST['intrb'][$i] :'';

mluci12
Thank you very very much!!!!!!

Similar Topics