return value from drop down

Hi,

      I am using WAMP. I have a form page where I am populating a drop down list from a mysql table

import cgi, sys, re, os
from time import strftime
form = cgi.FieldStorage()

try:
      if len(form.keys())>0:
            artist = form.getvalue('artist')
            department = form.getvalue('department')

.
.
.
.
This works well if the field is a text field. Is it possible to get values from a drop down list. How.

Brgds,

kNish
primefocusAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
AlexConnect With a Mentor Software EngineerCommented:
Hi again my frined primefocus

So did i help you with all that i suggest you?
0
 
AlexSoftware EngineerCommented:
Hi primefocus,

You said that you populate a drop down list from a mysql table so i think that you had made a <select> with
<option> ... etc. </option>
</select>

you must gave a name to the select for example
<select name="test1">
<optio> mpla mpla ... </option>
</select>

So you ask how you can get the selected value of the drop down.You can simply do this with the $_POST command.

e.g.

echo $_POST['test1']; //you get the value of the drop down box with the name test1.

... but don't forget to have a form and to submit it so to be able to get the selected value.
0
 
primefocusAuthor Commented:
I have two files. One file (File A )has code to display the drop down menu. The other file (File B ) has code to read what has been entered. You are correct that $_POST['test1'] will hold the value of the selected item. However, If I am calling this from another file. In what manner will I get this value.

At present,

        artist = form.getvalue['artist'] gives the value for a text field entry. If this is replaced with a drop down list. How is it possible to get that selected value in another file.
Say, File B has code to display drop down. File A has code to get the value. How will it do it.?

BRgds,

kNish
File A :
==============
  <form action="http://renderunit-19/cgi-bin/submitDailies.cgi" method="get" enctype="multipart/form-data" >
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <table width="100%" border="0">
      <tr>
	  <td height="33">&nbsp;</td>
        <td width="14%" class="style3">Artist</td>
        
	  <?php
		$db_artist = mysql_connect("****", "*****", "")or die(mysql_error());
		mysql_select_db("daily",$db_artist)or die(mysql_error());
	  ?>
	  <?php
	  $options = mysql_query("SELECT artist_name FROM artist");
	  $options=mysql_fetch_array($options);
	  echo '<td>';
	   $test = enhanced_list_box_05(array(
		'table' => 'artist',
		'value_field' => 'artist_name',
		'highlight_id' => 1));
		echo "$test";
		function enhanced_list_box_05($options){
			$sql = "select " . $options['value_field'];
			$sql .= " from " . $options['table'];
			$result = mysql_query($sql)or die("error in SQL");
			echo '<select name="', $options['value_field'],'" size="1">';
			while ($row = mysql_fetch_array($result, MYSQL_NUM))
			{
				if($row[0] == $options['highlight_id']) {
					echo '<option value="' . $row[0] . '"SELECTED>' . $row[0] . '</option>';
					$sendData = $row[0];
				} else {
					echo '<option value="' . $row[0] . '">' . $row[0] . '</option>';
					echo $row[0];
				}
			}
			echo '</select>';
		}
	  echo '</td>';
	  mysql_close($db_artist);
	?>
      </tr>
    </table>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
  </form>
  <div id="footer">
    <p>&nbsp;</p>
  <!-- end #footer --></div>
<!-- end #container --></div>
</body>
</html>
 
 
File B :
==============================
 
.
.
....
 
import cgitb
cgitb.enable()
 
import cgi, sys, re, os
from time import strftime
form = cgi.FieldStorage()
artist	 = form.getvalue('artist')
department   = form.getvalue('department')
project      = form.getvalue('project')
seq          = form.getvalue('sequence')
shot         = form.getvalue('shot')
ver          = int(form.getvalue('version'))
filePath     = form.getvalue('filePath')
send2Preview = str(form.getvalue('send2Preview')) # its either on or off
comments     = form.getvalue('Comments')
datentime = strftime("%Y-%m-%d %H:%M:%S")
 
..
.
.
.
.
.

Open in new window

0
 
AlexSoftware EngineerCommented:
If i understand correctly, now, your problem is that you want to replace a text field with a drop down box and if you do this how you can get the selected value.I don't see any difference with the previous post and you have to use again the $_POST[''] or if you use a get method to your form to use the $_GET[''].

If you want to use a javascript method try this

-->  document.formname.selectboxname.value
0
 
primefocusAuthor Commented:
Hi,

          Thank you for your answer. I tried the following code. It worked. code is as attached below. I am using the code snippet as an include file and the code below to actually call the list. However, the line <option value="NONE">Select Location</option> shows "Select Location" as one option in the drop down list. Is it possible to eliminate it. In other words, the drop down list will purely display the table.

Brgds,

kNish


<p>Location:<br>
<select name="intLocation_id">
<option value="NONE">Select Location</option>
<?php drop_down("id", "artist_name","artist"); ?></p>
<?php
 
// Kim Briggs 2006-05.  http://kimbriggs.com/computers/
// Function completes a drop-down box
// by dynamically querying ID-Name pair from a lookup table
// 
// intID = Integer "ID" field of table, usually the primary key
// strName = Name field that user picks as a value, ordered by this field
// tableName = Name of MySQL table containing intID and strName
// strOrderField = Which field you want results sorted by
// strMethod = Sort as asc=ascending (default) or desc for descending
 
function drop_down($intID, $strName, $tableName) {
 
	$strQuery = "select $intID, $strName from $tableName";
	$rsrcResult = mysql_query($strQuery);
	
	while($arrayRow = mysql_fetch_assoc($rsrcResult)) {
		$intIdField = $arrayRow["$intID"];
		$strNameField = $arrayRow["$strName"];
		echo "<option value=\"$strNameField\">$strNameField</option>\n";
	}	
 
	echo "</select>\n\n";
}
 
?>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.