Solved

php dropdown box value

Posted on 2010-08-18
13
679 Views
Last Modified: 2013-12-13
hi

I have a php form with two text boxes and a dropdown box and I want to pass the values from the text boxes and the selected value from the dropdown box. Have been able to pass the text box values but am unable to pass the selected value from the dropdown box. How do I do it, please?
0
Comment
Question by:Paul-Brooks
  • 4
  • 3
  • 3
  • +1
13 Comments
 
LVL 9

Expert Comment

by:Snarfles
ID: 33463680
Could you post your code please and I'll take a look.

Cheers
0
 

Author Comment

by:Paul-Brooks
ID: 33463777
<?php if (isset($reg_error)) { ?>
There was an error: <?php echo $reg_error; ?>, please try again.
<?php } ?>


<?php
// Include init file
include 'init.php';
?>

<form action="register.php" method="post">

<b>Username:</b> <input type="text" size="20" maxlength="20" name="username"

<?php if (isset($_POST['username'])) { ?> value="<?php echo $_POST['username']; ?>" <?php } ?>/><br />

<b>Password:</b> <input type="password" size="20" maxlength="10" name="userpassword" /><br />

<b>Confirm Password:</b> <input type="password" size="20" maxlength="10" name="confirmpass" /><br />

<p>Authority Level:<br /><!-- Drop-down Box queries A_TABLE_NAME table in A_DB_NAME -->
<?php
/*
// <?php dropdown(location_id, location_name, location, location_name, location_name1); ?></p>
*/
?>

<? php
/*

This is the function: function dropdown($intIdField, $strNameField, $strTableName, $strOrderField, $strNameOrdinal, $strMethod="asc")

This is the query   $strQuery = "select $intIdField, $strNameField from $strTableName order by $strOrderField $strMethod";

*/
?>


<?php dropdown(auth_id, auth_desc, auth_tbl, auth_id, authority); ?></p>

<?php
/*

// tried these two to pass the Auth but failed
select name="auth";
$auth = $_POST["auth"];
*/

/*
select name ="authority";

*/


$auth = $_GET["authority"];

echo $auth;


?>

The form opens and the drop down ox populates OK and a value can be selected. The function sits in another script but the details shown should be enough, I hope.

Thanks
0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33464411
Hi Paul

When you post code if you use the 'code' attachment/box at the bottom it will post it in a more easily readable format.

the <b> tag is an old tag, to be compliant you should change all these to <strong></strong> tags.

You need to make sure that you are referencing the variables correctly with the dollar sign ($).

<?php dropdown(auth_id, auth_desc, auth_tbl, auth_id, authority); ?></p>

It's actually quite hard to work out whats going on with snippets of code. Are you able to paste the entire file or to provide a live url of the page?

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 33464516
Please look this over - it teaches how to use PHP to process the contents of an HTML form that contains a "select" (also called a dropdown).

You might find this book useful - it will help you get some foundation in the use of PHP.  Great examples, and a downloadable code library.
http://www.sitepoint.com/books/phpmysql4/

Best regards, ~Ray
<?php // RAY_select.php
error_reporting(E_ALL);

// DEMONSTRATE HOW TO USE THE INFORMATION FROM A DROPDOWN SELECT BOX

// IF ANYTHING WAS POSTED
if (!empty($_POST["e"]))
{
    // GET OUR INPUT VALUE(S) INTO A PREDICTABLE FORMAT
    $e = strtolower(trim($_POST["e"]));

    // A DEFAULT VALUE
    $fruit = 'NOTHING';

    // CHOOSE - COULD ALSO BE A SWITCH / CASE
    if ($e == 'a') { $fruit = 'APPLE'; }
    if ($e == 'p') { $fruit = 'PEACH'; }
    if ($e == 'o') { $fruit = 'ORANJ'; }

    // REPORT
    echo "<br />YOU SELECTED $fruit \n";
}
// END OF PHP - PUT UP THE FORM
?>

<form method="post">
<select name="e">
<option value="" selected>WHICH FRUIT?</option>
<option value="a">Apple</option>
<option value="p">Peach</option>
<option value="o">Oranj</option>
</select>
<input type="submit">
</form>

Open in new window

0
 

Author Comment

by:Paul-Brooks
ID: 33464647
I'll have a look at your code, thanks. In the meantime I have attached 3 bits of code showing the registerform.php (again); the dropdown function and the subsequent register.php script. I hope this makes it a little clearer.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
registerform.php
~~~~~~~~~~~~~~~~


<?php if (isset($reg_error)) { ?>


There was an error: <?php echo $reg_error; ?>, please try again.


<?php } ?>


<?php

// Include init file
include 'init.php';

?>



<form action="register.php" method="post">




<b>Username:</b> <input type="text" size="20" maxlength="20" name="username" 

<?php if (isset($_POST['username'])) { ?> value="<?php echo $_POST['username']; ?>" <?php } ?>/><br />

<b>Password:</b> <input type="password" size="20" maxlength="10" name="userpassword" /><br />

<b>Confirm Password:</b> <input type="password" size="20" maxlength="10" name="confirmpass" /><br />

<p>Authority Level:<br /><!-- Drop-down Box queries A_TABLE_NAME table in A_DB_NAME -->
<?php
/*
// <?php dropdown(location_id, location_name, location, location_name, location_name1); ?></p>
*/



?>

