I am using this code in my form to create a drop down menu. (the list of options loads corrects from my sql database). Once the user hits submit, I should be able to retrieve the value selected with $_POST['field'].
<form action="page2.php" method="post" name="form" id="form">
<?php
$query = sprintf("SELECT domaine FROM `domainema` WHERE userid='%s' ", $userid);
$result=mysql_query($query);
echo "<select name=domaine value=''>Domain </option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[id]>$nt[domaine]</option>";
}
echo "</select>";
?>
...
On the second page, I use this code:
$domaine = strip_tags(substr($_POST['domaine'],0,32));
echo "You selected $domaine";
But I get nothing a blank value, what am I doing wrong?
Thanks!
In your query you didn't selected the id, only the domaine. Change it to be like this:
<form action="page2.php" method="post" name="form" id="form">
<?php
$query = sprintf("SELECT id, domaine FROM `domainema` WHERE userid='%s' ", $userid);
$result=mysql_query($query);
echo '<select name="domaine">';
while($nt=mysql_fetch_array($result)){
echo '<option value="$nt[id]">$nt[domaine]</option>';
}
echo "</select>";
?>
This line is probably incorrect...
echo "<select name=domaine value=''>Domain </option>";
Should it be
echo "<select name=domaine value=''>";
You should also note that if none of the options are selected, then you won't get a value back. To ensure you get a value back, select one of them (eg the first one) by default, by adding selected="selected" to it...
I'd also recommend quoting values a little more clearly.
For the sake of completeness...
<?php
$query = sprintf("SELECT domaine FROM `domainema` WHERE userid='%s' ", $userid);
$result=mysql_query($query);
echo '<select name="domaine" value="">';
$isfirst = true;
while ($nt=mysql_fetch_array($result)) {
echo '<option value="'.$nt[id].'"';
if ($isfirst)
echo ' selected="selected"';
echo '>'.$nt[domaine].'</option>';
$isfirst = false;
}
echo '</select>';
?>
Related
I have a simple table with staff names stored in the column f_operator_name.
I have a drop down menu in a php form with these staff names available for selection. Here is a snippet of the relevant the code:
<?php
echo "<h2>Operator: <select name=f_operator_id></h2>";
$sql="SELECT * FROM radio_archive_index_gui.t_operator ORDER BY f_operator_id";
$result = pg_query($connection, $sql);
if (!$result){
die("Error in SQL query: " . pg_last_error());
}
while ($arr = pg_fetch_array($result, null, PGSQL_ASSOC)){
$operator_id=$arr['f_operator_id'];
$operator=$arr['f_operator_name'];
echo "<option value='$operator'>$operator</option>";
}
echo "</select>";
##### submit form to carry out echo statement for testing purposes
echo "<ul>
<th><input type='submit' name='new' value='Confirm Information'/></th>
</form>
</ul>";
if (isset($_POST['new']))
{
echo $_POST['operator'];
}
?>
When someone selects the staff name I want it to be stored in a variable. I'm testing the submit form at the bottom which is intended to print out the name that has been selected ( in the variable operator), but it's not printing anything out. Can anyone see any issues?
EDIT *** Here's the updated code after some advice from Barmar with the variable information also, for some reason the echo statement still isn't working:
<?php
$connection = pg_connect("host=10.100.51.42 port=5432 dbname=reportingdb user=rai_gui password=password");
echo "<h2>Operator:</h2> <select name='f_operator_id'>";
$sql="SELECT * FROM radio_archive_index_gui.t_operator ORDER BY f_operator_id";
$result = pg_query($connection, $sql);
if (!$result){
die("Error in SQL query: " . pg_last_error());
}
while ($arr = pg_fetch_array($result, null, PGSQL_ASSOC)){
$operator_id=$arr['f_operator_id'];
$operator=$arr['f_operator_name'];
echo "<option value='$operator'>$operator</option>";
}
echo "</select>";
##### submit form to carry out echo statement for testing purposes
echo "<ul>
<th><input type='submit' name='new' value='Confirm Information'/></th>
</form>
</ul>";
if (isset($_POST['new']))
{
echo $_POST['f_operator_id'];
}
?>
You can't put <select> inside <h2> and then put the <option>s and </select> outside it. HTML elements have to be nested properly, and <option> has to be inside <select>.
Change it to:
echo "<h2>Operator:</h2> <select name='f_operator_id'>";
And the index in $_POST has to match the name of the <select>, so $_POST['operator'] should be $_POST['f_operator_id'].
I am trying to edit my form. I want to get selected value selected in selection list.
I have created function to store values in database, and it works. Below is html code I use and function below to insert values in database.
// insert values in database
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" required>
<?php dobavljac() ?>
</select>
function dobavljac(){
$sql=mysqli_query($link, "SELECT * FROM `partneri` WHERE `Dobavljac`='1'
order by `PartnerId` asc ");
echo '<option value="">Izaberi dobavljača</option>';
while($record = mysqli_fetch_array($sql)) {
echo '<option value= "' .$record['PartnerId']. '">' . $record['PartnerNaziv'] . ' </option>';
}
}
// edit values
First I retrieve information from database
$id=$_GET['id'];
$sql = "SELECT * FROM materijali where Id=$id ";
$q = $conn->query($sql);
$r = $q ->fetch();
if ($r) {
$dobavljac=$r['Dobavljac'];
I want to get selected value in box
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" value="<?php echo $dobavljac; ?>">
<?php dobavljac() ?>
</select>
Probably I am not doing it the right way, any advice would be appreciated
Try this
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" value="<?php echo $dobavljac; selected?>">
<option value=<?php echo $dobavljac?> selected>
<?php dobavljac() ?>
</option>
</select>
Try this...
$id=$_GET['id'];
$sql = "SELECT * FROM materijali where Id=$id ";
$q = mysql_query($query);
echo "<select name="dobavljac" class="form-control">";
while (($row = mysql_fetch_row($q)) != null)
{
echo "<option value = '{$row['Dobavljac']}'>";
echo $row['Dobavljac'];
echo "</option>";
}
echo "</select>";
I want the value which I select from the listbox and want to store in php variable because I want to use that variable for further use. Please help..
<form name="myform" method="post">
<?php
include('dbconnect.php');
db_connect();
$query = "SELECT * FROM test_customer"; //Write a query
$data = mysql_query($query); //Execute the query
?>
<select id="cust_name" name="mylist" onchange="selectedvalue()">
<?php
while($fetch_options = mysql_fetch_array($data)) { //Loop all the options retrieved from the query
?>
//Added Id for Options Element
<option id ="<?php echo $fetch_options['test_id']; ?>" value="<?php echo $fetch_options['cust_name']; ?>"><?php echo $fetch_options['cust_name']; ?></option><!--Echo out options-->
<?php
}
?>
</select>
</form>
<select id="cust_name" name="mylist" onchange="selectedvalue(this.value)">
access that values selectedvalue(val) in parameter
get that selected value and store it in input type= hidden as u need it for further use..
If you want to store into a php-variable, you must post the form. I've added a submit-button below. You also must speciy an action to where the form is submitted to: action="whatever.php"
<form name="myform" method="post" action="whatever.php"> <!-- added action here -->
<?php
include('dbconnect.php');
db_connect();
$query = "SELECT * FROM test_customer"; //Write a query
$data = mysql_query($query); //Execute the query
?>
<select id="cust_name" name="mylist" onchange="selectedvalue()">
<?php
while($fetch_options = mysql_fetch_array($data)) { //Loop all the options retrieved from the query
?>
//Added Id for Options Element
<option id ="<?php echo $fetch_options['test_id']; ?>" value="<?php echo $fetch_options['cust_name']; ?>"><?php echo $fetch_options['cust_name']; ?></option><!--Echo out options-->
<?php
}
?>
</select>
<!-- added submit button -->
<input type="submit" value="ok" />
</form>
Create a php-file called whatever.php and in this file, store the value into $cust_name by doing this:
<?php
$cust_name = $_POST['mylist']; //mylist is the name of the select-list
?>
If you want to post the form without having to reload the page, you must use something called ajax.
This works for me.Try this example.
public function getAptTypesBySelectedKy($valKy) {
$stmt = "SELECT ap_typ_ky, ap_typ_desc FROM apt_types WHERE ap_stat=1 order by ap_typ_desc";
try {
$con = DataHandler::connect();
$values = $con->prepare($stmt);
if ($values->execute()) {
echo '<select class="form-control" name="type" id="apt_types_slc">';
while ($row = $values->fetch(PDO::FETCH_ASSOC)) {
if ($valKy === $row['ap_typ_ky']) {
echo '<option value="' . $row['ap_typ_ky'] . '" selected>' . $row['ap_typ_desc'] . '</option>';
} else {
echo '<option value="' . $row['ap_typ_ky'] . '">' . $row['ap_typ_desc'] . '</option>';
}
}
echo '</select>';
}
} catch (PDOException $ex) {
echo 'Error on apartment types';
$error = $ex;
print_r('<pre>' . $ex->getCode() . '</pre>');
print_r('<pre>' . $ex->getMessage() . '</pre>');
}
}
In your html form page.
$__typKy = $_RA['typeky'] //this line get the selected value from database and set as parameter to the function getAptTypesBySelectedKy()
<?php
$__typKy;
if (isset($_RA)) {
$__typKy = $_RA['typeky'];// you need to find this key from database.
//this is the selected value key of `<select>`
}
$var = new DataHandler();
$var->getAptTypesBySelectedKy($__typKy);
?>
I have a dynamic combobox and I have my Fetch button. When a user selects a value from combobox and clicks fetch button, all the other related values are displayed in a textbox for the user to edit and update records. And that works fine.
<form id="form1" method="post" action="edit.php">
<select name="ID" id="select">
<?php display_Id();?>
</select>
<input type="submit" name="Fetch" id="Fetch" value="Fetch" />
</form>
function display_Id() {
$query = "SELECT * FROM Flight";
$result = mysql_query($query) or die("Failed to fetch records");
confirm_query($result);
while($rows = mysql_fetch_array($result)) {
$flightNum = $rows['FlightNo'];
echo "<option value=\"$flightNum\" ";
echo " selected";
echo "> $flightNum </option>";
}
}
The problem is in the Fetch button. When user clicks Fetch, other values are displaying but the selected value from combobox is refreshing. How to make the values remain selected even after pressing the Fetch button?
Your question is incomplete in the sense, that you don't have your dislay_Id() code shown here. However, Generally speaking, you should add selected after <option value="something" programmatically,
Code should be something like this:
function displayId(){
if($value[x]== $currentValue) {
echo "<option value='$value[x]' selected>sth</option>";
}
else
{
echo "<option value='$value[x]'>sth</option>";
}
}
EDIT:: Your code adds a "selected" to each of the values, you must only add a "selected" to a current value.
So, your code must look like this:
echo "<option value=\"$flightNum\" ";
if($_POST['ID'] == $flightNum)
{
echo " selected";
}
echo "> $flightNum </option>";
while($rows = mysql_fetch_array($result))
{
$flightNum = $rows['FlightNo'];
echo "<option value=\"$flightNum\" ";
if($_POST['ID'] == $flightNum)
{
echo " selected";
}
echo "> $flightNum </option>";
}
i have a drop down list which is populated dynamically from database table i.e
<form method=post action='dropdown.php'>
<?php
$query="SELECT DISTINCT style FROM style";
$result = mysql_query ($query);
echo "<select name=style[] size=4 value='' multiple>Choose Style</option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[style]>$nt[style]</option>";
}
echo "</select>";// Closing of list box
mysql_close($conn);?>
<input type=submit> </form>
When i post the form to another page the value of dropdown after space is not shown i.e if i select (Micro Pave) from drop down list then it will only show Micro. my php code is
$style=$_POST['style'];
if( is_array($style)){
while (list ($key, $val) = each ($style)) {
echo $val."<br/>";
}
}
echo "<option value='$nt[style]'>$nt[style]</option>";
Missed your single quotes! :)
<?php
$query = "SELECT DISTINCT style FROM style";
$result = mysql_query($query);
echo 'Choose Style
<select name="style[]" size="4" multiple="multiple">';
while ($nt = mysql_fetch_array($result)) {
echo '<option value="'.$nt['style'].'">'.$nt['style'].'</option>';
}
echo '</select>';
mysql_close($conn);
?>
missing quotes in the option value:
$ntstyle = htmlspecialchars($nt['style'], ENT_QUOTES);
echo "<option value='{$ntstyle}'>".$ntstyle."</option>";
Well for a start any attributes should be contained in double quotes.
echo "<option value=$nt[style]>$nt[style]</option>";
Should be
echo '<option value="'.$nt[style].'">'.$nt[style].'</option>';
So many things wrong unfortunately... :(
Start with "{$nt['style']}" instead of just "$nt[style]".
(or better yet: echo 'Constant', $arr['item'];)
Fix this:
echo "<select name=style[] size=4 value='' multiple>**Choose Style</option>**;
It's a wrong place for a label and the tag is not open!
Also add "" to your params="" in HTML.