Change value of jQuery mobile flip switch using php - php

Does anyone know how to set the value of a jQuery mobile flip switch using just php? I have tried the below without any luck. Any ideas would be appreciated!
<?php
ifFilled = '1';
?>
<select name="flip-min" id="flip-min" data-role="slider">
<option value="1" <?php echo $isFilled == '1' ? ' selected="selected"' : '';?>>Filled</option>
<option value="0" <?php echo $isFilled == '0' ? ' selected="selected"' : '';?>>Un-Filled</option>
</select>

Do it with JavaScript:
Look at this fiddle first.
<label for="flip-a">Select slider:</label>
<select name="slider" id="flipMe" data-role="slider">
<option value="off">Off</option>
<option value="on">On</option>
</select>
<?php if ($ifFilled == true): ?>
var fts = $('#flipMe');
fts.val('on');
fts.slider('refresh');
<?php endif; ?>

opps $ifFilled does not match $isFilled lol dummy of the year award over here!!! the code works fine other than that....

Related

How to get certain data from database based on primary key from select option? [duplicate]

Is there any way to set the selected item in a drop down box using the following 'type' code?
<select selected="<?php print($row[month]); ?>"><option value="Janurary">January</option><option value="February">February</option><option value="March">March</option><option value="April">April</option></select>
The database holds a month.. and I want to allow on the edit page, them to choose this month.. but it to be pre-filled with their current setting?
You need to set the selected attribute of the correct option tag:
<option value="January" selected="selected">January</option>
Your PHP would look something like this:
<option value="January"<?=$row['month'] == 'January' ? ' selected="selected"' : '';?>>January</option>
I usually find it neater to create an array of values and loop through that to create a dropdown.
You mark the selected item on the <option> tag, not the <select> tag.
So your code should read something like this:
<select>
<option value="January"<?php if ($row[month] == 'January') echo ' selected="selected"'; ?>>January</option>
<option value="February"<?php if ($row[month] == 'February') echo ' selected="selected"'; ?>>February</option>
...
...
<option value="December"<?php if ($row[month] == 'December') echo ' selected="selected"'; ?>>December</option>
</select>
You can make this less repetitive by putting all the month names in an array and using a basic foreach over them.
You can use this method if you use a MySQL database:
include('sql_connect.php');
$result = mysql_query("SELECT * FROM users WHERE `id`!='".$user_id."'");
while ($row = mysql_fetch_array($result))
{
if ($_GET['to'] == $row['id'])
{
$selected = 'selected="selected"';
}
else
{
$selected = '';
}
echo('<option value="'.$row['id'].' '.$selected.'">'.$row['username'].' ('.$row['fname'].' '.substr($row['lname'],0,1).'.)</option>');
}
mysql_close($con);
It will compare if the user in $_GET['to'] is the same as $row['id'] in table, if yes, the $selected will be created. This was for a private messaging system...
Simple and easy to understand example by using ternary operators to set selected value in php
<?php $plan = array('1' => 'Green','2'=>'Red' ); ?>
<select class="form-control" title="Choose Plan">
<?php foreach ($plan as $id=> $value) { ?>
<option value="<?php echo $id;?>" <?php echo ($id== '2') ? ' selected="selected"' : '';?>><?php echo $value;?></option>
<?php } ?>
</select>
Its too old but I have to add my way as well :) because it is generic and useful especially when you are using static dropdown values.
function selectdCheck($value1,$value2)
{
if ($value1 == $value2)
{
echo 'selected="selected"';
} else
{
echo '';
}
return;
}
and in you dropdown options you can use this function like this and you can use this as many as you can because it fits with all of your select boxes/dropdowns
<option <?php selectdCheck($row[month],january); ?> value="january">january</option>
:) I hope this function help others
Simple way
<select class ="dropdownstyle" name="category" selected="<?php print($messageeditdetails[0]['category_id']); ?>">
<option value=""><?php echo "Select"; ?></option>
<?php foreach ($dropdowndetails as $dropdowndetails) { ?>
<option <?php if($messageeditdetails[0]['category_id'] == $dropdowndetails['id']) { ?> selected="<?php echo $dropdowndetails['id']; ?>" <?php } ?> value="<?php echo $dropdowndetails['id']; ?>"><?php echo $dropdowndetails['category_name']; ?></option>
<?php } ?>
</select>
This is the solution that I came up with...
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="select_month">
<?php
if (isset($_POST['select_month'])) {
if($_POST["select_month"] == "January"){
echo '<option value="January" selected="selected">January</option><option value="February">February</option>';
}
elseif($_POST["select_month"] == "February"){
echo '<option value="January">January</option><option value="February" selected="selected">February</option>';
}
}
else{
echo '<option value="January">January</option><option value="February">February</option>';
}
?>
</select>
<input name="submit_button" type="submit" value="Search Month">
</form>
A Simple Solution:
It work's for me
<div class="form-group">
<label for="mcategory">Select Category</label>
<select class="form-control" id="mcategory" name="mcategory" required>
<option value="">Please select category</option>
<?php foreach ($result_cat as $result): ?>
<option value="<?php echo $result['name'];?>"<?php
if($result['name']==$mcategory){
echo 'selected';
} ?>><?php echo $result['name']; ?></option>
}
<?php endforeach; ?>
</select>
</div>
Check this:
<select class="form-control" id="department" name="department" type="text">
<option value="medical-furniture" #if($list->department == "medical-furniture") selected #endif>Medical furniture</option>
<option value="medical-table" #if($list->department == "medical-table") selected #endif>Medical table</option>
<option value="service" #if($list->department == "service") selected #endif>Service</option>
</select>
My suggestion is to leverage the hidden/collapse attribute. Try with this example:
<select>
<option value="echo $row[month]" selected disabled hidden><? echo $row[month] ?></option>
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
</select>
in case of null for $row[month] the selected item is blank and with data, it would contain less codes for many options and always working for HTML5 and bootstrap etc...
If you have a big drop down. it's much easier to use jQuery with PHP.
This is how to do it:
<script>
$(document).ready(function () {
$('select[name="country"]').val('<?=$data[0]['Country']?>');
});
</script>
The easiest solution for the selected option in dropdown using PHP is following
<select class="form-control" name="currency_selling" required >
<option value="">Select Currency</option>
<option value="pkr" <?=$selected_currency == 'pkr' ? ' selected="selected"' : '';?> >PKR</option>
<option value="dollar" <?=$selected_currency == 'dollar' ? ' selected="selected"' : '';?> >USD</option>
<option value="pounds" <?=$selected_currency == 'pounds' ? ' selected="selected"' : '';?> >POUNDS</option>
<option value="dirham" <?=$selected_currency == 'dirham' ? ' selected="selected"' : '';?> >DRHM</option>
</select>
You can try this after select tag:
<option value="yes" selected>yes</option>
<option value="no">no</option>

