Select form option on page load - php

I would like to select the option contained within a php variable on page load.
<?php
$pre = 78;
?>
<select id="form8" name="form8">
<option value="15">15</option>
<option value="25">25</option>
<option value="64">64</option>
<option value="78">78</option>
<option value="82">82</option>
</select>
<script>
$(document).ready(function(){
$("form8").val("<?php echo $pre; ?>");
}
</script>
Expected output should be,
<select id="form8" name="form8">
<option value="15">15</option>
<option value="25">25</option>
<option value="64">64</option>
<option value="78" selected="selected">78</option>
<option value="82">82</option>
</select>
http://jsfiddle.net/qQZVN/1/

Example
$('#form8').val("<?php echo $pre; ?>") ;

Why not just use HTML to set selected='selected'? Seems easier, and supports non-JS users.

$(document).ready(function(){
$("#form8").val(<?php echo $pre; ?>);
}

$('select#form8').val('<?php echo $pre; ?>');

Related

Unset session by selecting dropdown menu option

I would like to add an option in my drop down menu that unset or destroys the session $_SESSION['selected_opt].
Practice code:
<form class="search-form">
<select name="thema" class="selectpicker">
<option value="t1" class="special">Select something</option>
<optgroup label="Select...">
<option value="s1" data-icon="glyphicon-sort-by-alphabet" class="special">Selection 1</option>
<option value="s1" data-icon="glyphicon-sort-by-alphabet" class="special">Selection 2</option>
<option value="s1" data-icon="glyphicon-sort-by-alphabet" class="special">Selection 3</option>
<option value="s4" data-icon="glyphicon-sort-by-alphabet" class="special">Selection 1</option>
</optgroup>
</select>
</form>
I've seen some code's like this around:
<option value="black" <?php if(isset($_SESSION['kategorie']) == "black") { echo ' selected';} ?>>Black and white</option>
So I've put something like that in the code:
<option value="t1" <?php if($_SESSION['selected_opt']) { unset($_SESSION['selected_opt']);} ?> class="class_opt">Select something</option>
But of course this didn't work. Is this at all possible or should I use something like jQuery? I could find a suitable answer anywhere... I hope someone can help...
You can do something like:
<select name="thema" class="selectpicker" change='selectChanged(this);'>
and create the function:
<script>
function selectChanged(obj){
if($(obj).val() == "LogOut"){
//redirect to your session clearing / log out page
window.location.href = 'http://example.com/LogOut.php''
};
});
</script>
and your option will be:
<option value="LogOut">Log Out</option>

How to get value of select option an used in php variable?

I'm trying to take the value of SELECT option and use it in php variable, but can not figure out how. Would you assist me. Thank you.
Here is my code:
<select name="pcs" id="pcs">
<option class="option" value="8" selected class="selected">8</option>
<option value="12">12</option>
<option value="16">16</option>
<option value="20">20</option>
<option value="24">24</option>
<option value="24+">над 24</option>
</select></br>
My goal is, after taking the value to make some calculation with it and show results. Thank you for the help.
This is one option:
<select name="pcs" id="pcs" onchange="this.form.submit()">
<option class="option" value="8" selected">8</option>
<option value="12" <?php if($pcs =="12") { echo "selected"; } ?>>12</option>
<option value="16" <?php if($pcs =="16") { echo "selected"; } ?>>16</option>
<option value="20" <?php if($pcs =="20") { echo "selected"; } ?>>20</option>
<option value="24" <?php if($pcs =="24") { echo "selected"; } ?>>24</option>
<option value="24+" <?php if($pcs =="24+") { echo "selected"; } ?>>над 24</option>
</select></br>
But I do not want the page to reload.
Using jQuery get the select box value on change or submit, pass the value to a php file via Ajax say calculation.php, Do the necessary calculation and echo the result in calculation.php. Get the value and display inside DIV or desired place.
EXAMPLE BELOW:
main_file.php
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#pcs").on('change', function(){
var pcs_val = $(this).val();
$.ajax({
type: "POST",
url: "calculation.php",
data: { pcs: pcs_val},
success: function(theResponse) {
$('#outputDiv').html(theResponse);
}
});
});
});
</script>
<select name="pcs" id="pcs">
<option class="option" value="8" selected class="selected">8</option>
<option value="12">12</option>
<option value="16">16</option>
<option value="20">20</option>
<option value="24">24</option>
<option value="24+">над 24</option>
</select></br>
<div id="outputDiv">--output here--</div>
calculation.php
<?php
$pcs = isset($_POST['pcs']) ? $_POST['pcs'] : '';
echo 'Calculation Result = '.$pcs;
?>
Try the following without using AJAX:
<?php
$pcs_array = array('8'=>'8','12'=>'12','16'=>'16','20'=>'20','24'=>'24','24+'=>'над 24');
$select_pcs = '';
if(isset($_POST['pcs']) && !empty(isset($_POST['pcs']))){
$select_pcs = $_POST['pcs'];
}
if(!empty($pcs_array)){
?>
<select name="pcs" id="pcs">
<?php
$option_count = 0;
foreach($pcs_array as $k=>$v){
if(!empty($select_pcs) && $select_pcs == $k){
echo '<option class="option" value="'.$k.'" selected class="selected">'.$v.'</option>';
}elseif($option_count == 0 && empty($select_pcs)){
echo '<option class="option" value="'.$k.'" selected class="selected">'.$v.'</option>';
}else{
echo '<option value="'.$k.'">'.$v.'</option>';
}
$option_count++;
}
?>
</select></br>
<?php
}
?>

How to set Dropdown List default value based on user selection

