Solved

return value from drop down

Posted on 2007-11-16
5
160 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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now