• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 694
  • Last Modified:

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?
0
Paul-Brooks
Asked:
Paul-Brooks
  • 4
  • 3
  • 3
  • +1
1 Solution
 
SnarflesCommented:
Could you post your code please and I'll take a look.

Cheers
0
 
Paul-BrooksAuthor Commented:
<?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
 
SnarflesCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Ray PaseurCommented:
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
 
Paul-BrooksAuthor Commented:
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
 
Paul-BrooksAuthor Commented:
Have just ordered a copy of the book you suggested, thanks.
0
 
Ray PaseurCommented:
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
 
shaziashaikhCommented:
You can get the selected value from the dropdown box in PHP as shown below:

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now