I have a dropdown list. However i want to set a default based on user selection. Therefore the default value is not consistent. What can i do to achieve this? I have set $country = $_POST['country']; as user selection.
<td>Country:</td>
<td colspan="2"><select name="country">
<option value="93">93-Afghanistan</option>
<option value="355">355-Albania</option>
<option value="213">213-Algeria</option>
<option value="1-684">1-684-American Samoa</option>
<option value="376">376-Andorra</option>
<option value="244">244-Angola</option>
<option value="1-264">1-264-Anguilla</option>
<option value="672">672-Antarctica</option>
<option value="1-268">1-268-Antigua and Barbuda</option>
<option value="54">54-Argentina</option>
<option value="374">374-Armenia</option>
<option value="297">297-Aruba</option>
<option value="61">61-Australia</option>
</select>
To add on. There are 200 over options (I never list all down) so i hope to get a convenience way to achieve this
You can use 'selected' for relevant option
<option value="93" <?php if($country==93) echo "selected" ?> >93-Afghanistan</option>
<option value="355" <?php if($country==355) echo "selected" ?> >355-Albania</option>
like this add if conditon for each option.
Try using session for it.
The php code:
session_start();
$_SESSION['selectedCountry'] = $_POST['country'];
Then you can use JQuery to make dropdown selected:
$(document).ready(function(){
$(function() {
$("#country").val("<?php echo $_SESSION['selectedCountry'];?>");
});
})
Please try out this ..
HTML :-
<td>Country:</td>
<td colspan="2"><select id="country" name="country">
<option value="93">93-Afghanistan</option>
<option value="355">355-Albania</option>
<option value="213">213-Algeria</option>
<option value="1-684">1-684-American Samoa</option>
<option value="376">376-Andorra</option>
<option value="244">244-Angola</option>
<option value="1-264">1-264-Anguilla</option>
<option value="672">672-Antarctica</option>
<option value="1-268">1-268-Antigua and Barbuda</option>
<option value="54">54-Argentina</option>
<option value="374">374-Armenia</option>
<option value="297">297-Aruba</option>
<option value="61">61-Australia</option>
</select>
</td>
JQuery :-
$(document).ready(function(){
$(function() {
$("#country").val("<?php echo $_POST['country'];?>");
});
})
Try this...
<?php
(isset($_POST["country"])) ? $country = $_POST["country"] : $country=93;
?>
<form action='stackover.php' method='POST'>
<select id="country" name="country">
<option <?php if ($country == 93 ) echo 'selected' ; ?> value="93">93-Afghanistan</option>
<option <?php if ($country == 355 ) echo 'selected' ; ?> value="355">355-Albania</option>
<option <?php if ($country == 213 ) echo 'selected' ; ?> value="213">213-Algeria</option>
</select>
<input type="submit" value="Submit">
</form>

PHP/JQuery Dynamic Drop Down not sending variable via POST

I have a two step drop down menu. The second select is triggered with jquery. Since I have put this step in, my form does not pass the variable of the selected option. I expect it to just post but it doesn't. I have always had it loaded dynamically, so I didn't think that was the issue. I can't get it to work by passing it through the URL either.
Here is my code:
search.php
while($row = mysqli_fetch_array($result)) { ?>
<option value="<?=$row['id']?>"> <? echo $row['name'] ?></option>
<?php }
<form method = "post" action="index.php?page=users&id=<?php echo $row['id']; ?>">
<p>
<select name="list-select" id="list-select">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
</select>
<?php
echo '<select name="list-target" id="list-target">';
echo '</select>';
$installation_id = $_POST['list-target'];
mysqli_close($con);
?>
users.php:
$installation_id = $_POST['list-target'];
.js
$(document).ready(function($) {
$("#list-select").change(function() {
$("#list-target").load("index.php?page=search&svalue=" + $("#list-select").val());
});
});
Thanks.

how to concatinate a link with <select> <option> value on "onchange" event

<?php echo $this->getUrl(); ?> gives me my index page url .i.e. www.domain.com/index.php/
But now, what I want is, when any particular option is selected, it should add to this url.
.e.g.
www.domain.com/index.php/wordpress/
I write this code for it but now don know how to get it done. :(
<select onchange="setLocation('<?php echo $this->getUrl(); ?>')">
<option value=""> </option>
<option value="wordpress">Wordpress</option>
<option value="drupal">drupal</option>
<option value="magento">Megento</option>
</select>
Also, I had searched this link but couldn't take help from it. As it didnt work for me.
<select name="forma" ONCHANGE="location = this.options[this.selectedIndex].value;">
<option value="Home">Home</option>
<option value="Contact">Contact</option>
<option value="Sitemap">Sitemap</option>
</select>
With this (note that I've added this as the first argument):
<select onchange="setLocation(this, '<?php echo $this->getUrl(); ?>')">
<option value=""> </option>
<option value="wordpress">Wordpress</option>
<option value="drupal">drupal</option>
<option value="magento">Megento</option>
</select>
You could write the following JS:
function setLocation(elm, baseUrl) {
var keyword = elm.options[elm.selectedIndex].value; // Gets the selected keyword (some sanity check could be done)
window.location = 'http://' + baseUrl + keyword + '/'; // Redirects
}
Alternatively:
<select name="forma" onchange="window.location='http://<?php echo $this->getUrl(); ?>' + this.options[this.selectedIndex].value + '/';">
<option value="Home">Home</option>
<option value="Contact">Contact</option>
<option value="Sitemap">Sitemap</option>
</select>
(EDIT Prepended http:// to both examples.)

Categories