populate select box with data from mysqltable - php

How to populate select box with data from mysql table, and then move that value from one page to another.
I did a small coding, and i was able to get all the values from table, but i cant move those value to other page.
$query1="SELECT * FROM seat_no WHERE seatno NOT IN(SELECT seatno FROM check_in_desk)";
$result1 = mysql_query($query1);
<select name="txt_seatno">
<?php
while($nt=mysql_fetch_array($result1))
{
echo "<option value=$nt[id]>$nt[seatno]</option>";
}
</select>
?>

Can you just lookup the values on the page where you need them? If not, you could pass the data as a GET or POST variable, and then use this to generate the select box options on the new page.

There's two ways I can think of;
1) append the value to the url, so it'd be;
echo '' . $nt[seatno] . '';
and in the receiving page, you'd put;
$id = (int) $_GET['id'];
2) Or you could simply use this;
http://www.w3schools.com/html/tryit.asp?filename=tryhtml_select2
(dropdown menu)
(btw; your < /select> should come after ?>)

Related

PHP Select tags problems

I just started learning some PHP and SQL for my uni. I got everything figured out somehow but there have been a few problems. So what I'm doing is getting the values of a 'select' dropdown dynamically from the Database.
$sql = "SELECT catDesc, catID from Categories";
$queryResult = $dbConn->query($sql);
echo '<select name="eventcat" size="1" class="dropdownstyle" id="catevent" required/>';
echo '<option value="choose">Event Category</option>';
while($row=mysqli_fetch_array($queryResult)){
$xx = $row['catDesc'];
$id = $row['catID'];
echo '<option value="' . $id . '">' . $xx . '</option>';
}
So this above piece of code works. However. After the user submits the form. It redirects to a new page. "admin-process.php". I want this page to somehow get the value of the variable "$xx". I know how to get the value by using this method:-
$id = isset($_REQUEST['eventcat']) ? $_REQUEST['eventcat'] : null;
However, this displays the id of the option. Not the main thing that I need. The id and the value differ here. So in short. How do I get the name of the option tag?
How do I get the name of the option tag.
You don't. At least not directly. The only value posted as part of the form is the selected value from that element. In this case your catID value.
That value should uniquely identify the record which was selected. (If it doesn't, that's a different problem.) With that value you can then query the database to get the rest of the information from the uniquely identified record. It may contain one more field, several more fields, joins with other tables, etc. Doesn't really matter what it contains, as long as you can uniquely identify it based on that ID.
So on your next page (admin-process.php) you'd read the posted catID value and use it in a query to your Categories table. That query should return one record, from which you'd display the additional data.

PHP Edit form for MySQL database <option> value is not correct after a fetch

I have a Edit form in php and everything work great. The only issue i have is when i click edit it returns all the data except in the Select drop down. it does not have the chosen category it always shows the first value in the list. But i then can click on the drop down and choose a new category and it works.
//Query the category table
$resultSet = $con->query("SELECT * FROM schedule_category");
<select id="schedule_category" name="schedule_category" class="custom-select">
<?php
while($rows = mysqli_fetch_assoc($resultSet))
{
?>
<option value = "<?php echo($rows['schedule_category'])?>">
<?php echo($rows['schedule_category']) ?>
</option>
<?php
}
?>
</select>
I would like to have it show the correct select option record not the first one in the drop down list. Here is an image of what happens https://imgur.com/a/XVXQ2Sa
You'll need to have your code compare each to the selected value, and add the appropriate keyword:
$previous_selection = // whatever it is, from your data
while($rows = mysqli_fetch_assoc($resultSet))
{
$thisone = $previous_selection == $rows['schedule_category'] ? " selected " : "";
echo '<option value = "';
echo ($rows['schedule_category']) . '"' . $thisone . '>';
echo($rows['schedule_category']) . '</option>';
}
What you're doing here is comparing your previously-selected value to each row, when it matches, the variable $thisone is set to "selected", otherwise it's empty. You then add that to each option line after the value and before the close-tag for the option, and it will add "selected" when the value matches.
Also I personally don't like switching in and out of PHP for no good reason, makes it really difficult to read, hence I echo the various bits of HTML here.
ETA - actually that could be simplified further, if your selection value is the same as the text displayed in the list, there's no need to actually specify the value in the option tag. That is only required when the value is different to the display, for example if you want the user to see your category names, but you want to submit the category ID.

Set the default value of a dynamic select drop down list in php

I have a form in an edititem.php page that is used to edit an item. The idea is that from another page, the searchitem.php a user can click edit and is taken to the edititem.php page to edit the selected item. What I'm trying to do is that automatically the default values in the form elements are populated with the values of the item selected to edit (from the searchitem.php) so that the user finds the form populated and only needs to modify the necessary value/s.
I'm passing all the required variables to the edititem.php and I am able to poulate all the input tags but I have a problem with the select tag which is not being set with the desired value. The select tag is being populated dynamically from a mysql database.
<label>Category:</label>
<select name="category">
<?php
$selectedCategory = '';
if (isset($_POST['category'])) {
$selectedCategory = $_POST['category'];
}
$sql_cat = "SELECT id, description FROM category ORDER BY description ASC";
$result_cat = mysqli_query($connection, $sql_cat);
if(mysqli_num_rows($result_cat) > 0){
while($row = mysqli_fetch_assoc($result_cat)){
$selected = '';
if ($selectedCategory == $row['id']) {
$selected = 'selected';
}
echo '<option value="' . htmlspecialchars($row['id']) . '" '.$selected.'>'
. htmlspecialchars($row['description'])
. '</option>';
}
}
?>
With the above code the items in the select tag are being populated dynamically from a table and also if the page is refreshed the selected item is maintained as the selected value.
However I cannot set the default value when I press the edit item from the searchitem.php. The last value in the table is being displayed as default. Any ideas how I can achieve this since I cannot figure out how to do it. Note that all the variables are being passed successfully to the edititem.php page I just need to set the default value of the select drop down list as per the passed variable while keeping the select drop down list dynamic.