Whats the best way to show selected options on a form when reloaded using PHP

I would like the select option the user selected to be shown when the form is reloaded. I am currently using bootstrap select picker to style the drop down. Right now I am creating an array and echoing out a selected class based on the value stored in the database.
This is not a major problem with a small amount of options like the code provided below, however with a select box with larger amounts of options this obviously amounts to lots of extra code.
There must be a smarter way to do this than the code i'm using below. Can anyone help?
<select class="selectpicker" name="Do_You_Have_A_Job_Spec_You_Can_Email">
<option value="" <?php if ($user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email'] == '') echo "selected='selected'"; ?>>Please Select</option>
<option value="Yes" <?php if ($user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email'] == 'Yes') echo "selected='selected'"; ?>>Yes</option>
<option value="No" <?php if ($user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email'] == 'No') echo "selected='selected'"; ?>>No</option>
</select>
If the option's are static you could use ternary operators, so it will be something like :
<?php $selected_option = $user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email']; ?>
<select class="selectpicker" name="Do_You_Have_A_Job_Spec_You_Can_Email">
<option value="" <?php echo $selected_option==''?'selected':''; ?>>Please Select</option>
<option value="Yes" <?php echo $selected_option=='Yes'?'selected':''; ?>>Yes</option>
<option value="No" <?php echo $selected_option=='No'?'selected':''; ?>>No</option>
</select>
Hope this helps.
<?php
$text = $user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email'];
?>
<select class="selectpicker" name="Do_You_Have_A_Job_Spec_You_Can_Email">
<option value="">Please Select</option>
<option value="Yes" <?php echo $text == 'Yes' ? 'selected' : ''; ?>> Yes </option>
<option value="No" <?php echo $text == 'No' ? 'selected' : ''; ?>> No </option>
</select>
Alternative Way
"..however with a select box with larger amounts of options this
obviously amounts to lots of extra code.".
In this case, when you are having lots of data for dropdown. You can create a table where you can have both option name and option value.
job_specification_table
id option_value option_name
1 Please Select
2 Yes Yes
3 No No
.
.
.
Then, retreive those values and check for user_data and according to it, option will get selected.
<?php
$sql = "SELECT * FROM job_specification_table";
$result = mysqli_query($db_con,$sql);
$text = $user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email'];
?>
<select class="selectpicker" name="Do_You_Have_A_Job_Spec_You_Can_Email">
<?php
while ($row = mysqli_fetch_array($result)){
$option_value = $row['option_value'];
$selected = ($option_value == $text) ? "selected" : "";
?>
<option value="<?=$option_value;?>" <?=$selected;?>>
<?=$row['option_name'];?>
</option>
<?php }?>
</select>
Here is more readable solution:
<select class="selectpicker" name="Do_You_Have_A_Job_Spec_You_Can_Email">
<option value=
<?php
$choise = $user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email'];
switch ($choise) {
case 'Yes':
echo '"Yes" selected="selected" >Yes';
break;
case 'No':
echo '"No" selected="selected" >No';
break;
case '':
echo '"" selected="selected" >Please Select';
break;
}
?>
</option>
</select>

How to see default selection on select type without using an array

Im trying to set my website so when I enter an users form it selects their selection in the select box first and then the other answers below it. The select box only has two answers, Yes & No. But at the moment it is showing their answer up first in the $returned variable but then also listing the two answers below.
currently see ;
Yes
No
Yes
rather than their answer and then the other one.
The code im using is;
<div id="editp"><p align="center">
<label class="labelform">Returned:</label><select name="returned" style="width: 150px">
<option><?php echo $returned;?></option>
<option value="No">No</option>
<option value="Yes">Yes</option>
</select></p></div>
How would I change the code to display the users answer in $returned and then the alternative answer below?
Thanks
In PHP:
$returnedYes = ($returned == 'Yes') ? "selected=selected" : "";
$returnedNo = ($returned == 'No') ? "selected=selected" : "";
In HTML:
<option value="No" <?php echo $returnedNo;?>>No</option>
<option value="Yes" <?php echo $returnedYes;?>>Yes</option>
That's not a very user-friendly way to handle that as the order of the options changes with the answer.
Instead you should mark the selected one as selected.
A simple example:
<select name="returned" style="width: 150px">
<option value="Yes" <?php echo $returned === 'Yes' ? 'selected="selected"' : '' ?>>Yes</option>
<option value="No" <?php echo $returned === 'No' ? 'selected="selected"' : '' ?>>No</option>
</select>

Using Selected="Selected" from dynamic list of options

I'm posting variables from my form on to another form (a search records form), this will have all the variables that are being posted to the current form and place within INPUT field or SELECT fields. Any fields which are INPUT type I can POST to using the Value="" section of the INPUT. Now for SELECT I have a little problem, any small list that I prepopulate in the code are fine I have used this code below
<label>Fuel</label>
<select tabindex="1" id="propertyfueltype" name="propertyfueltype">
<option value=""></option>
<option value="1" <?php echo ($searchfuel == '1' ? 'selected' : '')?>>Mains gas</option>
<option value="2" <?php echo ($searchfuel == '2' ? 'selected' : '')?>>Wood or coal fire</option>
<option value="3" <?php echo ($searchfuel == '3' ? 'selected' : '')?>>Oil</option>
<option value="4" <?php echo ($searchfuel == '4' ? 'selected' : '')?>>Electric storage heaters</option>
<option value="5" <?php echo ($searchfuel == '5' ? 'selected' : '')?>>LPG or bottled gas</option>
<option value="6" <?php echo ($searchfuel == '6' ? 'selected' : '')?>>No central heating system</option>
</select>
As I said when I run the Sear records form and return the posted variables, this works great. My problem is with big data sets within SELECT which are from the database, this is what I have currently, which is perfect to get data into a SELECT but how can I post a variable to it in a similar way to the previous code?
<label>Fuel Type</label>
<?php $fueltype = db::getInstance()->query('SELECT * FROM lkup_fueltype');
if(!$fueltype->count()) { echo 'Problem'; } else { ?>
<select tabindex="1" id="propertyfueltype" name="propertyfueltype">
<?php foreach ($fueltype->results() as $fueltype) { ?>
<option value="<?php echo $fueltype->PropertyFuelType; ?>"><?php echo $fueltype->PropertyFuelType; ?></option>
<?php } } ?>
</select>
Like this:
<option
value="<?php echo $fueltype->PropertyFuelType; ?>"
<?php echo $fueltype->PropertyFuelType == $searchfuel ? "selected" : ""; ?>
><?php echo $fueltype->PropertyFuelType; ?></option>
Having said that, I would recommend using a PHP templating system; or at least convert this logic into a function.

php: issue with the select box

test5.php
<form action="test4.php" method="post">
Please input one package:(Goa, Kashmir, Rajasthan):<input type="text" name="package">
<input type="submit" value="submit">
<form>
test4.php
<label>Select Tour Package<span class="note">*</span>:</label>
<select name="package">
<option value="Goa" <?php ($_POST['package'] == "Goa")? "selected":"";?>>Goa</options>
<option value="Kashmir" <?php ($_POST['package'] == "Kashmir")? "selected":"";?>>Kashmir</options>
<option value="Rajasthan" <?php ($_POST['package'] == "Rajasthan")? "selected":"";?>>Rajasthan</options>
</select>
I want to have this kind of function: when someone inputs one package on test5.php, the same package will be chosen/selected in select-box on test4.php, but it seems does not work, so what is wrong with the scripts above?
You want to echo out the value as follows:
<option value="Goa" <?php echo ($_POST['package'] == "Goa")? "selected":"";?>>Goa</options>
I think it is as simple as an echo
You forgot echo.
<option value="Goa" <?php echo ($_POST['package'] == "Goa") ? "selected" : "" ?>>Goa</options>
For a simpler notation you can use <?=, that will print out result of any expression inside <?= ?>
<option value="Goa" <?= ($_POST['package'] == "Goa") ? "selected" : "" ?>>Goa</options>
You need to do two things...
Enable error reporting during development.
ini_set('display_errors', 'On');
error_reporting(E_ALL);
Echo selected
<?= isset($_POST['package']) && $_POST['package'] == 'Goa' ? 'selected' : '' ?>
add echo in each option as
<?php echo ($_POST['package'] == "Goa")? "selected":"";?>
also try with trim($_POST['package']) for remove spaces
<select name="package">
<option value="Goa" <?php if($_POST['package'] == "Goa") echo 'selected="selected"';?>>Goa</options>
<option value="Kashmir" <?php if($_POST['package'] == "Kashmir") echo 'selected="selected"';?>>Kashmir</options>
<option value="Rajasthan" <?php if($_POST['package'] == "Rajasthan") echo 'selected="selected"';?>>Rajasthan</options>
</select>

Categories