Selecting Data from select tag - html

i have to select data from select tag rather then selecting value as its selects value for default
<select name="time" >
<option selected="selected" >timings</option>
<option value="155">9:00AM - 12:00PM</option>
<option value="244">12:00AM - 15:00PM</option>
</select>
I want to select 12:00AM - 15:00PM values and store it in my DB. How to do it any ideas.
Thanks in advance
Ameeth

<select name="time">
<option selected="selected" >timings</option>
<option value="9:00AM - 12:00PM">9:00AM - 12:00PM</option>
<option value="12:00AM - 15:00PM">12:00AM - 15:00PM</option>
</select>
<?php
$value = $_POST["time"]; //what method you are using
?>
or
<select name="time" >
<option selected="selected" >timings</option>
<option value="155">9:00AM - 12:00PM</option>
<option value="244">12:00AM - 15:00PM</option>
</select>
<?php
$value = "";
switch($_POST['time']){
case '155':
$value="9:00AM - 12:00PM";
break;
case '244':
$value = "12:00AM - 15:00PM";
break;
default:
$value = "No data found";
break;
}
?>

You've two options;
Use 12:00AM - 15:00PM inside the value="" parameter.
OR
Do something like this in PHP file for collecting data and inserting it into db;
if ($_POST['time'] == "155")
{
$time = '9:00AM - 12:00PM';
}
elseif ($_POST['time'] == "244")
{
$time = '12:00AM - 15:00PM';
}
// do rest of the data insertion into db

Related

Dropdown option getting selected when it should not

Current code for selecting birthday dates
<label>Birthday</label>
<select name="dob-year" id="dob-year" class="form-control">
<option value="" disabled>Year</option>
<option value="" disabled>----</option>
...
<option value="1971" {{date("Y", strtotime($friend->birthday)) == '1971' ? 'selected' : ''}}>1971</option>
<option value="1970" {{date("Y", strtotime($friend->birthday)) == '1970' ? 'selected' : ''}}>1970</option>
<option value="1969" {{date("Y", strtotime($friend->birthday)) == '1969' ? 'selected' : ''}}>1969</option>
...
</select>
<select name="dob-month" id="dob-month" class="form-control">
<option value="" disabled>Month</option>
<option value="" disabled>-----</option>
<option value="01" {{date("F", strtotime($friend->birthday)) == 'January' ? 'selected' : ''}}>01</option>
<option value="02" {{date("F", strtotime($friend->birthday)) == 'February' ? 'selected' : ''}}>02</option>
...
</select>
<select name="dob-day" id="dob-day" class="form-control">
<option value="" disabled>日</option>
<option value="" disabled>---</option>
<option value="01" {{date("d", strtotime($friend->birthday)) == '01' ? 'selected' : ''}}>01</option>
<option value="02" {{date("d", strtotime($friend->birthday)) == '02' ? 'selected' : ''}}>02</option>
...
</select>
What I want to do
is to add {{$friend->birthday == null ? 'selected' : ''}} like below in each Year/Month/Day tags
<select name="dob-year" id="dob-year" class="form-control">
<option value="" disabled>Year</option>
<option value="" {{$friend->birthday == null ? 'selected' : ''}} disabled>----</option>
...
</select>
The problem
This a weird one I cant seem to find the issue that is causing this.
Ive disabled all javascript and double checked what is being returned from controller too but cannot find the source of the issue.
The default selected values of Year/Month/Day is always 1970/01/01 even if I hard code selected on other options. For example <option value="" selected>Year</option> would have it only select 1970.
If I delete the whole 1970 option tag only then everything works fine. So for this to work I had to delete 1970 option tag, 01 January option tag and 01 day option tag. Which is no use...
Does anyone have an idea what could be causing this?
This is happening due to the format you are giving to strtotime() function, when giving any unsupported format to
echo date('Y/d/m',strtotime("20/03/03"));
echo date('Y:d:m',strtotime(null))
it will return you default value which is `1970/01/01`
you are passing either wrong format or null as parameter to strtotime()
Suggestion
You can do something like this, where you want to show the dropdown get dbirthday into a variable and check if it is null or not, and then update your $year, $day, $month value in that case if value is null it will not select any value and on right value it will select the option is correct.
#php
$friendBD = '';
$day = '';
$month = '';
$year = '';
#endphp
#foreach ($friends as $friend)
#php
$friendBD = $friend->birthday
#endphp
#if(!is_null($friendBD))
#php
$date = \Carbon\Carbon::create($friendBD);
$month = $date->format('M');
$day = $date->format('d');
$year = $date->format('Y');
#endphp
#endif
<label>Birthday</label>
<select name="dob-year" id="dob-year" class="form-control">
<option value="" disabled>Year</option>
<option value="" disabled>----</option>
<option value="1971" >1971</option>
<option value="1970" {{$year == '1970' ? 'selected' : ''}}>1970</option>
<option value="1969" {{$year == '1969' ? 'selected' : ''}}>1969</option>
<option value="2020" {{$year == '2020' ? 'selected' : ''}}>2020</option>
</select>
#endforeach
Your PHP might be invalid
http://sandbox.onlinephpfunctions.com/code/bb07478fd5cc5da9535750524631d0c78763fe11
If you have
$friend['birthday'] = "1980/02/01";
or
$friend = array("birthday"=>"1980/02/01");
then you need to access the birthday like this
echo strtotime($friend["birthday"]);
So your statements need to look like this
echo date("Y", strtotime($friend["birthday"]));
echo date("F", strtotime($friend["birthday"]));
echo date("d", strtotime($friend["birthday"]));
If you do NOT access the string correctly, your will get 1970, January, 01 for any date. In my case since I am in GMT+1, I get 1969, December, 31 for the "0" date resulting from an invalid date passed to strtotime
Alternatively use JavaScript:
const dob = "1971/02/01" // "<?= $friend["birthday"] ?>";
const [yyyy,mm,dd] = dob.split("/")
document.getElementById("dob-year").value=yyyy;
document.getElementById("dob-month").value=mm;
document.getElementById("dob-day").value=dd;
<label>Birthday</label>
<select name="dob-year" id="dob-year" class="form-control">
<option value="" disabled>Year</option>
<option value="" disabled>----</option>
<option value="1971">1971</option>
<option value="1970">1970</option>
<option value="1969">1969</option>
</select>
<select name="dob-month" id="dob-month" class="form-control">
<option value="" disabled>Month</option>
<option value="" disabled>-----</option>
<option value="01" 01</option>
<option value="02">02</option>
</select>
<select name="dob-day" id="dob-day" class="form-control">
<option value="" disabled>日</option>
<option value="" disabled>---</option>
<option value="01">01</option>
<option value="02">02</option>
</select>

