Link to home
Start Free TrialLog in
Avatar of Paul Brooks
Paul BrooksFlag for United Kingdom of Great Britain and Northern Ireland

asked on

php dropdown box value

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?
Avatar of Snarfles
Snarfles
Flag of Australia image

Could you post your code please and I'll take a look.

Cheers
Avatar of Paul Brooks

ASKER

<?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
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?

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Have just ordered a copy of the book you suggested, thanks.
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.
Avatar of shaziashaikh
shaziashaikh

You can get the selected value from the dropdown box in PHP as shown below:

$_POST['authority'];
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.
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
Tested and working sample code was provided here: ID:33464516.