Laonda Posted August 9, 2009 Share Posted August 9, 2009 I'm making a form, using the values from a database to display a select element (drop down menu). But I can't seem to get the value when i want to insert into another db table. The code for the form: <form action="<?php echo $PHP_SELF; ?>" method="post"> <table> <tbody> <tr> <td>Kunde</td> <td> <?php $sql = "SELECT name FROM kontakt"; $query = mysql_query ( $sql ); $rowCheck = mysql_num_rows ( $query ); echo "<select>"; for($i = 0; $i < $rowCheck; $i ++) { $resultRow = mysql_fetch_array ( $query ); echo "<option>" . $resultRow ['name'] . "</option>"; } echo "</select>"; ?> I also got some plain HTML elements that works just fine. The code i use to insert it into the database: if (isset ( $_POST ['unitSubmit'] )) { $sqlInsert = "INSERT INTO terminal VALUES ('" . $_POST ['terminalID'] . "','" . $_POST ['bax'] . "','" . $_POST['terminal'] . "')"; $sqlQuery = mysql_query ( $sqlInsert ) or die ( error ); } Can anyone please help me so i can insert the value i get from the select (drop down) and into the database? Quote Link to comment https://forums.phpfreaks.com/topic/169517-solved-how-to-get-select-value-from-for-loop/ Share on other sites More sharing options...
wildteen88 Posted August 9, 2009 Share Posted August 9, 2009 Give your drop down menu a name echo "<select name=\"your_drop_down_menu_name\">"; In order for the selected value to be sent you'll need to change this echo "<option>" . $resultRow ['name'] . "</option>"; To echo "<option value=\"" . $resultRow ['name'] . "\">" . $resultRow ['name'] . "</option>"; Now you'd use $_POST['your_drop_down_menu_name'] to get the selected value. Quote Link to comment https://forums.phpfreaks.com/topic/169517-solved-how-to-get-select-value-from-for-loop/#findComment-894401 Share on other sites More sharing options...
MadnessRed Posted August 9, 2009 Share Posted August 9, 2009 terminalID, tarminal and bax. Where are you getting that data from? Those all need to be in the html somewhere. If you are doing 3 variables then you have 3 options. Firstly you could use a select which uses javascript to populate 3 other cells. Though i think it would bedone easier in php with explode/implode. eg <form action="<?php echo $PHP_SELF; ?>" method="post"> <table> <tbody> <tr> <td>Kunde</td> <td> <?php $sql = "SELECT name FROM kontakt"; $query = mysql_query ( $sql ); $rowCheck = mysql_num_rows ( $query ); echo "<select name=\"unitSubmit\">"; for($i = 0; $i < $rowCheck; $i ++) { $resultRow = mysql_fetch_array ( $query ); //Now whereever terminal,terminalID and bax should be done here $result = ; echo "<option value=\"".$terminal.",".$terminalID.",".$bax."\">" . $resultRow ['name'] . "</option>"; } echo "</select>"; ?> Then if (isset ( $_POST ['unitSubmit'] )) { $data = explode(",",$_POST ['unitSubmit'],3); //foreach($data as $k => $v){ $data[$k] = mysql_real_escape_string($v); } $sqlInsert = "INSERT INTO terminal VALUES ('" . $data[0] . "','" . $data[1] . "','" . $data[2] . "')"; $sqlQuery = mysql_query ( $sqlInsert ) or die ( error ); } The comment is a quick code to protect against sql injection, I would recommend uncommenting. also could I sugest, slightly off topic but rather than $rowCheck = mysql_num_rows ( $query ); echo "<select>"; for($i = 0; $i < $rowCheck; $i ++) { $resultRow = mysql_fetch_array ( $query ); you do echo "<select>"; while($resultRow = mysql_fetch_array ( $query )) { Quote Link to comment https://forums.phpfreaks.com/topic/169517-solved-how-to-get-select-value-from-for-loop/#findComment-894415 Share on other sites More sharing options...
Laonda Posted August 9, 2009 Author Share Posted August 9, 2009 Worked like a charm! Thanks for your help:) Give your drop down menu a name echo "<select name=\"your_drop_down_menu_name\">"; In order for the selected value to be sent you'll need to change this echo "<option>" . $resultRow ['name'] . "</option>"; To echo "<option value=\"" . $resultRow ['name'] . "\">" . $resultRow ['name'] . "</option>"; Now you'd use $_POST['your_drop_down_menu_name'] to get the selected value. Quote Link to comment https://forums.phpfreaks.com/topic/169517-solved-how-to-get-select-value-from-for-loop/#findComment-894416 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.