Selected Dropdown data will not display in another page - php

I have a dropdown bar that is displayed using php.
<select id = "equipment" name="dropdwn" class="form-control">
<option selected="" disabled="">Select Equipment</option>
<?php
$sql = mysqli_query($conn, "SELECT resource_name From resources WHERE resource_type = 'EQUIPMENT';");
$row = mysqli_num_rows($sql);
while ($row = mysqli_fetch_array($sql)){
echo "<option name = 'equipment' value ='". $row['resource_name'] ."'>" .$row['resource_name'] ."</option>" ;
}
?>
</select>
So I want to display the data that is being selected from that dropdown to another page.
I tried this and it does not work. It gives me an error "Notice: Undefined index: equipment"
<?php
include('dbconnector.php');
$equipment = $_POST['equipment'];
echo $equipment;
?>
If you could help me I would really appreciate it. Thanks ahead!

Change select name to equipment:
See below:
<select id = "equipment" name="equipment" class="form-control">
<option selected="" disabled="">Select Equipment</option>
<?php
$sql = mysqli_query($conn, "SELECT resource_name From resources WHERE resource_type = 'EQUIPMENT';");
$row = mysqli_num_rows($sql);
while ($row = mysqli_fetch_array($sql)){
echo "<option name = 'equipment' value ='". $row['resource_name'] ."'>" .$row['resource_name'] ."</option>" ;
}
?>
</select>

There are many selectors for HTML form elements
1) id -> used for css and javascript purposes (unique for each element).
2) class -> used for css and javascript purposes (multiple elements can have same class).
3) name -> This selector is very important. When we get our HTML form submitted to PHP, the elements are accessed with this selector.
For example:
<form method="post"...>
<input name="student" type="text" id="id_student"/>
...
If we submit the above form, in PHP we will the texbox value through:
$_POST['student']
So, please correct your name attribute of drop down to make the page working.

Related

Select values based on database result

I have a form with a multi select which presents the user with a list of groups to chose from,which is then saved in the database. My aim is that when the user wants to edit his information, the groups saved in the database to be selected and the others not to be selected. Please, see my code and advise.
I populate the saved groups to a variable
$groups = array($row['groups']); // This outputs the groupId from the db eg 1,2,3,6
Populate groups to a multi select
<div class="form-group">
<!-- <label>Church Groups: </label> -->
<select name="groups[]" class="form-control mandatory" id="groups" multiple="multiple" >
<?php
$sql="SELECT ID,UCASE(groupName) AS groupName FROM tblgroups WHERE (congregationId=?)";
$stmt=mysqli_stmt_init($con);
mysqli_stmt_prepare($stmt,$sql);
mysqli_stmt_bind_param($stmt,'i',$congId);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_array($result)) {
$selected = "";
if(in_array($row['ID'],$groups)){
$selected = "selected";
}
echo '<option value='.$row['ID'].' '.$selected.'>'.$row['groupName'].'</option>';
}
?>
</select>
Thats my code but it doesn't work. If the first group in the list was saved, only that group is selected all others remain unselected whether in the db or not.
<div class="form-group">
<!-- <label>Church Groups: </label> -->
<select name="groups[]" class="form-control mandatory" id="groups" multiple="multiple" >
<?php
$sql="SELECT ID,UCASE(groupName) AS groupName FROM tblgroups WHERE (congregationId=?)";
$stmt=mysqli_stmt_init($con);
mysqli_stmt_prepare($stmt,$sql);
mysqli_stmt_bind_param($stmt,'i',$congId);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_array($result)) {
$selected = "";
if(in_array($row['ID'],$groups)){
$selected = "selected";
}
echo '<option value='.$row['ID'].' '.$selected.'>'.$row['groupName'].'</option>';
}
?>
</select>
Using your code, I have uncommented the line you commented out and introduced a variable that will be blank if the value is not in the group, otherwise it will have the value "selected", which achieves the same functionality as selected = "selelected"
First of all, assuming that your $result element is correct, you need something like this.
<?php
while ($row = mysqli_fetch_array($result)) {
$isSel = in_array($row['ID'],$groups)? "selected" : "";
echo "<option value=\"".$row['ID']."\" $isSel >".$row['groupName']."</option>";
}
?>
It is just a matter of order and linearity:
Populate a $isSel variable that contains the selected string only when your conditions are met
Print all the options, and always append $isSel. It will just contain selected only when it is required
But there are other two issues preventing your drop-down menu to be shown as expected:
Remove the square brackets [] from the select name. They are preventing the correct parsering of the multiple attribute
You don't need to specify multiple="multiple". Just multiple is enough (see here). You did also the same mistake with selected="selected": just selected is enough
So, your <select> tag definition becomes
<select name="groupsABC" class="form-control mandatory" id="groups" multiple >
instead of
<select name="groups[]" class="form-control mandatory" id="groups" multiple="multiple" >

Displaying default value through select tag

