Jump to content

[SOLVED] How to get select value from for loop


Laonda

Recommended Posts

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?

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.

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 )) {

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.