<? php
/*

This is the function function dropdown($intIdField, $strNameField, $strTableName, $strOrderField, $strNameOrdinal, $strMethod="asc")

This is the query   $strQuery = "select $intIdField, $strNameField from $strTableName order by $strOrderField $strMethod";

*/
?>


<?php dropdown(auth_id, auth_desc, auth_tbl, auth_id, authority); ?></p>

<?php 
/*

// tried these two to pass the Auth but failed
select name="auth";
$auth = $_POST["auth"];
*/

/*
select name ="authority";

*/


$auth = $_GET["authority"];

echo $auth;


?>
*/


<input type="submit" name="submit" value="Register!" />



</form>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
from functions script
~~~~~~~~~~~~~~~~~~~~~~~

function dropdown($intIdField, $strNameField, $strTableName, $strOrderField, $strNameOrdinal, $strMethod="asc") {

   //
   // PHP DYNAMIC DROP-DOWN BOX - HTML SELECT
   //
   // 2006-05, 2008-09, 2009-04 http://kimbriggs.com/computers/
   //
   // Function creates a drop-down box
   // by dynamically querying ID-Name pair from a lookup table.
   //
   // Parameters:
   // intIdField = Integer "ID" field of table, usually the primary key.
   // strMethod = Sort as asc=ascending (default) or desc for descending.
   // strNameField = Name field that user picks as a value, ordered by this field.
   // strNameOrdinal = For multiple drop-downs to same table on same page (Ex: strNameField.$i).
   // strOrderField = Which field you want results sorted by.
   // strTableName = Name of MySQL table containing intID and strName.
   //
   // Returns:
   // HTML Drop-Down Box Mark-up Code.
   //

   echo "<select name=\"$strNameOrdinal\">\n";
   echo "<option value=\"NULL\">Select Value</option>\n";

   $strQuery = "select $intIdField, $strNameField from $strTableName order by $strOrderField $strMethod";
   $rsrcResult = mysql_query($strQuery);

   while($arrayRow = mysql_fetch_assoc($rsrcResult)) {
      $strA = $arrayRow["$intIdField"];
      $strB = $arrayRow["$strNameField"];
      echo "<option value=\"$strA\">$strB</option>\n";
   }

   echo "</select>\n";
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
register.php
~~~~~~~~~~~~~~


<?php
// Include init file
include 'init.php';
if (!isset($_POST['submit']))
{
    	// Show the form
     include 'register_form.php';
     exit;
}
else
{

	echo $_POST['username'];
	echo $_POST['userpassword'];
	echo $_POST['authority'];


     // Check if any of the fields are missing
     if (empty($_POST['username']) || empty($_POST['userpassword']) || empty($_POST['confirmpass'] || empty($_POST['authority']))
     {
	
			
          // Reshow the form with an error
          $reg_error = 'One or more fields missing';

		

          // include 'register_form.inc.php';
		include 'register_form.php';

          exit;
     }

     // Check if the passwords match
     if ($_POST['userpassword'] != $_POST['confirmpass'])
     {
          // Reshow the form with an error
          $reg_error = 'Your passwords do not match';
         // include 'register_form.inc.php';
		 include 'register_form.php';

          exit;
     }




     // Everything is ok, register
	// first get
	


     user_register ($_POST['username'], $_POST['userpassword']);

     echo 'Thank you for registering on our site, <a href="index.php">click here</a> to go back.';


	
// $actorquery="SELECT * FROM Users_tbl";
// $actorresult=mysql_query($actorquery);

// $actornum=mysql_numrows($actorresult);

/*
mysql_close();
*/

// echo "<b><center>Database Output</center></b><br><br>";



// $i=0;
// while($i<$actornum){
// $username=mysql_result($actorresult,$i,"user_name");
// $userpassword=mysql_result($actorresult,$i,"user_password");
// echo "<b>$username </b> Password: $userpassword <hr><br>";
// $i++;
// }




}

?>

Open in new window

0
 

Author Comment

by:Paul-Brooks
ID: 33464970
Have just ordered a copy of the book you suggested, thanks.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33466810
You might want to hire a developer to help you with this, at least until you can work through some of the examples in the SitePoint book.  I'm not having much luck following the logic in the code posted above.

When a form is submitted via the POST method, the contents of the form are present in an array named $_POST.  You can print this out with the PHP function var_dump() -- something like this:

var_dump($_POST);

If you echo "<pre>"; before var_dump(); it is easier to read.  Try adding that to the top of your action script so you can see what came through from the HTML form.
0
 
LVL 1

Expert Comment

by:shaziashaikh
ID: 33483180
You can get the selected value from the dropdown box in PHP as shown below:

$_POST['authority'];
0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33788021
If the question you originally asked has been answered, the fact that you decide to not use it is irrelevant, not the question and answer.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33788365
I object to deleting this.  The question was, "...but am unable to pass the selected value from the dropdown box. How do I do it, please?"  There was a complete answer with a tested and working example at ID:33464516.  

In related news, I am glad you have decided to use a framework, and I still hope you will get the SitePoint book recommended above.  The framework will take care of a lot of the housekeeping functions for your application, but you will still need to know how to program PHP.

Best regards, ~Ray
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33793476
Tested and working sample code was provided here: ID:33464516.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add email address from a web page to outlook 2010 contacts ? 8 40
hbo knew my windows software 4 62
Why is my wamp get_include_path() wrong? 2 21
Currency in SQL? 2 30
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

840 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