PHP selection of drop down menu not storing in variable - php

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'].

Related

mysql distinct to form selections

I have to create a form that takes the select options from mySQL database so that all different values in one column are listed. The code that is not working is following:
<form class="form-horizontal" method="get" action="startlist.php">
<select id="selectbasic" name="klass" class="form-control">
<?PHP
$connection = mysqli_connect("link","dbuser","pass","dbname");
mysqli_set_charset($connection,"utf8");
$sql = "SELECT DISTINCT Klass FROM Voistlejad";
$result = mysqli_query($connection,$sql);
while ($row = mysqli_fetch_array($connection,$result)) {
echo $row["Klass"];
echo "<option value='".$row[0]."'>".$row[0]."</option>";
}
mysqli_close($connection);
?>
<option value='32KK5B'>32KK5B</option><!--This is how it needs to be-->
</select>
<button type='submit' class="btn btn-primary">Move on</button>
</form>
What is wrong?
I think you are mixing up where to use single and double quotes..
Generally, for arrays/objects like $row, you get the value from the array with either no quotes, or single quotes, echo $row['Klass']
while ($row = mysqli_fetch_array($connection,$result)) {
echo $row['Klass']; // single quotes
echo "<option value='". $row['Klass'] ."'>". $row['Klass'] ."</option>";
}
// shows sql error ONLY if there is one to show
printf(" %s\n", mysqli_error($link));
P.S strongly recommend adding code to display MySQLi errors, because stuff happens!

How to write an update SQL statement to update multiple records

I have this code so far, which reads a simple table with 3 varchar fields:
<?php
//db connection code...
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$query = "SELECT * FROM Sheet1";
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<html><body><table cellpadding=10 border=1>";
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['stickerID']."</td>";
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<form name="some form" action="editform.php" method="post">
<input type="checkbox" name="<?php echo $row['stickerID'] ?>" value=" <?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
}
echo "</table></body></html>";
echo " " ?>
<input type="submit" name="editWish" value="Edit">
</form>
<?php " ";
} else {
// no
// print status message
echo "No rows found!";
}
// free result set memory
mysql_free_result($result);
// close connection
mysql_close($connection);
?>
The database has 4 fields, 3 varchar and 1 int with current value of 0. I checked the page source code and confirmed each checkbox name is the stickerID. Now I will post this to the editform.php which I must create. What Im wondering is how should I write the update sql so that it takes into account each new value selected by the user in the form?
This is my idea, but how to I do it for every checkbox?
editform.php
<?php
//update multiple records
//UPDATE user_items SET stickerStatus = $_POST["stickerStatus"] WHERE stickerID = $_POST["stickerID"];
?>
First question: use mysql_fetch_assoc() instead of mysql_fetch_row(). That will return an associative array instead of an enumerated one.
Second question: read up on HTML forms and form handling.
The answer to the question in the comments:
// The <form> tag should only be echoed once.
echo '<form name="some form" action="editform.php" method="post">';
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['stickerID']."</td>";
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<input type="hidden" name="status_<?php echo $row['stickerID"; ?>" value="0">
<input type="checkbox" name="status_<?php echo $row['stickerID'] ?>" value="<?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
}
// You need a submit button to send the form
echo '<input type="submit">';
// Close the <form> tag
echo '</form>';
Using a hidden input with the same name as the checkbox makes sure a value for the given input name is sent to the server. The value of a checkbox that's not checked will not be sent. In that case the hidden input will be used.
You can get the submitted values in editform.php as follows:
<?php
foreach ($_POST as $field => $value) {
if (strpos($field, 'status_')) {
// Using (int) makes sure it's cast to an integer, preventing SQL injections
$stickerID = (int) str_replace('status_', '', $field);
// Again, preventing SQL injections. If the status could be a string, then use mysql_real_escape_string()
$stickerStatus = (int) $value;
// Do something with the results
}
}
Do
print_r($row)
to find out exactly how your row arrays are constructed and work from there.
For your comparison operator, use
$row[3] === 0
instead of
$row[3] == 0
This will return true if both the value and data type match rather than just the value.
0 can mean the Boolean false aswell as the numeric value 0

displaying results based on combobox selection

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>";
}

problem with drop down value in php

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.

How to retrieve select value with method POST in a PHP from?

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

Categories