Form selector failing $_POST['medalCount'] !== '' test - php

In my post form, I have a selector for users to select the number of medals. In my getSearchFields() function, if the selector isn't empty (aka the selector is not on Select Medal Count), the function completes task 1. However, the selector is failing the $_POST['medalCount']!== '' test and is doing task 1. $_POST['medalCount'] is type string. How do I fix this?
<form action="index.php" method="post" name="search">
<select name="medalCount">
<option value=0 selected> Select Medal Count </option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
</select>
</form>
In index.php...
function getSearchFields(){
if($_POST['medalCount']!== ''){
//Task 1
$search_fields['medalCount'] =$_POST['medalCount'];
}
}

Change this
<option value=0 selected> Select Medal Count </option>
to this
<option value="" selected> Select Medal Count </option>
With all <option> elements having non-empty value attributes, you would never (normally) receive an empty string value in $_POST['medalCount'] and $_POST['medalCount']!== '' would always be true.

This line is wrong $_POST['medalCount']!== '', You need to try $_POST['medalCount']!= 0. As your selectors empty value is 0.

Related

How to retrieve <option> id to insert data into database

I have a form with a dropdown list using <select> and <option> tags.
below that I have a textarea field.
The option ID's are the databases names, and I need to insert the textarea-input into the selected database.
Code:
HTML FORM:
<select class="form-control" id="selection" name="selection_name">
<option id="option1">First item</option>
<option id="option2">Second item</option>
</select>
PHP:
<?php
$dbToInsert = $_POST[''] // <-What do I insert here to get the selected option?
...
?>
You Should use "value" in option tag
<select name="vehicle">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
In the above example, if you have chosen "Audi", on form submission you will get the value "audi" in $_POST["vehicle"]

Form with disabled select option selected as default not sending anything to validate

I have a form like so:
<select name="employment">
<option disabled="" selected="">-- What is your employment status --</option>
<option value="employed">Employed</option>
<option value="self_employed">Self Employed</option>
</select>
In PHP I am running through each of the $_POST variables and then checking if they have a value. If not I then add that field to the array for an error message.
The issue is that if I leave the default 'disabled' message selected nothing is passed through a post value so theres nothing for me to validate.
If I print_r my $_POST variable then it contains no 'employment' field unless I select an option.
How can I solve this?
<select name="employment">
<option disabled selected>-- What is your employment status --</option>
<option value="employed">Employed</option>
<option value="self_employed">Self Employed</option>
</select>
disabled attribute
The disabled attribute is a boolean attribute.
When present, it specifies that an option should be disabled
A disabled option is unusable and un-clickable.
Syntax:
<select disabled>
Not
<select disabled="">
In case of XHTML, syntax differs like
<select disabled="disabled">
hidden attribute
So, If you want to validate it. Use hidden in option.
<select name="employment">
<option hidden>-- What is your employment status --</option>
<option value="employed">Employed</option>
<option value="self_employed">Self Employed</option>
</select>
When, nothing got selected, then it will output as -- What is your employment status --
<?php
echo $Employment=$_POST['employment'];
?>
Output: -- What is your employment status --
So, Now you can easily use your validation in dropdown
For more info, click disabled attribute - W3 Schools
Try this:
<select name="employment">
<option disabled="" selected="" value ="">-- What is your employment status --</option>
<option value="employed">Employed</option>
<option value="self_employed">Self Employed</option>
</select>
PHP:
if(empty($_POST['employment'])){
return false;
}else {
// I got value;
}

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>

change browser href in combobox option

I try to make a list of data from a table in a database. I do this with an select option, because the user is only allowed to choose one.
I do it on this way:
<select name="test">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php" onClick="window.location.href='other.php'">
Other
</option>
</select>
This works in IE and Firefox, but not in Google Chrome.
It's not an option to put the onClick in the <select>-tag, because they don't have a href and I need their values to get the primary key and store something in the table.
I hope you can help me.
UPDATE: this is the solution for my case, solved by Harry
<select name="test" onchange="this.options[this.selectedIndex].value.indexOf('.php')!=-1 &&
(window.location = this.options[this.selectedIndex].value);">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php">Other</option>
</select>
Try the below code and check if it solves your problem. You needn't have the href values in the select tag for this to work. The href can be in your option's value tag.
UPDATE: The below code would do the redirect only if the value of the selected option tag has .php in it. For the first two options, no action will be taken.
<select name="test"
onchange="this.options[this.selectedIndex].value.indexOf('.php')!=-1 &&
(window.location = this.options[this.selectedIndex].value);">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php">Other</option>
</select>
this isn't valid HTML. it should at least be:
<option value="other.php" onClick="window.location.href='other.php'">
try this
<select name="test"
onchange="var val = this.value;
if (val.indexOf('other.php')!=-1) window.location=loc">
<option value="line1">Line1</option>
<option value="line2">Line2</option>
<option value="other.php">Other</option>
</select>

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

Categories