I have two table in database. in table their is my all divisions which are shown in dropdown options. In second table, i submit my division name in division field.
I want value in division field show as a default value in dropdown.here is my code..
here is my first query by which all division are comes
$all_customers = mysql_query("select * from `supp_customers`");
<select name="division" id="c_name" required class="form-control" onchange="my_function(this,<?php echo $users['id']; ?>)">
<option value="">All Divisions</option>
<?php while($customer = mysql_fetch_array($all_customers)){ ?>
<option value="<?php echo $customer['name']; ?>"><?php echo $customer['name']; ?></option>
<?php } ?>
</select>
and my second query
$pro_qry = mysql_query("SELECT * FROM `supp_average_notes` where `id`='$id'");
$div_query = mysql_fetch_array($pro_qry);
$dive_query['division'];
?>
i Want to set $div_query['division'] as a default in dropdown. How it can be possible
In your while loop, you should compare the division to the current division. If it's the same, select the option. I would recommend you to
do what #RiggsFolly says,
look into a PHP framework; it will take a lot of work out of your hands and add security, e.g. Laravel, Yii or Symfony,
use id's in your select option values instead of names,
use alternative code style in views like below,
improve your variable naming so the name states what it holds.
.
<?php foreach ($divisions as $division): ?>
<?php if ($division['id'] == $currentDivision['id']) $selected = ' selected="selected"'; else $selected = ''; ?>
<option <?= $selected ?> value="<?= $division['id'] ?>">
<?= $division['name'] ?>
</option>
<?php endforeach ?>
Related
I have a create.php which creates a user, and edit.php for editing information of the student.
I'm a beginner in PHP so if this might be a question that has a very easy method, please do tell.
I've been searching for retrieving a value that has already been set on a dropdown, but the only things I've found are:
a.) They only print the selected value, not retrieve the already-set option.
sample code:
<p>Schedule</p>
<select value="<?= $data->schedule; ?>" name = "schedule">
<?php
$schedule = array ('Morning', 'Afternoon');
foreach ($schedule as $sched) { ?>
<option value="<?php echo $sched; ?>"><?php echo $sched; ?></option>
</select>
<?php } ?>
b.) I have no knowledge of javascript, I tried it, but as I have no knowledge of it, I can't seem to make it work.
sample codes I tried:
<p>Schedule</p>
<select id = "my_select" name = "schedule">
<option id = "o1" id="id1">Morning</option>
<option id = "o2" id="id2">Afternoon</option>
</select>
<script type="text/javascript">
$("#my_select").change(function() {
var id = $(this).find('option:selected').attr('id');
});
</script>
I also tried
document.getElementById('schedule').selectedOptions[0].text
but still does not output "afternoon"
This is my last resort as I have no other resource to find.
I added a picture of what I wanted to do since my question might be a little confusing.
Selects use selected attribute not the value to define the selected state.
So in your code check the value and then apply selected.
<p>Schedule</p>
<select name="schedule">
<?php foreach (['Morning', 'Afternoon'] as $sched): ?>
<option value="<?= $sched ?>"<?= ($data->schedule == $sched ? ' selected' : null) ?>><?= $sched ?></option>
<?php endforeach ?>
</select>
I am a complete novice but trying to design my first site in Wordpress. I am trying to retain the value of a dropdown that pulls values from a database after a form is submitted. I have searched many questions and tried many different ways of trying to write selected="selected" in my code but none of them seem to work. Please help.
Here is my code that just pulls from the database but isn't trying to retain the selected value:
<select name = "box1" class="searchbox">
<option value = "">All Values</option>
<?php
global $wpdb;
$ddresult = $wpdb->get_results("SELECT Field1 FROM pc_table ORDER BY Field1 ASC");
foreach($ddresult as $ddrow) {
?>
<option value="<?php echo $ddrow->Field1; ?>"><?php echo $ddrow->Field1; ?> </option>
<?php
} ?>
</select>
How can I add the code to keep the value selected after form submit? Any help much appreciated, thank you.
Try this. It checks if box1 was submitted. Then it compares that value to the items in the options loop. A match will set $selected to the correct attribute, otherwise it will stay an empty string by default. (NB: if form is using get method, then change $POST to $_GET)
<select name = "box1" class="searchbox">
<option value = "">All Values</option>
<?php
global $wpdb;
$ddresult = $wpdb->get_results("SELECT Field1 FROM pc_table ORDER BY Field1 ASC");
foreach($ddresult as $ddrow) {
$selected = '';
if(isset($_POST['box1'])){
if($ddrow->Field1==$_POST['box1']){ $selected = 'selected="selected"'; }
}
?>
<option value="<?php echo $ddrow->Field1; ?>" <?php echo $selected; ?>><?php echo $ddrow->Field1; ?> </option>
<?php
} ?>
</select>
This example of course does not include if you want to save the submitted value to the database and then re introduce it in the output at a later time. If that's what you would like to know, then please leave a comment.
I am new to PHP and trying to make dynamic list box using SQL
<label> Select Sport</label>
<select name = "Sport">
<option value = "">Select Sport</option>
<?php
$all = "SELECT * FROM EventTable";
$result = $pdo->query($all);
foreach($result as $Sport){
?>
<option value ="<?php echo $Sport['Sport']; ?>"></option>
<?php
}
?>
</select>
But its printing BLANK space
Try this:
<select name="">
foreach($result as $Sport){
?>
<option value ="<?php echo $Sport['Sport']; ?>"><?php echo $Sport['Sport']; ?></option> // The value between <option> value </option> is the value which is to be shown in the dropdown, without this it is showing blank
<?php
}
</select>
You probably shouldn't use SELECT *, but since you're learning, I digress. Also, I personally have a hard time reading a lot of opening and closing php tags scattered throughout plus they often give weird results than what you're expecting, so this is how I'd write it.
<?php
$all = "SELECT * FROM EventTable";
$result = $pdo->query($all);
foreach($result as $sport)
{
echo "<option value =" . $sport['Sport'] . ">" . $sport['Sport'] . "</option>";
}
?>
The blank spaces indicates that you are actually hitting the loop and iterating, but the values for that array item are null. Are you sure the field name isn't 'sport' instead of 'Sport'?
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>
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; ?>