Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

return value from drop down

Posted on 2007-11-16
5
Medium Priority
?
167 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:Alex
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
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 earned 1500 total points
ID: 20303981
Hi again my frined primefocus

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 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