HTML <SELECT> on selected value the other module will be null - php

I need help with this: HTML
<select class="nesty-input" style="max-width: 100%" name="what_about">
<option selected disabled hidden><?php echo $what_about[1]; ?></option>
<option value="0|-">-</option>
<option value="1|one">one</option>
<option value="2|two">two</option>
<option value="3|three">three</option>
</select>
<select class="nesty-input" style="max-width: 100%" name="im">
<option selected disabled hidden><?php echo $im[1]; ?></option>
<option value="0|-">-</option>
<option value="1|one">one</option>
<option value="2|two">two</option>
<option value="3|three">three</option>
</select>
PHP
$what_about = explode('|', $_POST['what_about']);
$im = explode('|', $_POST['im']);
So, what basically does this script: on page reload prints the value (0=id, 1=text), this because I wanted to do that in the database will be archived the id of the selected item, the problem is that when I select a value and I reload the page it still remains, but when I select the new one the value of the old is null.
Also known as: Warning: Undefined offset: 1.

You default selected element has no vlaue. So then you you submit it you get default values of $what_about and $im equals ['']. And somewhere in your code later you are calling for $what_about[1] or $im[1]
I would recommend you to check if count($what_about) == 2 before you call for $what_about[1] and if count($im) == 2 before you call for $im[1]
or set the value for default select option
<option selected disabled hidden value='|'><?php echo $im[1]; ?></option>

Related

How to $_GET values from sql to be inserted in <select type> PHP