How can I insert variable into SQL query only if it exist?

Let's say I have such html:
<form id="form">
Level:
<br/>
<select id="userLevel" name="userLevel">
<option value="">Choose level</option>
<option value="novice">novice</option>
<option value="intermediate">intermediate</option>
<option value="advanced">advanced</option>
</select>
Days per week:
<select id="days" name="days">
<option value="">Days per week:</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<input type="button" id="btn" value="Submit"/>
</form>
<br />
<div id="txtHint"><b>Table goes here...</b></div>
Also I have a database with "user_level" and "days_per_week" columns. I want my code to show certain rows from that table, depends on wich parametrs user has chosen. But, if he didn't choose one of them, 'days' for example, program must display rows depending on only chosen parameters and dismising not chosen ones. How can I constuct SQL query properly? Something to check if that variable was set and if it wasn't - neglect it.
$userLevel = $_POST['userLevel'];
$days = $_POST['days'];
$sql="SELECT *
FROM table
WHERE ifset(user_level){
user_level = $userLevel
} else {
neglect this variable
}
AND ifset(days_per_week){
days_per_week= $days
} else {
neglect this variable
}"
EDIT: that table has about dozen of columns, and eventually that sql query will operate with several variables. I brought two of them just for example.
Here is your code :
$q = "";
if(isset($_POST['userLevel']) && $_POST['userLevel'] != "") {
$userLevel = $_POST['userLevel'];
$q .= " and user_level = '$userLevel'";
}
if(isset($_POST['days']) && $_POST['days'] != "") {
$days = $_POST['days'];
$q .= " and days_per_week= '$days'";
}
$sql="SELECT *
FROM table
WHERE id is not null $q"

How to indicate the default value in a dropdown menu

