You guys have been patient with me and very helpful so far, so I'll try another one. I have looked around on here and tried many different options of select="selected" and I just can't get the code right or something, a simple fix I'm sure for you guys. What I'm trying to do is have a drop down list that is populated from a table of countries, default to the United States. Here is the code:
<select name="cnt" id="">
<option value="">Select</option>
<?php
$rescnt=mysql_query("select * from `country` ORDER by cnt_name ASC");
while($rowcnt=mysql_fetch_array($rescnt)) {
?>
<option value="<?php echo $rowcnt['cnt_id']; ?>"><?php echo $rowcnt['cnt_name']; ?></option>
<?php } ?>
</select>
Assuming the cnt_id for the United States is 'US', the option line should be:
<option value='<?php echo $rowcnt['cnt_id']; ?>'<?php if($rowcnt['cnt_id']=='US') echo ' selected'; ?>><?php echo...
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 have edit form where I get info from database
<select name="table">
<?php
//fetch all tables from database
$user = $con->query("SELECT * FROM table") or die(mysql_error());
while($row = $table->fetch_object()) { ?>
<option value="<?php echo $row->tablename;?>">
<?php echo $row->tablename; ?>
</option> <?php }?> </select>
<label for="time">Time :</label>
<select name="time">
<option value="twotothree">2PM-3PM</option>
<option value="threetofour">3PM-4PM</option>
<option value="fourtofive">4PM-5PM</option>
<option value="fivetosix">5PM-6PM</option>
</select>
The names of the tables from drop-down menu are different. Everytime I select a table and a time slot and save the data, the selection goes back to the first row of the menu. Eg I select table 3 and 4PM-5PM after saving it goes back to table 1 and 2PM-3PM. I need to be fixed on the last selection as I might use 4PM-5PM for table 4 also. Any idea? Thanks
You can add selected attribute when rendering your select list, depending on $_POST variable, when it's available. For example for you table select element:
<select name="table">
<?php
$user = $con->query("SELECT * FROM table") or die(mysql_error());
while($row = $table->fetch_object()) { ?>
<option value="<?php echo $row->tablename;?>" <?php if (isset($_POST['table']) && $_POST['table'] == $row->tablename) echo 'selected'; ?> >
<?php echo $row->tablename; ?>
</option>
<?php }?>
</select>
In similar way you can do for time select element.
I don't know if it is possible ...
But basically in my drop down list i want to have something of the sort:
[$value_________________$value] _ = space
Hope that is clear enough ... i know how to make the whole text go to the right but just wondering if there is a trick or something idk.
Currently my code is below:
<select name="lesson_type" id="lesson_type">
<option value="" disabled="disabled" selected="selected">Please select a module type.</option>
<?php
while($row=mysqli_fetch_array($result)){
?>
<option value="<?php echo $row['module_code'];?>">
<?php echo $row['module_name'];
echo $row['module_code'];?>
<option>
<?php
}
?>
</select><br />
<?php
$sql="select software from software_table";
$qry=mysql_query($sql);
$count=mysql_num_rows($qry);
$row=mysql_fetch_array($qry);
$data=['software_name'];
?>
<?php
for($i=0; $i<=count($data); $i++){
?>
<select name="software_name">
<option vale="<?php echo $data[$i]?>">$data</option>
}
?>
`I dont know if this is possible, but what I want to happen is that, when I add new software from software_table. My drop-down menu for software will get its option value from the database to become updated. I am thinking to use For-Loop for this, but I dont know how to start or is it really possible.
example.
in my drop-down menu. i have 3 option value.
1.MS OFFICE
2.AUTODESK
3.PRIMAVERA
and then I add another software from the database which is WINDOWS 8.
I want my drop-down menu to have those 4 value.
Try to use the following code -
$sql="select software from software_table";
$qry=mysql_query($sql); ?>
<select name="software_name">
<?php while($row=mysql_fetch_array($qry)) {
$data=$row['software_name']; ?>
<option vale="<?php echo $data; ?>"><?php echo $data; ?></option>
<?php }
?>
</select>
<select name="software_name">
<?php
$sql="select software from software_table";
$qry=mysql_query($sql);
while ($row=mysql_fetch_array($qry))
{
// In your case, each row contains only one value: $row[0] = the value of software field
echo "<option vale=\"$row[0]\">$row[0]</option>";
}
?>
</select>
This will generate the HTML code as:
<select name="software_name">
<option vale="MS OFFICE">MS OFFICE</option>
<option vale="AUTODESK">AUTODESK</option>
<option vale="PRIMAVERA">PRIMAVERA</option>
</select>
Im using osclass for a local classifieds website and im facing the following problem. I need to import 12 regions and 7000 cities/villages.
In the main.php im using a horizontal search bar with fields search text, categories (dropdown), Regions (dropdown), City (dropdown), Max price(text) and min Price (text)
For the Regions and Cities im using the code in inc.search.php
<?php $aRegions = Region :: newInstance()->listAll();?>
<?php if (count($aRegions) > 0) {?>
<select name="sRegion" id="sRegion">
<option value="">Select a Region</option>
<?php foreach ($aRegions as $region) {?>
<option value="<?php echo $region['s_name'];?>"><?php echo $region['s_name'];?> </option>
<?php } ?>
</select>
<?php } ?>
<?php $aCities = City::newInstance()->listAll(); ?>
<?php if(count($aCities) > 0 ) { ?>
<select name="sCity" id="sCity">
<option value="">Select a city</option>
<?php foreach($aCities as $City) { ?>
<option value="<?php echo $City['s_name'] ; ?>"><?php echo $City['s_name'] ; ?></option>
<?php }?>
</select>
<?php }?>
The problem is that the above code brings all cities when page loads and does not check what region is selected first. That means that the city dropdown will be filled with 7000 cities/villages when page loads.
I tried to remove lines
<?php foreach($aCities as $City) { ?>
<option value="<?php echo $City['s_name'] ; ?>"><?php echo $City['s_name'] ; ?></option>
<?php }?>
so when page loads, the city dropdown is empty but i dont know how to fill the dropdown with cities depend on region select.
The solution to how to use region/city in search form is described here http://forums.osclass.org/jobs/cities-dropdown-based-on-region-select/