I have a view with a dropdown list using select tag and I want to how can i have an event or an onchange event after selecting a value in my dropdown.
here is my code in index.phtml
<?php if (count($users)): ?>
<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>E-mail</th>
<th>Level</th>
<th></th>
</tr>
</thead>
<?php foreach ($users as $user): ?>
<tr>
<td><?php echo $this->escapeHtml($user->first_name . ' ' . $user->last_name); ?></td>
<td><?php echo $this->escapeHtml($user->email); ?></td>
<td>
<select name="level">
<?php $level = $this->escapeHtml($user->level); ?>
<?php if ($level == 1): ?>
<option value="1" selected="selected">Administrator</option>
<option value="2">Manager</option>
<option value="3">HR Staff</option>
<?php elseif ($level == 2): ?>
<option value="1" >Administrator</option>
<option value="2" selected="selected">Manager</option>
<option value="3">HR Staff</option>
<?php elseif ($level == 3): ?>
<option value="1" >Administrator</option>
<option value="2" >Manager</option>
<option value="3" selected="selected">HR Staff</option>
<?php endif; ?>
</select>
</td>
<td style="text-align: right;">
<i class="icon-remove icon-white"></i>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php else: ?>
<h3>There are no registered users available.</h3>
<?php endif; ?>
Thanks in Advance
create javascript function and put function in select box onchange event.
check working example on fiddle
<script>
function changeTest(obj){
alert(obj.options[obj.selectedIndex].value);
}
</script>
<select name="level" onChange="changeTest(this)">
<option value="1" selected="selected">Administrator</option>
<option value="2">Manager</option>
<option value="3">HR Staff</option>
</select>
Related
How to replace radio button by drop down select in wordpress meta box field ??
<tr valign="top">
<th scope="row"><label for="effectv">Select effect</label></th>
<td>
<input type="radio" id="effectv" name="b26slider_options[effectv]" <?php if($settings['effectv'] == 'zoomOut') echo 'checked="checked"'; ?> value="zoomOut" />zoomOut
<input type="radio" id="effectv" name="b26slider_options[effectv]" <?php if($settings['effectv'] == 'zoomIn') echo 'checked="checked"'; ?> value="zoomIn" />zoomIn
<input type="radio" id="effectv" name="b26slider_options[effectv]" <?php if($settings['effectv'] == 'panUp') echo 'checked="checked"'; ?> value="panUp" />panUp
</td>
</tr>
Please try this and let me know it's work or not.
<tr valign="top">
<th scope="row"><label for="effectv">Select effect</label></th>
<td>
<select name="b26slider_options[effectv]" id="effectv">
<option value="zoomOut" <?php echo ($settings['effectv'] == "zoomOut") ? 'selected' : ''; ?>>zoomOut</option>
<option value="zoomIn" <?php echo ($settings['effectv'] == "zoomIn") ? 'selected' : ''; ?>>zoomIn</option>
<option value="panUp" <?php echo ($settings['effectv'] == "panUp") ? 'selected' : ''; ?>>panUp</option>
</select>
</td>
</tr>
Just simply replace them with the correct HTML.
<tr valign="top">
<th scope="row"><label for="effectv">Select effect</label></th>
<td>
<select name="select">
<option value="value1">Value 1</option>
<option value="value2" selected>Value 2</option>
<option value="value3">Value 3</option>
</select>
</td>
Click here for more information.
I am trying to get the value from a selectbox. The form is placed multiple times on my page with a php loop (that's why I use classes instead of id's).
<html>
<body>
<table>
<form class="lesmaker">
<tr>
<td colspan="5">
<select name="type" class="type">
<option value="open">Open vraag</option>
<option value="info">Informatie</option>
<option value="discussie">Klassikaal</option>
<option value="meerkeuze" selected="selected">Meerkeuze</option>
</select>
</td>
</tr>
<tr>
<td colspan="5">
<select name="media" class="media">
<?php foreach($images as $image){if(substr($image,0,1) != '.'){ ?>
<option style="background-image:url(<?=$d_images.$image?>)" value="<?=$image?>" <?php if($r_media== $image) echo 'selected="selected"'; ?>><?=$image?></option>
<?php }} ?>
</select>
</td>
</tr>
</form>
</table>
<table>
<form class="lesmaker">
<tr>
<td colspan="5">
<select name="type" class="type">
<option value="open">Open vraag</option>
<option value="info">Informatie</option>
<option value="discussie">Klassikaal</option>
<option value="meerkeuze" selected="selected">Meerkeuze</option>
</select>
</td>
</tr>
<tr>
<td colspan="5">
<select name="media" class="media">
<?php foreach($images as $image){if(substr($image,0,1) != '.'){ ?>
<option style="background-image:url(<?=$d_images.$image?>)" value="<?=$image?>" <?php if($r_media== $image) echo 'selected="selected"'; ?>><?=$image?></option>
<?php }} ?>
</select>
</td>
</tr>
</form>
</table>
</body>
</html>
With jQuery I am trying to get the selected option.
$(function(){
$('.lesmaker').each(function() {
console.log($(this).find('.type').val());
});
});
The code above is not working. How do I need to do this?
$(function() {
$('.lesmaker').each(function() {
console.log($(this).find('table tr td').find('.type').val());
});
});
You need to change your HTML to following:
<form class="lesmaker">
<table>
<tr>
<td colspan="5">
<select name="type" class="type">
<option value="open">Open vraag</option>
<option value="info">Informatie</option>
<option value="discussie">Klassikaal</option>
<option value="meerkeuze" selected="selected">Meerkeuze</option>
</select>
</td>
</tr>
</table>
</form>
form needs to be either outside of table or inside of td :)
$(function() {
$('.type').each(function() {
console.log($('option:selected', this).val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<form class="lesmaker">
<tr>
<td colspan="5">
<select name="type" class="type">
<option value="open">Open vraag</option>
<option value="info">Informatie</option>
<option value="discussie">Klassikaal</option>
<option value="meerkeuze" selected="selected">Meerkeuze</option>
</select>
</td>
</tr>
<tr>
<td colspan="5">
<select name="type" class="type">
<option value="open">Open vraag</option>
<option value="info">Informatie</option>
<option value="discussie" selected="selected">Klassikaal</option>
<option value="meerkeuze" >Meerkeuze</option>
</select>
</td>
</tr>
<tr>
<td colspan="5">
<select name="type" class="type">
<option value="open">Open vraag</option>
<option value="info" selected="selected">Informatie</option>
<option value="discussie" >Klassikaal</option>
<option value="meerkeuze" >Meerkeuze</option>
</select>
</td>
</tr>
</form>
</table>
Just use the class of select and use $('option:selected', this).val()
<!DOCTYPE html>
<html lang="ro">
<head>
<title>MODIFICA</title>
</head>
<body>
<?php
$select1 = isset($_POST['select1']) ? $_POST['select1'] : '';
if($select1 == 'da'){echo 'ok';}
else { echo '!ok'; }
?>
<form method="POST">
<table border="1">
<thead>
<tr>
<th>Nume</th>
<th>Prenume</th>
<th>Adresa</th>
<th>Partid</th>
<th>Consiliul Local</th>
<th>Primar</th>
<th>Consiliul Judetean</th>
<th>Presedinte Consiliul Judetean</th>
<th>Senat</th>
<th>Deputat</th>
<th>Comisia Europarlamentara</th>
<th>Presedinte</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<?php
$stmt = $db->prepare('SELECT nume,prenume,adresa,partid,consiliul_local FROM candidati');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_OBJ)):?>
<tr>
<td><?php echo $row->nume;?></td>
<td><?php echo $row->prenume;?></td>
<td><?php echo $row->adresa;?></td>
<td><?php echo $row->partid;?></td>
<td>
<select name="select1">
<option value=""></option>
<option value="<?php echo $row->consiliul_local;?>">
<?php echo $row->consiliul_local;?>
</option>
</select>
</td>
<td>
<select name="select2">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<select name="select3">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<select name="select4">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<select name="select5">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<select name="select6">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<select name="select7">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<select name="select8">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<button type="submit" name="btn">SUBMIT</button>
</td>
</tr>
<?php endwhile;?>
</tbody>
</table>
</form>
</body>
</html
I just verify if the $select1 variable is set, and if it's the case, then if the value from "option" (line 44), is 'da', then display it.
The problem that I have is that value is not displayed accordingly. Anyone knows why, and what should I change in order to work?
Or, in simple words, for starting coding the page I need, firstly I just filled-in that "option" tag with the value from db (and yes, the value 'da' is presented in 'consiliul_local' field from db), and then I made a simple echo, above the "form", and if the value of the "select" is 'da', then it should display the message 'ok'. But it always displays the message placed on the else branch. I must specify that I must do the whole logic only with php, and not js or anything else.
Here is how my page looks like, and notice that selected 'da' in the dropdown, which is there because of the MySQL fetch (line 35 in pastebin).
http://www.2shared.com/photo/eXzJ7Glo/1_online.html
This is the page source, which says the value stored in db, is correctly inserted into that value attribute of the tag.
http://www.2shared.com/uploadComplete.jsp?sId=CACRC8H4n6GrF0jJ
So I select 'da' from that dropdown, then I press the button, and I expect the 'ok' message to be displayed. Any fix? Thanks so much!
There is two drop-down list having different values and a submit button. after submiting it the action is on the same page with $_SERVER['PHP_SELF']; now i want to show the selected dropdown value after the report is generated but i cant figure out how to do that.
<form name="gg" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table align="center">
<tr>
<th>
<label>Center Name:</label>
</th>
<td>
<select name="center_name" id="centername" required >
<option value="">Select Center</option>
<option value="xxx">XXX</option>
</select>
</td>
</tr>
<tr>
<th>
Age:
</th>
<td>
<select name="age_bracket" id="agebracket" required >
<option value="" >Select Age</option>
<option value="18-24" >18-23</option>
<option value="25-34" >25-34</option>
<option value="35-44" >35-44</option>
<option value="45-54" >45-54</option>
<option value="55-64" >55-64</option>
<option value="65-74" >65-74</option>
<option value="75" >75+</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Submit"></td>
</tr>
<?php
//db connection goes here
echo "<table style='width:70%' table border='1' style='table-layout:fixed' align='center'>";
echo "<tr>
<th>No</th>
<th>col1</th>
<th>col2</th>
<th>col3</th>
<th>col4</th>
</tr>";
if(isset($_POST['submit'])) {
//processing request here
//echo fetched rows
result comes like this
centername:-
age:-
submit
slno col1 col2 col3 col4
//after submit i get the report fetched here on the same page but could not get the selected drop-down values
I don't understand your question very well... I have read your comments... I think you want something like this?
<form name="gg" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table align="center">
<tr>
<th>
<label>Center Name:</label>
</th>
<td>
<select name="center_name" id="centername" required >
<option value="">Select Center</option>
<option value="xxx"<?php if(isset($_POST["center_name"]) && $_POST["center_name"] == "xxx") { echo " selected"; } ?>>XXX</option>
</select>
</td>
</tr>
<tr>
<th>
Age:
</th>
<td>
<select name="age_bracket" id="agebracket" required >
<option value="" >Select Age</option>
<option value="18-24"<?php if(isset($_POST["age_bracket"]) && $_POST["age_bracket"] == "18-24") { echo " selected"; } ?>>18-23</option>
<option value="25-34"<?php if(isset($_POST["age_bracket"]) && $_POST["age_bracket"] == "25-34") { echo " selected"; } ?>>25-34</option>
<option value="35-44"<?php if(isset($_POST["age_bracket"]) && $_POST["age_bracket"] == "35-44") { echo " selected"; } ?>>35-44</option>
<option value="45-54"<?php if(isset($_POST["age_bracket"]) && $_POST["age_bracket"] == "45-54") { echo " selected"; } ?>>45-54</option>
<option value="55-64"<?php if(isset($_POST["age_bracket"]) && $_POST["age_bracket"] == "55-64") { echo " selected"; } ?>>55-64</option>
<option value="65-74"<?php if(isset($_POST["age_bracket"]) && $_POST["age_bracket"] == "65-74") { echo " selected"; } ?>>65-74</option>
<option value="75"<?php if(isset($_POST["age_bracket"]) && $_POST["age_bracket"] == "75") { echo " selected"; } ?>>75+</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
To get the select values you can simply:
<?php
echo $_POST['center_name'];
echo $_POST['age_bracket'];
?>
You can remove <?php echo $_SERVER['PHP_SELF']; ?> from action as well.
<?php
echo "centername:".$_POST['center_name'];
echo "Age:" $_POST['age_bracket'];
?>
<form name="gg" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table align="center">
<tr>
<th>
<label>Center Name:</label>
</th>
<td>
<select name="center_name" id="centername" required >
<option value="">Select Center</option>
<option value="xxx">XXX</option>
</select>
</td>
</tr>
<tr>
<th>
Age:
</th>
<td>
<select name="age_bracket" id="agebracket" required >
<option value="" >Select Age</option>
<option value="18-24" >18-23</option>
<option value="25-34" >25-34</option>
<option value="35-44" >35-44</option>
<option value="45-54" >45-54</option>
<option value="55-64" >55-64</option>
<option value="65-74" >65-74</option>
<option value="75" >75+</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Submit"></td>
</tr>
</form>
i have two tables as shown, when employee edits its details department_id save as 0 in database
employee department
id| name|department_id id|department_name
1 |abc |1 1|HR
2 |xyz |4 2|Finance
3 |asd |3 3|Developer
and this view part
<tr>
<th> Department </th>
<td>
<SELECT name="department_id" style="width:180px;" >
<?php
foreach($departments as $row): ?>
<option value="<?php $employee['department_id'] ;?>"
<?php if ($row['id']== $stakehholder['department_id'])
{
echo 'selected';
}
?>>
<?php echo $row['department_name'];?>
</option>
<?php endforeach ?>
</SELECT>
</td>
<td> <?php echo form_error('department_id'); ?> </td>
</tr>
after edit 0 value get store in database
forget echo <option value="<?php $employee['department_id']; ?>"
<SELECT name="department_id" style="width:180px;" >
<?php foreach($departments as $row): ?>
<option value="<?php echo $row['department_id']; ?>"
<?php if ($row['department_name']== $stakehholder['department_id']) {
echo 'selected';
}
?>>
<?php echo $row['department_name']; ?>
</option>
<?php endforeach ?>
</SELECT>
updated line is in as below
<option value="<?php echo $employee['department_id'] ;?>
<tr>
<th> Department </th>
<td>
<SELECT name="department_id" style="width:180px;" >
<?php
foreach($departments as $row): ?>
<option value="<?php echo $employee['department_id'] ;?>"
<?php if ($row['department_name']== $stakehholder['department_id'])
{
echo 'selected';
}
?>>
<?php echo $row['department_name'];?>
</option>
<?php endforeach ?>
</SELECT>
</td>
<td> <?php echo form_error('department_id'); ?> </td>
</tr>
you have forgot to echo department_id in option value attribute and second you are comparing name to id which never match if id is int and name not.
try this updated code:
<SELECT name="department_id" style="width:180px;" >
<?php
foreach($departments as $row): ?>
<option value="<?php echo $row['department_id'] ;?>"
<?php if ($row['department_id']== $stakehholder['department_id'])
{
echo 'selected';
}
?>>
<?php echo $row['department_name'];?>
</option>
<?php endforeach ?>
</SELECT>