Unknown issue when trying to make a dropdown in html and php - php

Making a new tool and require a dropdown menu but it doesn't seem to work the way I have coded it.
Not sure exactly what the problem is. Have researched online but I haven't been able to figure it out.
if(!isset($_POST["ReasonList"]))
{
$error .= '<p><label class="text-danger">Please select a reason</label></p>';
}
<div class="form-group">
<label>Select Reason for Request</label>
<select id="ReasonList" name="ReasonList" class="form-control" value="<?php echo $ReasonList; ?>" />
<option value = "">Select...</option>
<option value = "1">Original Engineer has left the company</option>
<option value = "2">Actively involved in field work on customer site</option>
<option value = "3">No capacity due to customer mandated deadlines</option>
<option value = "4">Exception request by manager</option>
</select>
</div>
If option is selected then able to submit the form. If option is not selected it will give the error in the if statement - Please select a reason

See the corrected <select> element, and how a bit of code indentation make the HTML easier to read and debug.
<div class="form-group">
<label>Select Reason for Request</label>
<select id="ReasonList" name="ReasonList" class="form-control">
<option value = "">Select...</option>
<option value = "1">Original Engineer has left the company</option>
<option value = "2">Actively involved in field work on customer site</option>
<option value = "3">No capacity due to customer mandated deadlines</option>
<option value = "4">Exception request by manager</option>
</select>
</div>
The <select> element does not have a value property and you accidentally closed the <select> before the name attribute.

Related

Comparing HTML Option value with php variable

I've written this piece of code:
<select name="account_type" id="account" class="form-control" required>
<option value="Asset">Asset</option>
<option value="Bank">Bank</option>
<option value="Capital">Capital</option>
<option value="Cash" <?php if($_POST['Cash'] == $ledger_account){?> selected <?php }?>>Cash</option>
<option value="Expense">Expense</option>
<option value="Income">Income</option>
<option value="Liability">Liability</option>
<option value="Creditors">S. Creditors</option>
</select>
I got a value from different page, which is stored in $ledger_account. However, I want to see an option (for example, Cash) to be selected here when the page loaded if the option value matches with $ledger_account value. Please help me to identify what I've done wrong here.
Your code are correct. but it's give some error..
then try to somting like this...
<option value="Cash" <?php if($_POST['Cash'] == $ledger_account){ ?> selected="selected" <?php }?> >Case</option>
I am not sure but i think this useful for you

Whats the possibility of one updating a database field with a form having a drop down without changing the value of the drop down field

Lets say i input data into a mysql database using a drop down like:
<div>
<input name="name" type="text" id="name" placeholder="Enter Name"></div>
<div>
<select name="position" id="position" type="text">
<option selected="selected">Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
With a normal text field, i can simply include the value field to show on the form the initially entered data in the table. And change that so when i submit form, it updates the related field.
<div>
<select name="position" id="position" type="text" value="<?php echo $row_position['Position']; ?>>
<option selected="selected">Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
So assuming i do not want to update the field relating to the drop down, how do i it? Because what happens is, once i submit the form without selecting any option from the drop down, it picks the first option from the list which is the "Select" option and uses that to update the related field in the database.
I'd suggest at least two steps to make it user-friendly and achieve your goals:
First, make the initially entered selection selected if available:
<?php
$positions = Array("A", "B", "C");
?>
<div>
<select name="position" id="position">
<option value="-1">Select</option>
<?php
foreach($positions as $v) {
$selected = ($row_position['Position']===$v) ? "selected" : "";
echo "<option value='$v' $selected>$v</option>";
}
?>
</select>
</div>
And on the receiving php-script check if you've received a valid option, otherwise send error-message:
<?php
//other stuff...
if($_POST['position']>0) {
// save that value
} else {
// send error to user
}
// even more stuff..
?>
Notes: in select tag there is no type=text nor a value=anything available.
Assuming you are fetching dropdown options from database.
Note: Array keys, variables are only for demonstration purposes. This may differ from your actual records.
<div>
<select name="position" id="position">
<?php foreach($options as $option): ?>
<option value="<?= $option['position'] ?>" <?php if($option['position'] == $current_record['position']) { echo "selected"; } ?>> <!-- e.g. $option['id'] -->
<?= $option['name'] ?>
</option>
<? endforeach; ?>
</select>
</div>
What i have done that has solved the issue was to simply add the value from the database to the selectedoption or the first option field. So by default without changing the drop down, the first option with the value from the database is selected. And so there would be no change.
<div>
<div><?php echo $row_position['Position']; ?></div>
<select name="position" id="position">
<option value="<?php echo $row_position['Position']; ?>Select to Change</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
This gives a simple or straight forward solution to the problem.
Will still try the other suggestions to see how it all plays out. Thanks guys.

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;
}

Posting the selected html option attribute via php

Posting the selected html option attribute via php..Here is my html code and I'm trying to select one option from the event-drop down menu, but really finding it hard to configure with php variable $event. Any help will be much appreciated.
HTML Code:
<div>
<h4>Event</h4>
<p class="cd-select icon">
<select class="budget">
<option value="0">Select Occasion</option>
<option value="1">alpha</option>
<option value ="2">Bravo</option>
<option value="3">Charlie</option>
<option value="4">Delta</option>
</select>
</p>
</div>
PHP version
$event = $_POST['selected'];
In PHP form fields are posted with their name. In your case you'd have to add the name attribute to the select.
<select class="budget" name="selected">
<option value="0">Select Occasion</option>
<option value="1">alpha</option>
<option value ="2">Bravo</option>
<option value="3">Charlie</option>
<option value="4">Delta</option>
</select>

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>

Categories