I have a form containing a number of dropdown menu's. Each dropdown has a default option. I know how to set this default value, but I'm not sure on how to indicate this value, so that people know that is the default.
I create the dropdown like this
<?php
for ($mismatch = 0; $mismatch <= 3; $mismatch ++) {
if($mismatch == 1){
$select_mismatch .= "<option value = $mismatch selected > ".$mismatch." (default) </option>";
} else {
$select_mismatch .= "<option value = $mismatch > ".$mismatch." </option>";
}
}
?>
So on the web page, the dropdown looks like:
option 1 (default)
option 2
option 3
So now I indicate the default value by just adding (default) to it. Does anyone know another way to indicate this which is a bit more pleasing to the eye?
Try this:
<select data-placeholder="Please Select" class="required select-full" name="status" >
<option value=""></option>
<option value="1" <?php if ($post['status'] == "1") { ?> selected="true" <?php } ?>>Enable</option>
<option value="0" <?php if ($post['status'] == "0") { ?> selected="true" <?php } ?>>Disable</option>
</select>
maybe like this?
<select name="test">
<option>
1
</option>
<option>
2
</option>
<option selected>
3
</option>
<option>
4
</option>
<option>
5
</option>
</select>
Unless you mean to a drop down hover menu?
if so then you could add a colour to the default option.
you can use a CSS class to indicate.
JSFiddle DEMO
<select name="" id="">
<option value="1">please choose</option>
<option value="2" class="default">default</option>
<option value="3">other item</option>
</select>
.default{
background: #000;
color: #fff;
}

Change Database Output

I have a form setup and the data goes into a mysql database. There are several parts where the choice is both a numerical and a written rating - 1 - poor, 2 - fair, 3 - good, 4 - excellent. The field in the database is set to varchar with a length of 20. However, the output only returns the written portion - poor, fair, good, excellent, dropping the numerical. I want to either include the numerical with the written or just use the numerical itself. Can someone explain how I would set this up.
I am assuming the changes I need to make are in the php form itself - I am supplying a sample of the code and what I think I need to change - just want to verify before I do so.
<select name="programRating" class="pure-input-1">
<option value="">Select Rating...</option>
<option value="Poor" <?php if($_POST['programRating'] == "Poor") echo "selected"; ?>>1 - Poor</option>
<option value="Fair" <?php if($_POST['programRating'] == "Fair") echo "selected"; ?>>2 - Fair</option>
<option value="Good" <?php if($_POST['programRating'] == "Good") echo "selected"; ?>>3 - Good</option>
<option value="Excellent" <?php if($_POST['programRating'] == "Excellent") echo "selected"; ?>>4 - Excellent</option>
</select>
<select name="programRating" class="pure-input-1">
<option value="">Select Rating...</option>
<option value="Poor" <?php if($_POST['programRating'] == "1-Poor") echo "selected"; ?>>1 - Poor</option>
<option value="Fair" <?php if($_POST['programRating'] == "2-Fair") echo "selected"; ?>>2 - Fair</option>
<option value="Good" <?php if($_POST['programRating'] == "3-Good") echo "selected"; ?>>3 - Good</option>
<option value="Excellent" <?php if($_POST['programRating'] == "4-Excellent") echo "selected"; ?>>4 - Excellent</option>
</select>
The values which are stored in your database are the ones you specify in the "value" attribute of each "option".
So put the numbers in the "value" attribute instead of the text.

HTML select <option> returns only the first letter of selected option

I have a code somewhat like this
<?php
if($_POST['post'] == 'Post')
{
$cat = $_POST['cat'];
$update = "UPDATE table SET category='$cat' WHERE id = '$id' ";
$result = mysql_query($update) or die ("update error");
}
?>
<form action="#" method="post">
<select name="cat">
<option >Arts and Entertainment</option>
<option >Automotive</option>
<option >Book Reviews</option>
<option >Business</option>
<option >Communications</option>
<option >Computers and Technology</option>
<option >Finance</option>
</select>
<input type="submit" name="post" value="Post">
</form>
I summarized my code for you to get it easily,
whatever I select on my option,
only the First letter of the option comes out.
Your code seems ok on both PHP and HTML side.
So you might have store your value on category with its fieldtype varchar(1) or char(1).
If that is the case increase your char or varchar length to let say 50.