Hy there
I have some selecboxes on my site like this one:
<div class="registrationdate">
<select class="filter registrationdate" name="rdat" id="regdate">
<option value="">---</option>
<option value="2012">at 2012</option>
<option value="2011">at 2011</option>
<option value="2010">at 2010</option>
</select>
</div>
now i got a problem after submit the form...
if "at 2011" is selected after submit i get just "2011", the value, but i want the text.
after refresh the select boxes change the text to "at 2011"
in source i can see the problem, but i don't know how to solve it.
after submit the form and i land on the same page the source of the selectbox above is altered like this:
<div class="registrationdate">
<select class="filter registrationdate" name="rdat" id="regdate">
<option value="2012">2012</option>
<option value="">---</option>
<option value="2012">at 2012</option>
<option value="2011">at 2011</option>
<option value="2010">at 2010</option>
</select>
</div>
so can here some help me? i hope i expressed me understandable.
thanx for help!
EDIT
OK, i think i have found the Problem in the code of my coworker... but i encounter another problem, the same what he wanted to solve.
This is the actual code:
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' name='filterform' method='get' class="filterform">
<div class='registrationdate'>
<strong><?php echo $GLOBALS['Lng']['filter_Erstzulassung']; ?>:</strong>
<select id="regdate" name="rdat" class="filter registrationdate">
<?php if (isset($_GET['rdat'])&&$_GET['rdat']!='') echo '<option value="'.$_GET['rdat'].'">'.$_GET['rdat'].'</option>'; ?>
<option value="">---</option>
<?php for ($i = date(Y); $i >= (date(Y)-7); $i-- ) {
echo '<option value="'.$i.'">ab '.$i.'</option>';
}?>
</select>
</div>
<input type="submit" value="<?php echo $GLOBALS['Lng']['apply']; ?>" name="submit" id="submit" class="field">
</form>
The Problem is that he want the entered option visible again after submit the form. In normal case if you send a form u got the default values. he tried to overwrite this with the php part "isset then echo".
AFAIK PHP you can't get the Text part of an select field, or?
What could he else to solve this?
If you wish to get "at 2011" you should set value of the option to
<option value="at 2011">at 2011</option>
As far as I understood, you want to keep value attribute, but get the text inside option tag.
$("#regdate option[value='2011']").text() will return "at 2011"
http://jsfiddle.net/BtNNT/5/
Related
I want to validate select option but it's not working for me
this is my source code
<?php echo form_open_multipart('dashboard/add_item') ?>
<select name="provinsi_id" id='propinsi_id' required>
<option value="0">-Select Category-</option>
<option value="1">Graphics</option>
<option value="2">Fonts</option>
<option value="3">Prints Template</option>
</select>
<button type="submit" class="button big dark">Next<span class="primary"> Step</span></button>
<?php echo form_close(); ?>
required on a <select> will reject if the selected option has no value.
Try setting the value on the first option to "" instead of "0".
<option value="">-Select Category-</option>
Lets say i input data into a mysql database using a drop down like:
<div>
<input name="name" type="text" id="name" placeholder="Enter Name"></div>
<div>
<select name="position" id="position" type="text">
<option selected="selected">Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
With a normal text field, i can simply include the value field to show on the form the initially entered data in the table. And change that so when i submit form, it updates the related field.
<div>
<select name="position" id="position" type="text" value="<?php echo $row_position['Position']; ?>>
<option selected="selected">Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
So assuming i do not want to update the field relating to the drop down, how do i it? Because what happens is, once i submit the form without selecting any option from the drop down, it picks the first option from the list which is the "Select" option and uses that to update the related field in the database.
I'd suggest at least two steps to make it user-friendly and achieve your goals:
First, make the initially entered selection selected if available:
<?php
$positions = Array("A", "B", "C");
?>
<div>
<select name="position" id="position">
<option value="-1">Select</option>
<?php
foreach($positions as $v) {
$selected = ($row_position['Position']===$v) ? "selected" : "";
echo "<option value='$v' $selected>$v</option>";
}
?>
</select>
</div>
And on the receiving php-script check if you've received a valid option, otherwise send error-message:
<?php
//other stuff...
if($_POST['position']>0) {
// save that value
} else {
// send error to user
}
// even more stuff..
?>
Notes: in select tag there is no type=text nor a value=anything available.
Assuming you are fetching dropdown options from database.
Note: Array keys, variables are only for demonstration purposes. This may differ from your actual records.
<div>
<select name="position" id="position">
<?php foreach($options as $option): ?>
<option value="<?= $option['position'] ?>" <?php if($option['position'] == $current_record['position']) { echo "selected"; } ?>> <!-- e.g. $option['id'] -->
<?= $option['name'] ?>
</option>
<? endforeach; ?>
</select>
</div>
What i have done that has solved the issue was to simply add the value from the database to the selectedoption or the first option field. So by default without changing the drop down, the first option with the value from the database is selected. And so there would be no change.
<div>
<div><?php echo $row_position['Position']; ?></div>
<select name="position" id="position">
<option value="<?php echo $row_position['Position']; ?>Select to Change</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
This gives a simple or straight forward solution to the problem.
Will still try the other suggestions to see how it all plays out. Thanks guys.
I'm Beginner on PHP, Javascript, I would like to get selected value from select/option and stock it into a php variable ( $varpeoplesnames ), to use it in a future SQL request and show it with echo
my code:
<select name="peoplesnames">
<option value="1">john</option>
<option value="2">sarah</option>
<option value="3">samantha</option>
</select>
<?php (....) echo $varpeoplesnames;
How can I get the text of the selected option to show it with echo in php?
Wrap it in a form with action being the link to your php file and a submit button
HTML:
<form action='yourPhpFile.php' method='GET'>
<select name="peoplesnames">
<option value="1">john</option>
<option value="2">sarah</option>
<option value="3">samantha</option>
</select>
<input type='submit'></input>
</form>
PHP
<?php
$varpeoplesnames = $_GET['peoplesnames'];
echo $varpeoplesnames;
?>
I'm trying to use PHP to get the current index of my dropdown box.
<select name="iface" id="iface">
<option>vlan10</option>
<option>br0</option>
<option>br1</option>
<option>eth3</option>
</select>
The options are set from a loop that grabs interfaces from a server. I need to get the currently selected index if that is possible.
<form method="post">
<select name="iface" id="iface">
<option>vlan10</option>
<option>br0</option>
<option>br1</option>
<option>eth3</option>
</select>
<input type="submit" value="Print selected option"/>
</form>
<?php
if (isset($_POST['iface']))
{
echo $_POST['iface'];
}
?>
For example, the selected interface is "eth3".
So, just render with php:
<option selected="selected">eth3</option>
for indexing you render your list like this:
<select name="iface" id="iface">
<option value="0">vlan10</option>
<option value="1">br0</option>
<option value="2">br1</option>
<option value="3" selected="selected">eth3</option>
</select>
Now iface.value (or $_POST['iface']) = 3
I have dropdown list (menu) and a button created with this code:
<form name="period" action="all.php" method="POST">
<div align="center">
<select name=period_dropdown>
<option value="nill"></option>
<option value="48">48</option>
<option value="72">72</option>
<option value="96">96</option>
<option value="120">120</option>
</select>
<input type="submit" value= "OK" >
</div></form>
When option of dropdown is selected, must be on button instead of OK and when button is pressed to be assigned to variable $period1. So, when I select 72, button must have 72 instead of OK and when I click button, variable $period1 to get value 72 (integer). Please, no javascript. Just html and php.
Thanks
You have to use javascript for what you are trying to do. If you want the button to change on the fly without submitting the form, you have to do client-side scripting (javascript).
Either:
Change the value of button after the dropdown is selected and form is submitted
Use two lines of javascript to change the value of the button when the select box is changed
In all.php:
if (isset($_POST['period_dropdown'])) {
$period1 = $_POST['period_dropdown'];
//do something with $period1
}
?>
<form name="period" action="all.php" method="POST">
<div align="center">
<select id="period_dropdown" name="period_dropdown" onchange="updateButton()">
<option value="nill"></option>
<option value="48">48</option>
<option value="72">72</option>
<option value="96">96</option>
<option value="120">120</option>
</select>
<input id="period_button" type="submit" value= "OK" >
</div></form>
<script type="text/javascript">
function updateButton() {
document.getElementById("period_button").value = document.getElementById("period_dropdown").value;
}
</script>