I'm using xampp PHP
How to do I get values from MySQL in select type like getting value in
<input type="text" value="<?php $variable['valueinsql']; ?>">
But I want the value to be inserted in
<select type>
And I tried
If(isset($_GET['id'])){
<p> Banks:</p><select multiple="multiple" name="otherbanks[]" style=" width:190;" value = "<?php $list["Banks"] ?>"">
<option value="Banksone">One</option>
<option value="Bankstwo">two</option>
<option value="Bankthree">three</option>
I want to see it automatically highlighted.
Use in_array to check the value available in array or not. If available echo 'selected';
Try this ==>
<select multiple="multiple" name="otherbanks[]" style=" width:190;">
<option <?php if (in_array('Banksone',$list["Banks"])) {echo 'selected';} ?> value="Banksone">One</option>
<option <?php if (in_array('Bankstwo',$list["Banks"])) {echo 'selected';} ?> value="Bankstwo">two</option>
<option <?php if (in_array('Bankthree',$list["Banks"])) {echo 'selected';} ?> value="Bankthree">three</option>
</select>
To highlight particular options the selected attribute should be set on each option that is applicable - as below example.
<select name='otherbanks[]' multiple=true>
<option value=1 selected>one
<option value=2>two
<option value=3 selected>three
<option value=4>four
<option value=5 selected>five
</select>
Presumably you generate the select menu using PHP and a query to the database so to accomplish that ( highlighting ) in PHP you would need a loop to iterate through each value in $list["Banks"] and check the value against the value from sql. Without knowing exactly how you generate the select menu or knowing the value of $list['Banks'] it is hard to know exactly and I may be "barking up the wrong tree" as it were.

PHP - If select option value equals

I want to comment off .cities whenever I select the value Singaporean however it should get visible If I choose any other nationality apart from singaporean. Is it possible in PHP ?
<div class="cities">New York</div>
<select id="emp_nationality" name='emp_nationality' class='form-control'>
<option value="Singaporean">Singaporean</option>
<option value="Albanian">Albanian</option>
<option value="Algerian">Algerian</option>
<option value="American">American</option>
</select>
I tried this but for some reason it is not working
<?php
if ($_POST['emp_nationality'] != 'Singaporean') {
echo '<div class="cities">New York</div>';
}
?>
ERROR: Undefined index: emp_nationality
You can use an onchange handler in js and hide / show the ".cities" div depending on the value chosen. Note I added a display:none to the .cities since your first option in the select list is Singaporean and the function requires a change to trigger it. Also I would normally separate the onclick as a separate handler - rather than mixing it into the HTML but for this example I did it this way. Does not require PHP which would need the option to be selected on page load and would not easily change the visibility of the div on a different choice. This functions to hide the div if the correct option is selected and show it on other option selections.
function hideDiv(option)
{
if(option=="Singaporean"){$('.cities').hide()}else{$('.cities').show()}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="cities" style="display:none">New York</div>
<select id="emp_nationality" name='emp_nationality' class='form-control' onchange="hideDiv(this.value)">
<option value="Singaporean">Singaporean</option>
<option value="Albanian">Albanian</option>
<option value="Algerian">Algerian</option>
<option value="American">American</option>
</select>
<div class="cities" style="display:none">New York</div>
<select id="emp_nationality" name='emp_nationality' class='form-control' onchange="hideDiv(this.value)">
<option value="Singaporean">Singaporean</option>
<option value="Albanian">Albanian</option>
<option value="Algerian">Algerian</option>
<option value="American">American</option>
</select>
//js
function hideDiv(option)
{
if(option=="Singaporean"){$('.cities').hide()}else{$('.cities').show()}
}
If you like would to avoid the "Undefined index: emp_nationality" you need to make sure this variable exists before checking it's value. Use isset( ).
http://php.net/manual/en/function.isset.php
if (isset($_POST['emp_nationality']) && ($_POST['emp_nationality'] != 'Singaporean')) {
echo '<div class="cities">New York</div>';
}

How to keep showing selected option from drop down list?

I have a drop down list where I select options
<form action="" method="POST" class="styled-select">
<select name="seasons" onchange='this.form.submit()'>
<option value="">Select a Season</option>
<option value="1">2002/2003</option>
<option value="2">2003/2004</option>
<option value="3">2004/2005</option>
<option value="4">2005/2006</option>
<option value="5">2006/2007</option>
<option value="6">2007/2008</option>
<option value="7">2008/2009</option>
<option value="8">2009/2010</option>
<option value="9">2010/2011</option>
<option value="10">2011/2012</option>
<option value="11">2012/2013</option>
<option value="12">2013/2014</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
You can see the list here footystat
I am using the following PHP
if(isset($_POST['seasons'])){ $seasonette = $_POST['seasons']; }
if(isset($_POST['year'])){ $yearette = $_POST['year']; }
if(isset($_POST['comp'])){ $competitionette = $_POST['comp']; }
if(isset($_POST['which'])){ $whichette = $_POST['which']; }
When I select something from the list, I want selected item in the list to continue showing. At the moment when I select (for example) 2013/2014, it will show the results but the drop down menu goes back to its original state instead of showing 2013/2014.
Get Option value selected when it gets posted value, like this,
<option value="1" <?php if(isset($_POST['seasons']) && $_POST['seasons'] == '1'){ ?> selected="selected" <?php } ?>>2002/2003</option>
Set value like this for each option
You can set the "selected" property to the option , just like you set a value !
<option value="8" selected>2009/2010</option>
Use a if statement in PHP to determine which one should be selected.
Thats because the page refreshes.
On page load check if there is post variable than match the value with each option's HTML and write selected attribute.
The shorter way is
<option value="1" <?php echo $_POST['seasons']==1?"selected":""; ?>2002/2003</option>

optgroup get in php label associated

I have following code in a html form
<select name="category" class="input" onchange="ShowTB(this,'suggest');">
<option value="0" selected="selected">
[choose yours]
</option>
<optgroup label="Item">
<option value="SubItem1"SubItem1</option>
<option value="SubItem2">SubItem2</option>
</optgroup>
<option value="Item2">Item2</option>
<optgroup label="Item3">
<option value="SubItem4"SubItem4</option>
<option value="SubItem5">SubItem5</option>
</optgroup>
<option value="Item4">Item4</option>
<option value="Item5">Item5</option>
<option value="Item6">Item6</option>
<option value="Item7">Item7</option>
</select>
in php i get the value of field selected with:
$category = $_POST['category'];
in this mode if i select in the form ie: SubItem1 , in php i get value SubItem1 but i want also get associated label ie: Item or if i select SubItem5 i get SubItem5 but i want also get associated label ie: Item3
How to ?
Indeed, you only get the value. If you need more, just encode whatever you want into the value, for example:
<option value="Item3.SubItem5">SubItem5</option>
Alternatively, you could use javascript to catch onChange events on the select field and update a hidden input field with the desired label.
you could make the values arrays e.g.
<option value="Item3[SubItem5]">SubItem5</option>
so then your $_POST['category'] should return an array

select combo box based on get value

I'm using search with combo box
Here is my combo box source code
<select name="salary" class="styled">
<option selected="selected" value=''>Any</option>
<option value="10000">10,000</option>
<option value="20000">20,000</option>
<option value="30000">30,000</option>
<option value="40000">40,000</option>
<option value="50000">50,000</option>
<option value="60000">60,000</option>
<option value="70000">70,000</option>
<option value="80000">80,000</option>
<option value="90000">90,000</option>
<option value="100000">100,000</option>
<option value="110000">110,000</option>
<option value="120000">120,000</option>
<option value="130000">130,000</option>
<option value="140000">140,000</option>
<option value="150000">150,000</option>
</select>
I would like to select value based on $salary=$_GET['salary']; if $salary empty I need to select first one as selected
To set a default value for an HTML select element, you need to give the appropriate <option> element the selected attribute. It would look like this:
<option value='50000' selected='selected'>50,000</option>
In your PHP code, you would add this by setting a string for each option, to either "selected='selected'" or blank, depending on whether that option is the one you want to have selected.
This is obviously far easier to put into a loop rather than writing the same code twenty times, so you would need to rewrite your output to create a loop for creating your options.
Hope that helps.
You can do something like this on creation...[not tested]
<?php
$options = array(
'10000' => '10,000',
...
'150000' => '150,000'
); //From MySQL
array_unshift($options, '' => 'Any');
$salary = isset($_GET['salary'])?$_GET['salary']:'';
?>
<select name="salary" class="styled">
<?php foreach ($options as $value=>$text){ ?>
<option <?php if($value == $salary){echo 'selected="selected"'; }?> value="<?php echo $value; ?>"><?php echo $text; ?></option>
<?php } ?>
</select>
If I understand your question correctly you want that if the person doesn't select any thing you want 10000 to be automatically selected for him. And I also assume that the options in the select list are static. So..
<?php
if($_GET['salary']=='')
$salary = 10000;
else
$salary = $_GET['salary'];
?>

Categories