Solved

php dropdown box value

Posted on 2010-08-18
13
675 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
Comment Utility
Could you post your code please and I'll take a look.

Cheers
0
 

Author Comment

by:Paul-Brooks
Comment Utility
<?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
Comment Utility
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Paul-Brooks
Comment Utility
Have just ordered a copy of the book you suggested, thanks.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
You can get the selected value from the dropdown box in PHP as shown below:

$_POST['authority'];
0
 
LVL 9

Expert Comment

by:Snarfles
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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 108

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

744 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

8 Experts available now in Live!

Get 1:1 Help Now