Solved

return value from drop down

Posted on 2007-11-16
5
163 Views
Last Modified: 2013-12-25
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
0
Comment
Question by:primefocus
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:alex_code
ID: 20297524
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
 

Author Comment

by:primefocus
ID: 20297692
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
 
LVL 9

Expert Comment

by:alex_code
ID: 20299808
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
 

Author Comment

by:primefocus
ID: 20303460
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
 
LVL 9

Accepted Solution

by:
alex_code earned 500 total points
ID: 20303981
Hi again my frined primefocus

So did i help you with all that i suggest you?
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question