I want a checkbox to be checked by default, [duplicate] - php

This question already has answers here:
How can I set the default value for an HTML <select> element?
(34 answers)
Closed 6 years ago.
When will I update my semester that time by default a checkbox to be checked how it possible ?
<select name="semester">
<option value="">Select Semester</option>
<option value="1">Semester 1</option>
<option value="2">Semester 2</option>
<option value="3">Semester 3</option>
</select>

You can use the selected attribute.
<select name="semester">
<option value="">Select Semester</option>
<option value="1" selected="selected">Semester 1</option>
<option value="2">Semester 2</option>
<option value="3">Semester 3</option>
</select>

Please try this
<select name="semester">
<option value="">Select Semester</option>
<option value="1" <?php if($data['semester']==1){echo 'selected';}?>>Semester 1</option>
<option value="2" <?php if($data['semester']==2){echo 'selected';}?>>Semester 2</option>
<option value="3" <?php if($data['semester']==3){echo 'selected';}?>>Semester 3</option>
</select>

If you want to select semester 2 so here you go:
HTML:
<select name="semester">
<option value="">Select Semester</option>
<option value="1">Semester 1</option>
<option value="2" selected>Semester 2</option>
<option value="3">Semester 3</option>
</select>
Jquery:
$("select option").each(function(){
if ($(this).text() == "Semester 2")
$(this).attr("selected","selected");
});

yes,it possible. you have to use selected in <option>
<select name="semester">
<option value="">Select Semester</option>
<option value="1" selected>Semester 1</option>
<option value="2">Semester 2</option>
<option value="3">Semester 3</option>
</select>
DEMO

<option value="1" <?= ($value=='1')?'selected':"" ?>>Semester 1</option>
<option value="2" <?= ($value=='2')?'selected':"" ?>>Semester 2</option>
<option value="3" <?= ($value=='3')?'selected':"" ?>>Semester 3</option>
you can get the value form database or session,

We really need your specific PHP code to help, but perhaps this generic example will show you how to do this when you have your data and your desired value:
<?php
//Your array of data
$myData = array(
'1' => 'Semester 1',
'2' => 'Semester 2',
'3' => 'Semester 3');
//The value you want to be selected
$myValue = "2";
?>
<select name="semester">
<option value="">Select Semester</option>
<?php
foreach($myData as $key => $value){
echo ('<option value="'.$key.'"' );
if( $myValue == $key){
echo( ' selected="selected" ');
}
echo ('>'.$value.'</option');
echo "\n";
}
?>
</select>

Related

how to check if a dropdown list, multiple=multiple, is selected in php?

