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

x
?
Solved

php dropdown box value

Posted on 2010-08-18
13
Medium Priority
?
689 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
[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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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 111

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 111

Expert Comment

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

Featured Post

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.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

705 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