I've googled a lot and found alternative solutions but in my case things are a bit different.
<select name='database'>
<?php foreach($databases as $row): ?>
<option value="<?php echo $row; ?>"><?php echo $row; ?></option>
<?php endforeach; ?>
</select>
I need to display the selected option after a POST request in between the option tags but since I already have a value for that I cannot find a way to do so. The idea is that I have one form with a couple of select menus. From the first one I select a database. The second one is for selecting a table from the already chosen database and another select menu for the columns. The problem is that I'm sending a new request for both the database and table and the chosen database cannot be remembered (it just 'resets' the select menu and starts from the first value).
Here's the whole code
Right now I need to reselect the database which I've previously chosen in order to display the columns from a table.
<select name='database'>
<?php foreach($databases as $row): ?>
<option value="<?= $row; ?>"
<?php if ($row == $_POST['database']){echo " selected";}?>>
<?= $row; ?>
</option>
<?php endforeach; ?>
</select>
Wouldn't this work?
$dbms=$_POST['database'];
<select name='database'>
<?php foreach($databases as $row): ?>
<option value="<?php echo $row; ?>"
<?php if ($row == $dbms) echo " selected"; ?>
> <?php echo $row; ?></option>
<?php endforeach; ?>
</select>
Related
I have a dropdown box where the data is coming from my database. I want to send the selected drop-down item to the controller action method. And then I will send these values to the model to do the further works. Here, the problem is I have two database values in a single item of drop-down box. And, I am not figuring out how to send those two the method. Here is my code given below,
<select name='select'>
<option selected disabled>Choose Stations</option>
<?php foreach ($get_stations as $get_stations_item): ?>
<option>Station <?php echo $get_stations_item['sourcestationid']; ?> - Station <?php echo $get_stations_item['destinationstationid']; ?></option>
<?php endforeach; ?>
</select>
From this dropdown items, I want to send the sourcestationid and destinationstationid separately as 2 parameters to my controller action method. Here is my controller code though this is not correct way I think,
function getdata(){
$iotdata['test'] = $this->input->post('select2');
//rest of the code according to the source and destination id item
}
Thanks in advance.
You haven't kept the variable inside option's value tag.
<select name='select'>
<option selected disabled>Choose Stations</option>
<?php foreach ($get_stations as $get_stations_item): ?>
<option value="<?php echo $get_stations_item['sourcestationid'].','.$get_stations_item['destinationstationid']; ?>">Station <?php echo $get_stations_item['sourcestationid']; ?> - Station <?php echo $get_stations_item['destinationstationid']; ?></option>
<?php endforeach; ?>
</select>
Also here at PHP end, you can explode the string into an array and store it further into db.
<?php
$select = explode(',', $select)
print_r($select); // this will have your two values
?>
Thanks #BitsPlease for our suggestion. Your idea is working. But I need to do a small change to get it instantly. My select tag needs to be under form tag to get them without reloading the page again. Here is my view,
<form method="post" accept-charset="utf-8" action="<?php echo site_url("controller/action"); ?>">
<select name='select' onchange="this.form.submit()">
<option selected disabled>Choose Stations</option>
<?php foreach ($get_stations as $get_stations_item): ?>
<option value="<?php echo $get_stations_item['sourcestationid'].','.$get_stations_item['destinationstationidr']; ?>">Station <?php echo $get_stations_item['sourcestationid']; ?> - Station <?php echo $get_stations_item['destinationstationidr']; ?></option>
<?php endforeach; ?>
</select>
</form>
Here is the php code,
$select = explode(',', $this->input->post('select'));
print_r($select)
I couldn't keep the selected value after loading the page, that's why I couldn't take the selected value from the select option in Database after done some function..
Here is my code
<select name='courseID' class="mySelect" id='courseID'>
<?php while ($row1 =mysqli_fetch_array($result1)):;?>
<option value="<?php echo $row1['CourseID'];?>"><?php echo $row1['CourseID'];?></option><?php endwhile; ?></select>
<script type="text/javascript">document.getElementById('courseID').value="<?php echo $_GET['courseID'];?>";</script>
Did JS can add in a HTML in this way??
You have add condition to check if the course id is in the result.
<?php while ($row1 =mysqli_fetch_array($result1)):;
$selected = ($_GET['courseID'] == $row1['CourseID']) ? 'selected' : '';
?>
<option value="<?php echo $row1['CourseID'];?>" <?php echo $selected;?>><?php echo $row1['CourseID'];?></option>
<?php endwhile; ?>
I have a dropdown showing options from mysql, no problem there. The problem is on the update page when i want to show the option already selected previously.
The dropdown selects options from the margins table and puts the value into a field in the products table.
This is the query that selects the product record :
<?php
$recordID = $_GET["recordID"];
$product_result = mysqli_query($con,"SELECT * FROM products WHERE product_code='$recordID'") or die(mysqli_error($con));
$product = mysqli_fetch_array($product_result);
$checked_special = $product['product_special'];
$checked_publish = $product['product_publish'];
$checked_frontpage = $product['product_display_frontpage'];
$checked_facebook = $product['display_facebook'];
{
?>
And this is the part of the form that gets the options from the margins table and displays them on page.
<tr>
<td>Display Facebook</td>
<td><input type="checkbox" name="display_facebook" id="display_facebook" value="y" <?php if ($checked_facebook == 'y') echo 'checked'; ?> /></td>
<td><strong>Margin Group :</strong></td>
<td>
<select name="margin_group" id="margin_group"><?php
$resul2 = mysqli_query($con,"SELECT * FROM margins");
while($row2 = mysqli_fetch_array($resul2))
{
?> <option value="<?php echo $row2['margin_group']; ?>"> <?php echo $row2['margin_group']; ?></option>
<?php } ?> </select></td>
</tr>
How can i get the $product['margin_group'] value from the products table show as selected option in the dropdown, so that the user doesn't have to reselect every time they update the page.
Thanks :)
MsKazza
The idea is to add the word selected in the desired option tag like this :
<option value="x" selected>x</option>
This way it will be selected in the form Check this
in order to do that we will make a conditional statement for every option value in the while loop. If the value meets the condition, we will echo the word selected
<?php while($row2 = mysql_fetch_array($resul2): ?>
<option value="<?= $row2['margin_group']; ?>"
<?php if($row2['margin_group']) == $products_table_variable) : ?>
selected
<?php endif; ?>
><?= $row2['margin_group']; ?></option>
<?php endwhile ?>
<select name="margin_group" id="margin_group">
<?php
$datasource = mysqli_query($con,"SELECT * FROM margins");
while($getdata= mysql_fetch_array($$datasource)){
?>
<option value="<?=$row2['margin_group']?>" <?php if($getdata['colume_name']==$row2['margin_group']) echo "selected";?>> <?=$row2['margin_group']?></option>
<?php } ?>
</select>
Hope it will help you :)
This will hopefully be an easy one, but I'm lacking the skills!
<select name="search_category" id="select1" >
<option value="">By Category</option>
<?php if (!empty($_POST['search_category'])) { ?>
<option value="<?php echo $_POST['search_category']; ?>" selected="selected"><?php echo $_POST['search_category']; ?></option>
<?php }?>
<?php foreach($categoriesListt as $row) : ?>
<option value="<?php echo $row->id; ?>"><?php echo $row->name; ?></option>
<?php endforeach; ?>
</select>
The above is one of many select in a search module. It returns a dynamic list of options from a query higher up in my page. My goal is to have the option last searched pre-selected. Everything works as intended, but my problem is minimal really; the value of the posted search category is an ID($row->id. What I am hoping to do is use the associated $row->name for display, but keep the id for value so my search function still works.
In other words, I'm hoping to do something like:
<?php echo $row->name; WHERE ID = $_POST['search_category']
Is there an easy way to do that in the above code, or will I need to add a special query at the top of my page, fetching the individual row name that matches the posted id?
Thanks!
EDIT: To simplify, I already have a query that returns row->id and row->name, which I use in a foreach loop to populate my option values and names. I simply need a way, or a line that I can add to my query to also get the value of the row->name that matches the POSTED id.
i would write a short function which gives this functionality even for others applications.
just like
<?php
function($id, $table) {
select ... etc
}
?>
For security Reasons I would suggest to use Prepared Statements or mysql_escape
Hope i could help
Perhaps
SELCET('id', 'name' FROM yourTable WHERE 'name' = $_POST['ID'])
you mean something like this or would you select the dropdown option
<?php foreach($categoriesListt as $row) : ?>
<option value="<?php echo $row->id; ?>"
<?php if($row->name == $_POST['search_category']) : ?>
selected="selected"<?php } ?>>
<?php echo $row->name; ?>
</option>
<?php endforeach; ?>
I have a couple of mysqli questions.
First. I know I can make groups into an array. I was just wondering if there was a way to get a row from the groups object without making it into an array?
$groups = $this->db->query("SELECT id, name FROM groups");
...
<select name="group">
<?php while($group = $groups->fetch_object()): ?>
<option value="<?php echo $group->id?>"><?php echo $group->name; ?></option>
<?php endwhile; ?>
</select>
...
<?php echo $groups[$user->group_id]; ?>
I know the final line won't work. Is there something like this $groups->fetch_row($group_id)->name?
My second question is to do with garbage collection. How much of a difference in a small application does it make if I free up a result as opposed to not? Instead of freeing up the result after each query could I close the database connection when the database class destructs. Would this have the same effect?
Use fetch_assoc():
<?php while($group = $groups->fetch_assoc()): ?>
<option value="<?php echo $group['id']; ?>"><?php echo $group['name']; ?></option>
<?php endwhile; ?>