I am having trouble passing & displaying default value for select tag. So, I have tried different variants of select and option tag but I am not able to get this right.
I want to fetch different options in form of a drop down menu. I want to display a default value in it. ($categorytemp[$i] in code). On displaying the form
A user can update this choice; or
A user can select the same choice again; or
A user can not change it at all.
Expected result for the above activity should be update, update, default value for $category
Below is the snapshot of my code
<select name="category" value="<?php echo $categorytemp[$i] ?>">
<?php
$sql = "SELECT * FROM education_details";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result))
{
$education_category = $row["id"];
$education_name= $row["name"];
?>
<option value='<?php echo $education_category?>'><?php echo $education_name;?></option>
<?php
}
?>
</select>
I tried using the option tag with selected attribute but that is making things more complicated. For example, if I use
<select name="category" value="<?php echo $categorytemp[$i] ?>">
<option selected="selected"><?php echo $categorytemp[$i]; ?> </option>
<?php
$sql = "SELECT * FROM education_details";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result))
{
$education_category = $row["id"];
$education_name= $row["name"];
?>
<option value='<?php echo $education_category?>'><?php echo $education_name;?></option>
<?php
}
?>
</select>
It displays the form as desired but on making no selection, it passes null value in the category. I have gone through questions of similar type asked earlier, but they do not answer my query to my satisfaction
You do it with a "selected" attribute of a given <option> and not in the <select> tag with the value attribute.
Check these two:
http://www.w3schools.com/tags/tag_select.asp
http://www.w3schools.com/tags/tag_option.asp
Have you consider using Javascript on the form submit to achieve it?
Another option would be a hidden field with default value before the select and an hidden option of the select field:
<html>
<head>
<body>
<form method="POST" action="">
<input type="hidden" name="dropdown" value="default" />
<select name="dropdown">
<option selected disabled hidden style="display: none" value="default"></option>
<option>1</option>
<option>2</option>
<input type="submit" value="send" />
</form>
</body>
</html>
Is $categoryTemp[$i] an ID or a name? If it is the ID and you are wanting to check the result against it to determine the default option to display then something along these lines would select the option that is equal to the value of $categoryTemp[$i].
<option <?php echo ($categorytemp[$i] == $education_category ? 'selected' : ''); ?>
So if $categorytemp[$i] = 1 and $education_category = 1 then that option would be marked selected and be displayed when the page loaded
This did the trick. When no value is selected, pass the default value as value in option
<select name="category" >
<option value='<?php echo $categorytemp[$i]?>'><?php echo $categorytemp[$i]; ?> </option>
<?php
$sql = "SELECT * FROM education_details";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result))
{
$education_category = $row["id"];
$education_name= $row["name"];
?>
<option value='<?php echo $education_category?>'><?php echo $education_name;?></option>
<?php
}
?>
</select>

using php code within html to dynamically load a dropdown list

I am trying to use php code within html to grab content from a database to populate the values for a drop down menu (see below). When I run the php script on its own (test.php) I get all the expected values. When nested within html I get nothing but a single blank value under Select a Species. I would expect this to run through the while loop more than once as there are about 7 values returned and I would also expect the contents to include data derived from the table.
What I am attempting is possible correct??
Is it just an error with code (no errors popping up in the logs)
<label for="select">Species Observed : </label>
<select name="select" class="textfields" id="species">
<option id = "0">-- Select a Species -- </option>
<?php
$con = mysqli_connect("localhost","xxx","xxxx","databases");
$result = mysqli_query($con, "SELECT * FROM bc_species WHERE 1");
while ($row = $result->fetch_assoc()){
?>
<option><?php echo $row['species'];?></option>
<?php } ?>
</select>
<option><?php echo $row["species"]; ?></option>
<?php
$optionData = '<option id = "0">-- Select a Species -- </option>';
$con = mysqli_connect("localhost","xxx","xxxx","databases");
$result = mysqli_query($con, "SELECT * FROM bc_species WHERE 1");
while ($row = $result->fetch_assoc()){
$optionData .= "<option>".$row['species']."</option>" ;
}
?>
<label for="select">Species Observed : </label>
<select name="select" class="textfields" id="species">
<?php echo $optionData;?>
</select>
//Put your fetched data on an array then loop them like this
<?php
foreach ($list as $data => $item) {
?>
<option value="<?php echo $data?>">
<?php echo $data?>
</option>
<?php
}
?>
This should work:
<label for="select">Species Observed : </label>
<select name="select" class="textfields" id="species">
<option id = "0">-- Select a Species -- </option>
<?php
$con = mysqli_connect("localhost","xxx","xxxx","databases");
$result = mysqli_query($con, "SELECT * FROM bc_species");
while ($row = $result->fetch_assoc()){
echo '<option>'. $row["species"].'</option>
} ?>
</select>
You were not iterating through the values within the HTML <option> tags, so I moved those into the while loop. (notice the PHP section includes echoing those tags).
I also removed the unnecessary "SQL injection-looking" where 1 clause from your query.

Select from drop-down menu and reload page