Assign and return select option dynamically?

I have a form select which is generated based on results returned from a mysql query.
Q1) How do I assign a name identity (integer value) based on the teamID pull out of the database?
Q2) How could I then get the option selected and add it to a php variable which could then be used to update a table based on the users selection?
Below is the code I have so far for creating the dynamic drop down list, which get the results out of the database.
<?php
$data= mysql_query("SELECT * FROM teams
WHERE teamID NOT IN (
SELECT TeamID
FROM leagueInformation
WHERE leagueID = 1
)
") or die(mysql_query());
echo "<select name=\"team\" class=\"col-lg-12\" style=\"padding:10px; background:#e1e1e1;\">\n";
while($info = mysql_fetch_array( $data ))
{
$teamID = $info['teamID'];
echo "<option name=" . $team . " value=" . $teamID . ">" .$info['teamName'] . "</option>";
}
echo "</select>\n";
?>
Q1) How do I assign a name identity (integer value) based on the teamID pull out of the database?
This question is not so clear, but I think this is what you are trying to do?
<?php $data= mysql_query(
"SELECT * FROM teams WHERE teamID NOT IN (
SELECT TeamID
FROM leagueInformation
WHERE leagueID = 1)
") or die(mysql_query());
?>
<form name="teams_form" method="POST">
<select name="team" class="col-lg-12" style="...">
<?php while($info = mysql_fetch_array( $data )): ?>
<option value="<?php echo $info['teamID'] ?>">
<?php echo $info['teamName'] ?>
</option>
<?php endwhile; ?>
</select>
</form>
Q2) How could I then get the option selected and add it to a php variable which could then be used to update a table based on the users selection?
First you will need to wrap your select inside a form (like I did), then you can check the selected value like this:
if(isset($_POST['team']) && !empty['team']){
$selected_team = $_POST['team'];
mysql_query("Do what you want with the selected team");
}
Some notes about your code:
Avoid to use mysql_*, use mysqli_* or PDO, and always prevent SQL injections
<option> tags don't have name properties, and in this case $team is not defined
Embed PHP inside HTML, not HTML in PHP.
Your first question doesn't make sense to me yet, I'll update the answer when it does.
When the user submits the form, you can get the option selected using the $_GET or $_POST (depending your your form's method) variables.
Like this:
$teamId = $_POST['team'];
put everything in a <form/> with an action pointing to the page, and a method (get or post), then in the script get the variable by the <select/> name value reading $_GET or $_POST variables, and I quote the comment about the "name" in option as invalid attribute.

select id from the select gender dropdown list

I have a dropdown list of gender. I am getting the values from my table 'candidate' and in this table i have a field which is actually a foreign key to another table.
The field is gender_code_cde, and the table name is gender_code. Now gender_code contains 2 rows. and id and a description. Its structure is like:
1->Male
2->Female
Its being displayed in dropdown list. but I want to get the id 1 if male is selected and id 2 if female is selected. My code is below:
<p>
<label for ="gender">Gender:</label>
<?php
$query = mysql_query("select * from gender_code"); // Run your query
echo '<select name="GENDER">'; // Open your drop down box
//Loop through the query results, outputing the options one by one
while ($row = mysql_fetch_array($query))
{
echo '<option value="'.$row['gender_cde'].'">'.$row['gender_dsc'].'</option>';
}
echo '</select>';
?>
</p>
I am working in codeIgniter.
// this will alert value of dropdown whenever it will change
<script>
function getvalue(val)
{
alert(val);
}
</script>
<p>
<label for ="gender">Gender:</label>
<?php
$query = mysql_query("select * from gender_code"); // Run your query
echo '<select name="GENDER" id="Gender" onchange="getvalue(this.value)">'; // Open your drop down box
//Loop through the query results, outputing the options one by one
while ($row = mysql_fetch_array($query))
{
echo '<option value="'.$row['gender_cde'].'">'.$row['gender_dsc'].'</option>';
}
echo '</select>';
?>
</p>
In Javascript, you can get the value with native JS.
var e = document.getElementById("give-the-select-element-an-id");
var gender_cde = e.options[e.selectedIndex].value;
If, you want it back at the server, it will come in to PHP as $_GET['GENDER'] or $_POST['GENDER'] depending on if the form does a POST or a GET request.
If you are submitting a (POST) form and this is inside that form you can access the the value (id) from CodeIgniter's $_POST wrapper: $this->input->post('GENDER');. I don't use CodeIgniter so I could be wrong, but it will be located in your $_POST array from PHP.
If you just want to replicate the variable somewhere on the page you can just echo it. Or set via js/jQuery with document.getElementById('#something').value = value; or jQuery('#something').html(value);.
You should also change the following:
use a foreach in place of your while:
foreach ($query as $row) {
echo $row['gender_x'] . $row['gender_y'];
}
use the CodeIgniter SQL wrapper instead of depreciated PHP functions. The query part is $this->db->query('SELECT statement goes here');. You should look at your CodeIgniters' version documentation for a more in depth explanation.
EDIT: To make it a bit more clear, an example:
$query = $this->db->query('SELECT * FROM gender_code');
foreach ($query->result() as $row) {
echo '<option value="' . $row->gender_cde . '">' . $row->geneder_dsc . '</option>';
}
This is assuming you have setup the previous calls in CodeIgniter. Please see http://ellislab.com/codeigniter/user-guide/database/examples.html and you may wish to read http://ellislab.com/codeigniter/user-guide/

Categories