I tried making dropdown readonly if the data exists in database. But when even if i make it readonly, user can change the values i mean dropdown values. so i changed it to disabled. but in this, i cannot receive the values after submission. Here is my code
<select name="sign" <?php echo empty($row['sign']) ? '' : 'readonly' ?> class="form-control">
<option value="<?php echo $row['sign']; ?>"><?php echo $row['sign']; ?></option>
<?php $m1 = mysqli_query($con, "SELECT * FROM signs");
while($m2 = mysqli_fetch_array($m1)) { ?>
<option value="<?php echo $m2['sign']; ?>"><?php echo $m2['sign']; ?></option>
<?php } ?>
</select>
how can i solve it?
i think the problem is this <?php echo empty($row['sign']) ? '' : 'readonly' ?>
maybe u can do this <?php echo (empty($row['sign']) ? '' : 'readonly'); ?>
One solution is when you make it disabled, to add another hidden input with the same name and value.
Other solution is to prevent user from changing the select, there are many javascript options, try this:
http://jsfiddle.net/qk2Pc/
HTML:
<select id="my_select">
<option>Option 1</option>
<option>Option 2</option>
</select>
JS:
my_condition = true;
var lastSel = $("#my_select option:selected");
$("#my_select").change(function(){
if(my_condition)
{
lastSel.attr("selected", true);
}
});
$("#my_select").click(function(){
lastSel = $("#my_select option:selected");
});
Related
I've try several times for dropdownlist validation,
my codes are in PHP and HTML, the the validation part is not working though and i've refer some of the solutions in stackoverflow which has similar case like mine.
Declare variable
$call_department = $db->escape((int)$_POST['call_department']); //where i declare this variable
HTML files
<tr><td>Department</td><td><select name='call_department'>
<option></option>
<?php $call_dept = $db->get_results("select type_id,type_name from site_types where type=1 order by type_name;");
foreach ($call_dept as $dept )
{?>
<option value='<?php echo $dept->type_id;?>'><?php echo $dept->type_name; required?></option>
<?php } ?>
</select></td></tr>
Validation part:
<?php
if(isset($_REQUEST['call_department']) && $_REQUEST['call_department'] == '0') {
echo 'Please select a department.';
}
?>
1) Set first option value="0" like this
<option value="0">select</option>
2) Required attribute should be set to select tag not to option tag <select name='call_department' required>
<select name='call_department' required>
<option value="0">select</option>
<?php $call_dept = $db->get_results("select type_id,type_name from site_types where type=1 order by type_name;");
foreach ($call_dept as $dept )
{
?>
<option value='<?php echo $dept->type_id;?>'><?php echo $dept->type_name; ?></option>
<?php } ?>
</select>
I have a problem which I'm not sure how to approach, I have a WP plugin which has a form, in the form I have a with result that could have varied amount of data (depending on what the user submits).
How can I add 'selected' to the selected item so when the user returns he can edit/view the item he selected?
<select name="supplier">
<option value="Supplier 1">Supplier 1</option>
<option value="Supplier 2">Supplier 2</option>
<option value="Supplier 3">Supplier 3</option>
<option value="Supplier 4">Supplier 4</option>
</select>
A loop comes to mind, shall I assign a number to each option? There may be 100 suppliers so it would need to count right?
I would use a loop like...
$theirChosenSupplier = $supplierVarFromDatabaseOrWherever;
?>
<select name="supplier">
foreach($allSuppliers as $individualSupplier) {
if($individualSupplier == $theirChosenSupplier) {
$selected = "selected";
} else {
$selected = "";
}
?><option value="<?php echo $individualSupplier; ?>" <?php echo $selected; ?>>
<?php echo $individualSupplier; ?>
</option>
<?php } ?>
</select>
This code isn't tested but should give you an idea. Apologies if I've misunderstood the question.
adding an id is always a good thing. Assuming this is wordpress generated and you don't have a loop already there is always the option of using javascript and maybe an onchange event.
I dont know if the form is submitted first or you do some other stuff but maybe:
<select id="foo">
<option id="provider_1">Provider 1</option>
<option id="provider_2">Provider 2</option>
</select>
<script type="text/javascript">
window.onload = function() {
var selectedValue = '<?php echo someEscapeFunction($_SESSION['id_provider']); // or _GET, _POST ?>';
document.getElementById('foo').selectedIndex = document.getElementById(selectedValue).index;
}
</script>
I have limited JavaScript knowledge and am trying to change a form so it autochanges when the drop down is selected without having to click a go button.
How would I amend the following code? There are 3 separate drop downs in there. I've tried inserting onselect in there but it didnt seem to work?
<select name="season">
<option value="0"><?= $txt_all ?></option>
<?php
while($data = mysql_fetch_array($get_seasons))
{
if($data['SeasonID'] == $defaultseasonid)
echo "<option value=\"$data[SeasonID]\" SELECTED>$data[SeasonName]</option>\n";
else
echo "<option value=\"$data[SeasonID]\">$data[SeasonName]</option>\n";
}
mysql_free_result($get_seasons);
?>
</select>
<select name="matchtype">
<option value="0"><?= $txt_all ?></option>
<?php
while($data = mysql_fetch_array($get_types))
{
if($data['MatchTypeID'] == $defaultmatchtypeid)
echo "<option value=\"$data[MatchTypeID]\" SELECTED>$data[MatchTypeName]</option>\n";
else
echo "<option value=\"$data[MatchTypeID]\">$data[MatchTypeName]</option>\n";
}
mysql_free_result($get_types);
?>
</select> <input type="submit" name="submit" value="Go"><td bgcolor="<?php echo $inside_c ?>" align="center">
Other Stats <br><select name="changeto">
<option value="1"><?= $txt_match_statistics ?></option>
<option value="2"><?= $txt_recordbook ?></option>
<option value="5"><?= $txt_opponent_list ?></option>
<option value="6"><?= $txt_this_day ?></option>
</select> <input type="submit" name="changepage" value="Go">
With jQuery you can listen your drop down list when it changes.
$('select').change(function(){
// Do stuff
});
If you want to submit your form immediately after changing drop down value you can do this:
$('select').change(function(){
$('form').submit();
});
In right context you can see example from here(and mess around with it!): http://jsfiddle.net/EB635/
i have a main page that i used in uploading tickets to db, i have Select field that i want to retain the value that the user selected prior to submitting the form but its not happening...
here is my code for select field:
<select name="XiD" id="XiD">
<option value="Blank" selected="selected">Please Select...</option>
<option value="AAA">AAA</option>
<option value="BBB">BBB</option>
<option value="CCC">CCC</option>
<option value="DDD">DDD</option>
<option value="EEE">EEE</option>
<option value="FFF">FFF</option>
</select>
just to add, this Xid is being posted prior to submitting
$XiD = $_POST['XiD'];
and i try to use this script:
UPDATE - changed from $_GET to $_POST but still not working...
<script type="text/javascript">
document.getElementById('XiD').value = "<?php echo $_POST['XiD'];?>";
</script>
my jquery version im using: jquery-ui-1.10.2 and jquery-1.9.1
Use jQuery like this.
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('select#XiD').val('<?php echo $_POST['XiD'];?>');
});
</script>
You are using PHP to generate HTML. There's absolutely no need to use a third language to dictate how that HTML should be!
<select name="XiD" id="XiD">
<option value="Blank"<?=$XiD=='Blank' ? ' selected="selected"' : ''?>>Please Select...</option>
<option value="AAA"<?=$XiD=='AAA' ? ' selected="selected"' : ''?>>AAA</option>
...
</select>
Or, even better, simplify your code with an array and avoid writing duplicate code that's hard to maintain:
<?php
$options = array(
'Blank' => 'Please Select...',
'AAA' => 'AAA',
'BBB' => 'BBB',
'CCC' => 'CCC',
'DDD' => 'DDD',
'EEE' => 'EEE',
'FFF' => 'FFF',
);
<select name="XiD" id="XiD">
<?php foreach(options as $k => $v){ ?>
<option value="Blank"<?php echo $XiD==$k ? ' selected="selected"' : ''?>>Please Select...</option>
<? } ?>
</select>
According to your problem, it seems like you are using POST method (because you are getting value to $XiD = $_POST['XiD'];) and using GET to retrieve value.
So, use document.getElementById('XiD').value = "<?php echo $_POST['XiD'];?>";
instead of
document.getElementById('XiD').value = "<?php echo $_GET['XiD'];?>";
I would create a function to check if the current GET value is the same as the option in the list and in that case, return the needed attribute to make it selected by default, which is selected="selected".
This should work:
<?php
function isSelected($value){
if($value == $_GET['XiD']){
return 'selected="selected"';
}else{
return '';
}
}
?>
<select name="XiD" id="XiD">
<option value="Blank" <? echo isSelected('Blank');?>>Please Select...</option>
<option value="AAA" <? echo isSelected('AAA');?>>AAA</option>
<option value="BBB" <? echo isSelected('BBB');?>>BBB</option>
<option value="CCC" <? echo isSelected('CCC');?>>CCC</option>
<option value="DDD" <? echo isSelected('DDD');?>>DDD</option>
<option value="EEE" <? echo isSelected('EEE');?>>EEE</option>
<option value="FFF" <? echo isSelected('FFF');?>>FFF</option>
</select>
After trying al this "solves" nothing work. Did some research on w3school before and remember there was explanation of keeping values about radio. But it also works for Select option. See here an example. Just try it out and play with it.
<?php
$example = $_POST["example"];
?>
<form method="post">
<select name="example">
<option <?php if (isset($example) && $example=="a") echo "selected";?>>a</option>
<option <?php if (isset($example) && $example=="b") echo "selected";?>>b</option>
<option <?php if (isset($example) && $example=="c") echo "selected";?>>c</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>
Im trying to implement the search feature in my website.
when the search keyword is entered in the textbox, and the category combo is selected, the form will be Posted and the result will be shown on the same page.
what i want is to keep the selected category of the combo by default in the form after posted
For eg., If i select the category 'Automobiles' in the combo and click search, after form submit, the combo should show the automobiles as default selected option. Please help me. Any help will be appreciated
I assume you get categories from database.
you should try:
<?php
$categories = $rows; //array from database
foreach($rows as $row){
if($row['name'] == $_POST['category']){
$isSelected = ' selected="selected"'; // if the option submited in form is as same as this row we add the selected tag
} else {
$isSelected = ''; // else we remove any tag
}
echo "<option value='".$row['id']."'".$isSelected.">".$row['name']."</option>";
}
?>
Assuming that by "combo" you mean "A regular select element rendering as a drop down menu or list box" and not "A combobox that is a combination of a drop down menu and free text input":
When outputting the <option> elements, check the value against the submitted data in $_POST / $_GET and output selected (in HTML) or selected="selected" (in XHTML) as an attribute of the option element.
Here is the JQuery way I am using.
<select name="name" id="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<script type="text/javascript">
$("#name").val("<?php echo $_POST['name'];?>");
</script>
But this is only if you have jquery included in your webpage.
Regards
<?php
$example = $_POST["friend"];
?>
<form method="POST">
<select name="friend">
<option value="tom" <?php if (isset($example) && $example=="tom") echo ' selected';?>>Thomas Finnegan</option>
<option value="anna" <?php if (isset($example) && $example=="anna") echo ' selected';?>>Anna Karenina</option>
</select>
<br><br>
<input type="submit">
</form>
This solved my problem.
This Solved my Problem. Thanks for all those answered
<select name="name" id="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<script type="text/javascript">
document.getElementById('name').value = "<?php echo $_GET['name'];?>";
</script>
$countries_uid = $_POST['countries_uid'];
while($row = mysql_fetch_array($result)){
$uid = $row['uid'];
$country = $row['country_name'];
$isSelected = null;
if(!empty($countries_uid)){
foreach($countries_uid as $country_uid){//cycle through country_uid
if($row['uid'] == $country_uid){
$isSelected = 'selected="selected"'; // if the option submited in form is as same as this row we add the selected
}
}
}else {
$isSelected = ''; // else we remove any tag
}
echo "<option value='".$uid."'".$isSelected.">".$country."</option>";
}
this is my solutions of multiple select dropdown box after modifying Mihai Iorga codes
After trying al this "solves" nothing work. Did some research on w3school before and remember there was explanation of keeping values about radio. But it also works for Select option. See here an example. Just try it out and play with it.
<?php
$example = $_POST["example"];
?>
<form method="post">
<select name="example">
<option <?php if (isset($example) && $example=="a") echo "selected";?>>a</option>
<option <?php if (isset($example) && $example=="b") echo "selected";?>>b</option>
<option <?php if (isset($example) && $example=="c") echo "selected";?>>c</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>
Easy solution:
If select box values fetched from DB then to keep selected value after form submit OR form POST
<select name="country" id="country">
<?php $countries = $wpdb->get_results( 'SELECT * FROM countries' ); ?>
<option value="">
<?php if(isset($_POST['country'])){echo htmlentities($_POST['country']); } else { echo "Select Country *"; }?>
</option>
<?php foreach($countries as $country){ ?>
<option <?php echo ($_POST['country'] == $country->country_name ? 'selected="selected"':''); ?> value="<?php echo $country->country_name; ?>"><?php echo $country->country_name; ?>
</option>
<?php } ?>
</select>