I've got a table that populates data from a MYSQL database and populates a drop-down menu from the same database. I have the drop down menu and table just fine, I would like to be able to choose which data I show in the table however.
<select name = 'peer-id' method='post' style = 'position: relative'>
<?php
while ($content = mysql_fetch_array($peer)) {
echo "<option value='" . $content['Peer'] . "'>" . $content['Peer'] . "</option>";
}
$results = mysql_query("SELECT Destination FROM rate ");
?>
</select>
That's what I have for the select box. How can I get the choice from that and save that as a variable and refresh the table data?
I need to clarify that this will change that current data
#Data#Data#Data
#Data#Data#Data
#Data#Data#Data
Then choose drop down choice and I want it to show new data
#Data2#Data2#Data2
#Data2#Data2#Data2
#Data2#Data2#Data2
So it's going to need to load a new page or refresh some how because it's changing via PHP and not javascript.
I think form may be better, for example
<form id="myform" method="post">
<select name = 'peer-id' style = 'position: relative' onchange="change()">
<option value="1">12</option>
<option value="2">15</option>
<option value="3">16</option>
<option value="4">18</option>
</select>
</form>
<script>
function change(){
document.getElementById("myform").submit();
}
</script>
In the above code, whenever you change the value of select, it will post to the backend, then according to the posted value, you can do want you want, to get the peer-id in php, you can use the following code
$peer-id = $_POST['peer-id'];
Hope helps!
apply this code in select tag hope this works
<select onchange="location = this.options[this.selectedIndex].value;" style="text-decoration:none;">
<option value="customers.php"></font></option>
</select>
insted of the static options, you can do it like this :) here you get all the options from the database. Just replace it with the static options
$peer = mysql_query("SELECT Peer FROM rate Group By Peer Where peer = 'variable'");
$result_peer = mysql_query($peer);
if($result_peer){
while($row_peer = mysql_fetch_array($result_peer)){
echo'<option value='.$row_peer['Peer'].'>'.$row_peer['Peer'].'</option>';
}
I agree in using form, and with this you can echo back onto the page with a submit button (code tested):
<form id="myForm" method="POST">
<select name="select" onchange="<?php echo $_SERVER['PHP_SELF'];?>">
<option value="N">No</option>
<option value="Y">Yes</option>
</select>
<input type="submit" name="formSubmit" value="Submit" >
</form>
<?php
if(isset($_POST['formSubmit']) ){
$var = $_POST['select'];
$query = "SELECT * FROM table_name WHERE DesiredField='$var'";
$result = mysql_query($query)
or die(mysql_error());
while($row = mysql_fetch_array($result)){
$var2 = $row['FieldName'];
echo "First field: " . $var2 . "<br>";
// and so on for what you want to echo out
}
}
?>

Displaying a php mysql result with two values with a foreach loop in PHP

I have result that I want to display as a drop down menu. The query selects id and name from a table.
$usersQuery = "SELECT id, name
FROM users";
$usersResult = mysqli_query ($dbc, $usersQuery);
I want to use this result as a list in a drop down menu. This is what i have so far.
<select id="dropdown" name="dropdown">
<option value="select" selected="selected">Select</option>
<?php
while ($usersRow = mysqli_fetch_array($usersResult, MYSQLI_ASSOC)){
foreach ($usersRow as $value){
echo "<option value=\"$value\"";
echo ">$value</option>\n";
}
}
?>
</select>
this would work fine if I just wanted to display name as both the value and the display to the user. But what I want to do is use the selected id as "value" for the select option and I want to show the name selected to the user. I have tried this but it does not work.
<select id="dropdown" name="dropdown">
<option value="select" selected="selected">Select</option>
<?php
while ($usersRow = mysqli_fetch_array($usersResult, MYSQLI_ASSOC)){
foreach ($usersRow as $id=>$name){
echo "<option value=\"$id\"";
echo ">$name</option>\n";
}
}
?>
</select>
Any help would be great.
Thanks in advance.
mysqli_fetch_array() is a function which converts your query results into an array. which means you can display your values like you would with a normal array value.
<select id="dropdown" name="dropdown">
<option value="select" selected="selected">Select</option>
<?php
while ($usersRow = mysqli_fetch_array($usersResult, MYSQLI_ASSOC)){
echo "<option value=\"".$usersRow['id']."\"";
echo ">".$usersRow['name']."</option>\n";
}
?>
</select>
No need for the foreach iteration; mysqli_fetch_array() already provides an associative array. After each fetch do
// Assuming "id" is a numeric value
printf('<option value="%d">%s</option>', $usersRow['id'], $usersRow['name']);
The foreach is unnecessary - using a while loop on the mysqli_fetch_array command will return all the results with each row in an array - you can use it like so:
while ($usersRow = mysqli_fetch_array($usersResult, MYSQLI_ASSOC)){
echo "<option value=\"".$usersRow['id']."\"";
echo ">".$usersRow['name']."</option>\n";
}

Categories