I have a multiple=multiple drop down list and I want to check if a user has selected any option or not using PHP code. If not, then I want to display an error message. How can I do that?
<select name="ddlskill" multiple="multiple">
<option value="Cake PHP">Cake PHP</option>
<option value="Database">Database</option>
<option value="hello">hello</option>
<option value="Java">Java</option>
<option value="Jquery">Jquery</option>
<option value="JSON">JSON</option>
<option value="JSP">JSP</option>
<option value="Mysql">Mysql</option>
<option value="PHP">PHP</option>
<option value="xyz">xyz</option>
<option value="XYZ2">XYZ2</option>
</select>
You should declare your html like this :
<select name="ddlskill[]" multiple="multiple">
<option value="Cake PHP">Cake PHP</option>
<option value="Database">Database</option>
<option value="hello">hello</option>
<option value="Java">Java</option>
<option value="Jquery">Jquery</option>
<option value="JSON">JSON</option>
<option value="JSP">JSP</option>
<option value="Mysql">Mysql</option>
<option value="PHP">PHP</option>
<option value="xyz">xyz</option>
<option value="XYZ2">XYZ2</option>
</select>
And then in PHP, you'll get an array of values :
if( !empty($_POST['ddlskill']) ){
print_r($_POST['ddlskill']);
// [PHP, JSP, Java, ...]
}
Maybe helpful,
<?php
if( isset($_POST) && !empty($_POST['ddlskill'])){
echo "<pre>";print_r($_POST['ddlskill']);
// [PHP, JSP, Java, ...]
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
<select name="ddlskill[]" multiple="multiple">
<option value="Cake PHP">Cake PHP</option>
<option value="Database">Database</option>
<option value="hello">hello</option>
<option value="Java">Java</option>
<option value="Jquery">Jquery</option>
<option value="JSON">JSON</option>
<option value="JSP">JSP</option>
<option value="Mysql">Mysql</option>
<option value="PHP">PHP</option>
<option value="xyz">xyz</option>
<option value="XYZ2">XYZ2</option>
</select>
<button type="submit"> Submit </button>
</form>
<?php
if(!empty($_POST['submit'])){
if(is_array($_POST['ddlskill']))
{
foreach($_POST['ddlskill'] as $key=>$value)
{
print_r($value);
}
}
else
{
print_r($_POST['ddlskill']);
}
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
<select name="ddlskill[]" multiple="multiple">
<option value="Cake PHP">Cake PHP</option>
<option value="Database">Database</option>
<option value="hello">hello</option>
<option value="Java">Java</option>
<option value="Jquery">Jquery</option>
<option value="JSON">JSON</option>
<option value="JSP">JSP</option>
<option value="Mysql">Mysql</option>
<option value="PHP">PHP</option>
<option value="xyz">xyz</option>
<option value="XYZ2">XYZ2</option>
</select>
<button type="submit" name="submit"> Submit </button>
</form>

how to insert the multi-select value in codeigniter

iam using codeigniter
<select id="example-label" multiple="multiple">
<option value="option-1">Option 1</option>
<option value="option-2">Option 2</option>
<option value="option-3">Option 3</option>
<option value="option-4">Option 4</option>
<option value="option-5">Option 5</option>
<option value="option-6">Option 6</option>
</select>`
how to insert multi-select value
<select id="example-label" multiple="multiple" name='youroption[]'>
<option value="option-1">Option 1</option>
<option value="option-2">Option 2</option>
<option value="option-3">Option 3</option>
<option value="option-4">Option 4</option>
<option value="option-5">Option 5</option>
<option value="option-6">Option 6</option>
</select>
Add This in Your Contoller
<?php
function get_data()
{
$optiondata=$this->input->post('youroption');
var_dump($optiondata);
}
?>
or extract using loop
foreach($optiondata as $key=>$value)
{
echo $value;
}

I have several dropdown list - I want to print out selected options of these dropdown lists

This is what I got so far, it's just the dropdown list. It's a form for making a reservation for appointment. I want to print out these selected values (= selected options of dropdown)
But I have no clue since I'm very new with PHP and HTML. Something with $_POST and $_GET ? Could I have some suggestions of solution? Much gratitude.
<html>
<head>
<title>title</title>
</head>
<body>
<select name="Ar" id="Ar">
<option value="1">2014</option>
<option value="2">2015</option>
<option value="3">2016</option>
<option value="4">2017</option>
<option value="5">2018</option>
<option value="6">2019</option>
</select>
<select name="Manad" id="Manad">
<option value="1">Januari</option>
<option value="2">Februari</option>
<option value="3">Mars</option>
<option value="4">April</option>
<option value="5">Maj</option>
<option value="6">Juni</option>
<option value="7">July</option>
<option value="8">Augusti</option>
<option value="9">September</option>
<option value="10">Oktober</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="Dag" id="Dag">
<option value="1">Mandag</option>
<option value="2">Tisdag</option>
<option value="3">Onsdag</option>
<option value="4">Torsdag</option>
<option value="5">Fredag</option>
</select>
<select name="Tid" id="Tid">
<option value="1">07:00</option>
<option value="2">08:00</option>
<option value="3">09:00</option>
<option value="4">10:00</option>
<option value="5">11:00</option>
<option value="6">12:00</option>
<option value="7">13:00</option>
<option value="8">14:00</option>
<option value="9">15:00</option>
<option value="10">16:00</option>
<option value="11">17:00</option>
<option value="12">18:00</option>
</select>
<input type="submit" name="submit" id="submit" value="Boka" />
</body>
</html>
You're missing the <form> tag. It should be wrapped around all your form-data (select, input) etc. Then you will fetch it using either $_GET or $_POST or even $_REQUIRE
Example (change site.php to whatever you please):
<html>
<head>
<title>title</title>
</head>
<body>
<form method="post" action="site.php">
<select name="Ar" id="Ar">
<option value="1">2014</option>
<option value="2">2015</option>
<option value="3">2016</option>
<option value="4">2017</option>
<option value="5">2018</option>
<option value="6">2019</option>
</select>
<select name="Manad" id="Manad">
<option value="1">Januari</option>
<option value="2">Februari</option>
<option value="3">Mars</option>
<option value="4">April</option>
<option value="5">Maj</option>
<option value="6">Juni</option>
<option value="7">July</option>
<option value="8">Augusti</option>
<option value="9">September</option>
<option value="10">Oktober</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="Dag" id="Dag">
<option value="1">Mandag</option>
<option value="2">Tisdag</option>
<option value="3">Onsdag</option>
<option value="4">Torsdag</option>
<option value="5">Fredag</option>
</select>
<select name="Tid" id="Tid">
<option value="1">07:00</option>
<option value="2">08:00</option>
<option value="3">09:00</option>
<option value="4">10:00</option>
<option value="5">11:00</option>
<option value="6">12:00</option>
<option value="7">13:00</option>
<option value="8">14:00</option>
<option value="9">15:00</option>
<option value="10">16:00</option>
<option value="11">17:00</option>
<option value="12">18:00</option>
</select>
<input type="submit" name="submit" id="submit" value="Boka" />
</form>
</body>
</html>
PHP example:
if(isset($_POST['Ar'])) { // Make sure it's sent
$year = $_POST['Ar'];
echo "År: " . htmlentities($year); // Output (htmlentities for safety when outputting)
$month = $_POST['Manad'];
echo "Månad: " . htmlentities($month);
}

POST Variables on a FORM PHP

I'm posting variables from my form on to another form, all the variables that are being posted on an INPUT field are showing fine. When a variable is needed to post on a SELECT how can I do this?
I tried within the <SELECT> tags to use value="<?php echo $myvariable; ?>" but this returns a blank.
To set the selected option within a <select> tag, use the selected attribute. In this example, "Value 3" is selected:
<select name="myvariable">
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3" selected>Value 3</option>
</select>
We can use PHP with something like this:
<select name="myvariable">
<option value="1" <?php echo ($myvariable == '1' ? 'selected' : '')?>>Value 1</option>
<option value="2" <?php echo ($myvariable == '2' ? 'selected' : '')?>>Value 2</option>
<option value="3" <?php echo ($myvariable == '3' ? 'selected' : '')?>>Value 3</option>
</select>
Hope this helps :) x
Your usage is incorrect. Check select documentation over here.
Example usage
<!-- The second value will be selected initially -->
<select name="select">
<option value="value1">Value 1</option>
<option value="value2" selected>Value 2</option>
<option value="value3">Value 3</option>
</select>
So for your case you should say
<select name="select">
<option value="value1"><?php echo $myvariable; ?></option>
</select>

Re load dropdown menu on page load

I have an html form that I have been using to POST to a php form, this saves the posted data into the database. I am now loading the data back into the form when I click edit, this works fine for the text fields as I can just use
value="<?php echo $somefield ?>"
The problem is im not sure how to re select my dropdown menu. The menu looks like this and is used for selecting a category, but when the page reloads nothing happens and the dropdown isn't reloaded.
<select name="category" id="category">
<option>- Select A Category -</option>
<optgroup label="Tractors">
<option value="Tractor">Tractor</option>
<option value="Compact">Compact</option>
<option value="Forklift">Forklift</option>
<option value="Load All">Load All</option>
<option value="Loader">Loader</option>
<option value="Maniscopic">Maniscopic</option>
<option value="Masted">Masted</option>
<option value="Piviot">Piviot</option>
<option value="Telescopic">Telescopic</option>
<option value="UTV">UTV</option>
</optgroup>
<optgroup label="Machinery">
<option value="Attachments">Attachments</option>
<option value="Bale Handling">Bale Handling</option>
<option value="Cultivation Equipment">Cultivation Equipment</option>
<option value="Drills">Drills</option>
<option value="Feeders">Feeders</option>
<option value="Fertlizer Spreaders">Fertlizer Spreaders</option>
<option value="Grass Harrows">Grass Harrows</option>
<option value="Grassland Equipment">Grassland Equipment</option>
<option value="Hedge Cutters">Hedge Cutters</option>
<option value="Mowers">Mowers</option>
<option value="Muck Spreaders ">Muck Spreaders </option>
<option value="Ploughs">Ploughs</option>
<option value="Post Hammers">Post Hammers</option>
<option value="Power Harrows">Power Harrows</option>
<option value="Rakes">Rakes</option>
<option value="Rolls">Rolls</option>
<option value="Rotovators">Rotovators</option>
<option value="Shredders">Shredders</option>
<option value="Splitters">Splitters</option>
<option value="Sprayers">Sprayers</option>
<option value="Spreaders">Spreaders</option>
<option value="Tankers">Tankers</option>
<option value="Tedders">Tedders</option>
<option value="Toppers">Toppers</option>
<option value="Trailers">Trailers</option>
<option value="Wrappers">Wrappers</option>
</optgroup>
<optgroup label="Forklifts">
</optgroup>
</select>
<?php
echo '<select>';
foreach( $names as $name )
echo '<option value="'.$name.'" '.($name==$selected?' selected="selected"':'').'>'.$name.'</option>'
echo '</select>';
?>
When building the list the selected option should have the attribute selected="selected".

Categories