<select>
<?php foreach($result as $city) { ?>
<option value="<?php echo $city->city_name; ?>" <?php if( strtolower($this->session->city) == strtolower($city->city_name) ) { echo "selected"; } ?>
> <?php echo $city->city_name; ?> </option>
<?php } ?>
</select>
Hi i am trying to auto select drop down box with the help of session but i am not able to select please some one help me out how could i auto select drop down box.
First of all. You should always provide the whole code, eg. in format of PHPFiddle.
Secondly, you should not mix HTML and PHP.
Also your problem cannot be deciphered from your question, because there might be several reasons. Like the city name could be in uppercase or capitalized in your session variable.
Your session variable might not be initialized with your session in the class constructor.
Also you might even not have a class at all, so the reference to $this would be obsolete.
But those aside, here's a code that might help you:
<?php
class MockSession {
function __construct() {
}
}
class Foo {
function __construct() {
$this->session = new MockSession();
$this->session->city = "Helsinki";
}
function printSelect() {
$result = array("London","New York","Helsinki");
echo '<select>';
foreach ($result as $city) {
$sel = '';
echo $city." ".$this->session->city;
if (strtolower($city)==strtolower($this->session->city)) {
$sel = ' selected="selected" ';
}
echo '<option value="'.$city.'" '.$sel.'>'.$city.'</option>';
}
echo '</select>';
}
}
$foo = new Foo();
$foo->printSelect();
?>
If you got value of your session on the page then you have to just modify your code as selected="selected". See updated code:
<select>
<?php
foreach ($result as $city) {
?>
<option
value="<?php echo $city->city_name; ?>"
<?php if (strtolower($this->session->city) == strtolower($city->city_name)) {
echo 'selected="selected"';
}
?>>
<?php echo $city->city_name; ?>
</option>
<?php
}
?>
</select>
But you should have to confirm first that you got value of $this->session->city.
Related
I'm trying to create a dynamic funktion in jQuery, which handles onchange event for a select (dropdown) and updates to other text fields with some calculated values.
But I'm having some trouble with.
My select is like this
<select name="r514b" id="r514b" size="1" class="listform" style="width: 155px;"> <option value="0">Vælg overfladebehandling</option> <?php while($farverow = mysql_fetch_array($farvequery, MYSQL_ASSOC)) { if ($res['r514b'] == $farverow['id']) {
$r514bselected = "selected";
} else {
$r514bselected = "";
}?> <option value="<?php echo $farverow['id']; ?>" <?php echo $r514bselected; ?> data-nypris="<?php echo $farverow['pris']; ?>" data-grundpris="<?php echo $farvegrundrow['pris']; ?>" data-sumvaegt="<?php echo $res['r477']; ?>" data-db="<?php echo $res['r502']; ?>"><?php echo $farverow['navn']; ?></option> <?php } ?> </select>
and the jQuery is like:
$('#r514b').change(function(){
var ny_pris = $(this).find(':selected').data('nypris');
var grundpris = $(this).find(':selected').data('grundpris');
var sumVaegt = $(this).find(':selected').data('sumvaegt');
var db = $(this).find(':selected').data('db')
var overflade_forskel = ny_pris - grundpris;
// alert(ny_pris);
// alert(grundpris);
// alert(sumVaegt); // * overflade_forskel);
var pris_forskel = (sumVaegt * overflade_forskel)/(1-(ds.r502.value/100));
// alert(pris_forskel);
ds.id514d.value = Math.round(pris_forskel);
ds.r514d.value = Math.round(pris_forskel);
});
The reason I want to make it dynamical is that I need to have multiple selects, which basically does the same thing. Only difference is which text fields they update.
So I was hoping that I somehow to make the function catch the selects dynamic, and update the fields dynamic maybe from some extra data attributes on the selects.
But how do I do that?
Hi all i was making a website were you can add excel file and choose a person and when you choose a person then it the web would only print those rows were only is a person that you chosen from select box.
Does anyone know how i could get result variable from js function (function getSelectedPerson()) and that resul insert in php for statement.
<script>
var filling_prices= new Array();
filling_prices["None"]=0;
filling_prices["dla_dei_mat"]="Deividas Matulis";
filling_prices["dla_tom_ver"]="Tomas Veršinskas";
filling_prices["dla_dar_ser"]="Darius Sereika";
filling_prices["dla_dov_pra"]="Dovydas Prakapas";
function getSelectedPerson()
{
var theForm = document.forms["dla_darbuotojo_pasirinkimas_form"];
var selectedPerson = theForm.elements["dla_darbuotojo_pasirinkimas"];
fillSelectedPerson=filling_prices[selectedPerson.value];
return fillSelectedPerson;
}
</script>
<?php
require_once "Classes/PHPExcel.php";
$chosenPerson = $_GET["getSelectedPerson()"];
$tmpfname = "visi.xls";
$excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
$excelObj = $excelReader->load($tmpfname);
$worksheet = $excelObj->getSheet(0);
$lastRow = $worksheet->getHighestRow();
echo "<table>";
for ($row = 1; $row <= $lastRow; $row++) {
if ($chosenPerson == ($worksheet->getCell('D'.$row)->getValue()) ) {
echo "<tr><td>";
echo $worksheet->getCell('D'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('F'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('G'.$row)->getValue();
echo "</td><tr>";
}
}
echo "</table>";
?>
<form name="dla_darbuotojo_pasirinkimas_form">
<div class="Choose_people">
<select name="dla_darbuotojo_pasirinkimas">
<option value="None">Darbuotojai</option>
<option value="dla_dei_mat">Deividas Matulis</option>
<option value="dla_tom_ver">Tomas Versinskas</option>
<option value="dla_dar_ser">Darius Sereika</option>
<option value="dla_dov_pra">Dovydas Prakapas</option>
</select>
</div>
</form>
I think this is what you are looking for. You can not use Javascript variables in PHP, you have to pass the values via GET or POST requests. You don't need the script portion of your example.
<form name="dla_darbuotojo_pasirinkimas_form" action="yourscript.php" method="get">
<div class="Choose_people">
<select name="dla_darbuotojo_pasirinkimas" onchange="this.form.submit()>
<option value="None">Darbuotojai</option>
<option value="dla_dei_mat">Deividas Matulis</option>
<option value="dla_tom_ver">Tomas Versinskas</option>
<option value="dla_dar_ser">Darius Sereika</option>
<option value="dla_dov_pra">Dovydas Prakapas</option>
</select>
</div>
</form>
Change
$chosenPerson = $_GET["getSelectedPerson()"];
to
$chosenPerson = $_GET["dla_darbuotojo_pasirinkimas"];
You may also need a condition in your PHP script
if(isset($_GET["dla_darbuotojo_pasirinkimas"])) {
// do the get person stuff
$chosenPerson = $_GET["getSelectedPerson()"];
$tmpfname = "visi.xls";
$excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
$excelObj = $excelReader->load($tmpfname);
$worksheet = $excelObj->getSheet(0);
$lastRow = $worksheet->getHighestRow();
echo "<table>";
for ($row = 1; $row <= $lastRow; $row++) {
if ($chosenPerson == ($worksheet->getCell('D'.$row)->getValue()) ) {
echo "<tr><td>";
echo $worksheet->getCell('D'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('F'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('G'.$row)->getValue();
echo "</td><tr>";
}
}
echo "</table>";
}
So if the script has been called with no person it won't try to run that code.
I have a form with one Select option and 3 input text box. I fetch the value of select option from database. I want to change data of the 3 input field based on what i select. I try many ways but faild.
<Select onchange="update(this)">
<?php
$query = "SELECT * FROM wheel1 ORDER BY chair_no ASC";
$run = mysqli_query($connect, $query);
$i = 0;
while ($chair = mysqli_fetch_assoc($start)) {
$i++;
?>
<option value="<?php echo $chair['chair_no'];?>"><?php echo $i ?></option>
<?php
$name = $chair['name'];
$phone = $chair['phone'];
$detail = $chair['detail'];
}
?>
It fetch data perfectly for Select menu, But only store the last value for name phone and detail variable. Here is javascript code.
function update( elem ) {
var name = "<?php echo $name; ?>";
var phone= "<?php echo $phone; ?>";
var detail = "<?php echo $detail; ?>";
document.getElementById("name").innerHTML = name;
document.getElementById("phone").innerHTML = phone;
document.getElementById("detail").innerHTML = detail;
}
Any help is appricated. Thanks in advance.
Set diffirent attribute in options for name, phone and detail
Demo: https://jsfiddle.net/sjhhv4pw/
<select onchange="update(this)">
<?php
$query = "SELECT * FROM wheel1 ORDER BY chair_no ASC";
$run = mysqli_query($connect, $query);
$i = 0;
while ($chair = mysqli_fetch_assoc($start)) {
$i++;
?>
<option data-name="<?php echo $chair['name']; ?>" data-phone="<?php echo $chair['phone']; ?>" data-detail="<?php echo $chair['detail']; ?>" value="<?php echo $chair['chair_no'];?>"><?php echo $i ?></option>
<?php
}
?>
</select>
jQuery:
function update( elem ) {
var name = $(elem).find("option:selected").attr("data-name");
var phone= $(elem).find("option:selected").attr("data-phone");
var detail = $(elem).find("option:selected").attr("data-detail");
}
Try below code
<Select onchange="update(this)">
" data-phone="" data-chair-detail="" value="">
javascript code, considering you are using jQuery
function update( elem ) {
var name = ele.data('chair-name');
var phone= ele.data('phone');
var detail = ele.data('chair-detail');
}
As I wrote in the title I have this part of code in page page.php that is in a subfolder admin. So the path of page is ../admin/page.php:
<select name="my_select" id="my_select" onchange="function(this.value);">
<?php
include "../db/db_config.php";
$conn = mysql_connect($host,$user,$password)or die(mysql_error());
mysql_select_db($db, $conn);
$query="Query";
$res=mysql_query($query,$conn);
while($row=mysql_fetch_array($res)){
$id=$row['id'];
$text=$row['text'];
echo "<option value='$id'>$text</option>";
}
}
?>
</select>
$var = $_POST['my_select'];
echo "I have selected $var";
I use a function that I have found on internet:
function fetch_select(val)
{
$.ajax({
type: 'post',
url: 'page.php',
data: {
get_option:val
},
success: function (response) {
document.getElementById("my_select").innerHTML=response;
}
});
}
What I have to do to take value in $var? Because I need this value to build other things. Is it possible?
EDIT:
Probably I don't explain very well my problem. I don't have very good with ajax because I never use it. I have a deadline so I can't study it now.
Now I have this situation:
I have a select-form with an input submit. After click on the button I use $_POST['myoption'] and I get the value.
Then I do it:
if($var == 1)
//a query from database
else if($var == 2)
//another different query
else
//other but no query
This work correctely. I need to change it and use in the same page. How can I do the same?
You don't to do a POST to do this you can do it with jQuery.
<?php
include "../db/db_config.php";
$conn = mysql_connect($host,$user,$password)or die(mysql_error());
mysql_select_db($db, $conn);
$query="Query";
$res=mysql_query($query,$conn);
?>
<select name="my_select" id="my_select">
<?php
while($row=mysql_fetch_array($res)){
$id=$row['id'];
$text=$row['text'];
echo "<option value='$id'>$text</option>";
}
?>
</select>
<span id="selected"></span>
<script>
$("#my_select").change(function() {
$("#selected").html($("#my_select option:selected").text());
});
</script>
This will give the select value to PHP:
<?php
include "../db/db_config.php";
$conn = mysql_connect($host,$user,$password)or die(mysql_error());
mysql_select_db($db, $conn);
$query="Query";
$res=mysql_query($query,$conn);
if (isset($_POST['my_select'])) {
$var = $_POST['my_select'];
} else {
$var = '';
}
?>
<form id="my_form" action="" method="POST">
<select name="my_select" id="my_select">
<?php
while($row=mysql_fetch_array($res)){
$id=$row['id'];
$text=$row['text'];
echo "<option value='$id'>$text</option>";
}
?>
</select>
</form>
<span id="selected">I have selected <?php echo $var; ?></span>
<script>
$("#my_select").change(function() {
$('#my_form').submit();
});
</script>
I am trying to create a filter for a gallery that I've created. The gallery has 5 filters using dropdown menu's. When a item is selected from one of the 5 filters it has to filter the images. When a second filter is selected it has to filter the results of the first filter and so on.
I am using the onchange='this.form.submit()' script but I don't know how to assign a certain action to it when an item is selected. This is my code at the moment of writing:
<td>
Kleur:
<form method="POST">
<select name="kleur" onchange='this.form.submit()'>
<option> -- Geen optie -- </option>
<?php while ($line1 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) { ?>
<option value="<?php echo $line1['kleur']; ?>"> <?php echo $line1['kleur']; ?>
</option>
<?php } ?>
</select>
</form>
<?php
if (isset($_POST['submit'])) {
$kleur = $_POST['kleur'];
$SQL = "SELECT * FROM `rozen` WHERE `kleur` LIKE '$kleur'";
$result = mysqli_query($connection, $sql);
echo $result;
}
?>
</br>
</td>
The following part doesn't seem to work:
<?php
if (isset($_POST['submit'])) {
$kleur = $_POST['kleur'];
$SQL = "SELECT * FROM `rozen` WHERE `kleur` LIKE '$kleur'";
$result = mysqli_query($connection, $sql);
echo $result;
}
?>
Does anyone know how to use this script? and perhaps explain how to save the selected item in the dropdown menu too?
You can add an attribute with all the information you need to filter to your images
<img filterInfo="Kleur|Geur|Bloemvorm|Gezondheid|Type|Zoeken">
then set a class for all your filters to catch the changes
$(".filters").on("change",function(){
var kleur = $('[name=Kleur]').val();
var Geur = $('[name=Geur]').val();
...
...
...
$.each($('#gallery img'),function(i,v){
var attrs = $(v).attr("filterInfo").slice("|");
if((kleur == "" || kleur == attrs[0]) && (Geur == "" || == attrs[1]) .... other filters)
$(this).show(); //or fadeIn();
else
$(this).hide(); //or fadeOut();
});
});