How to echo php and html of dropdown list? - php

I have problem with my codes and I'm still new to php. Please help me :)
echo "<td><select>
<option value='1'<?php if($row['Staf_Kamp'] == '1') { ?> selected='selected'<?php } ?>>1</option>;
<option value='2'<?php if($row['Staf_Kamp'] == '2') { ?> selected='selected'<?php } ?>>2</option>;
<option value='3'<?php if($row['Staf_Kamp'] == '3') { ?> selected='selected'<?php } ?>>3</option>;
</select></td>";
I am expecting the dropdown list that I have selected before edit will be selected in the edit page. But it is not working.

When echoing, you are already in a php context, so there is no need to use <?php ?> tags again. You can just concatenate the variables in your string.
echo "<td><select>
<option value='1'" . ($row['Staf_Kamp'] == '1' ? ' selected="selected"' : '') . ">1</option>;
<option value='2'" . ($row['Staf_Kamp'] == '2' ? ' selected="selected"' : '') . ">2</option>;
<option value='3'" . ($row['Staf_Kamp'] == '3' ? ' selected="selected"' : '') . ">3</option>;
</select></td>";

Try this:
<td><select>
<option value='1'<?php if($row['Staf_Kamp'] == '1') { echo ' selected'; } ?>>1</option>
<option value='2'<?php if($row['Staf_Kamp'] == '2') { echo ' selected'; } ?>>2</option>
<option value='3'<?php if($row['Staf_Kamp'] == '3') { echo ' selected'; } ?>>3</option>
</select></td>
You only need ; when working inside the <?php ?> tags so strip those out. Also save the opening and closing by just echoing out the selected value. Also the correct syntax is selected and not selected='selected'

you can not echo a string and put php code into the string
correct code:
<td>
<select>
<option value='1'<?php if($row['Staf_Kamp'] == '1') { ?> selected='selected'<?php } ?>>1</option>
<option value='2'<?php if($row['Staf_Kamp'] == '2') { ?> selected='selected'<?php } ?>>2</option>
<option value='3'<?php if($row['Staf_Kamp'] == '3') { ?> selected='selected'<?php } ?>>3</option>
</select>
</td>

Related

Highlight selected option from dropdown

Iam using PHP codeigniter framework. Value is set to select dropdown as below
<select class="form-control";>
<option <?php if($d1 == 'native') { echo 'selected'; }?> value='native' >native</option>
<option <?php if($d1 == 'migrated') { echo 'selected'; }?> value='migrated' >migrated</option>
</select>
for each row in table there is select option like above. I want to highlight option with color if option is set to migrated on page load.
I tried like below
<select class="form-control";>
<option <?php if($d1 == 'native') { echo 'selected'; }?> value='native' >native</option>
<option <?php if($d1 == 'migrated') { echo 'selected'; echo 'style="<color:red>"' }?> value='migrated' >migrated</option>
</select>
Your are not using proper syntax for style; you are missing a semicolon; and selected and style will be together making it useless. Try:
<select class="form-control">
<option <?php if($d1 == 'native') { echo 'selected'; }?> value='native' >native</option>
<option <?php if($d1 == 'migrated') { echo 'selected style="background-color:red"' }?> value='migrated' >migrated</option>
</select>
Although to me it is easier to read inline conditions as the following:
<select class="form-control">
<option <?php echo $d1 == 'native' ? 'selected' : ''; }?> value='native' >native</option>
<option <?php echo $d1 == 'migrated' ? 'selected style="background-color:red"' : ''; }?> value='migrated' >migrated</option>
</select>
your styling is wrong
<select class="form-control";>
<option <?php if($d1 == 'native') { echo 'selected'; }?> value='native' >native</option>
<option <?php if($d1 == 'migrated') { echo 'selected'; echo 'style="color:red"'; }?> value='migrated' >migrated</option>
</select>
you don't need the brackets <> inside the inline style and don't forget semicolon ;

PHP/HTML Showing selected option in dropdown from sql query

I wasn't sure how to title my question, I'm basically asking if anyone knows a better way to approach this. I'm using this function below to show which option is pulled from the DB, I'm wondering if there is a more compact way or better way to do this other than elseif each ID
function access_option($access)
{
if ($access == 0)
{
echo '<option value="0" selected>Member</option>
<option value="1">Streamer</option>
<option value="2">Moderator</option>
<option value="3">Manager</option>
<option value="4">Administrator</option>
<option value="5">Senior Administrator</option>';
}
elseif ($access == 1)
{
echo '<option value="0">Member</option>
<option value="1" selected>Streamer</option>
<option value="2">Moderator</option>
<option value="3">Manager</option>
<option value="4">Administrator</option>
<option value="5">Senior Administrator</option>';
}
elseif ($access == 2)
{
echo '<option value="0">Member</option>
<option value="1">Streamer</option>
<option value="2" selected>Moderator</option>
<option value="3">Manager</option>
<option value="4">Administrator</option>
<option value="5">Senior Administrator</option>';
}
elseif ($access == 3)
{
echo '<option value="0">Member</option>
<option value="1">Streamer</option>
<option value="2">Moderator</option>
<option value="3" selected>Manager</option>
<option value="4">Administrator</option>
<option value="5">Senior Administrator</option>';
}
elseif ($access == 4)
{
echo '<option value="0">Member</option>
<option value="1">Streamer</option>
<option value="2">Moderator</option>
<option value="3">Manager</option>
<option value="4" selected>Administrator</option>
<option value="5">Senior Administrator</option>';
}
elseif ($access == 5)
{
echo '<option value="0">Member</option>
<option value="1">Streamer</option>
<option value="2">Moderator</option>
<option value="3">Manager</option>
<option value="4">Administrator</option>
<option value="5" selected>Senior Administrator</option>';
}
}
Is this the best way to do it?
You can do it with less repetition using ternary operators:
function access_option($access)
{
echo '<option value="0"'.($access == 0 ? ' selected' : '').'>Member</option>';
echo '<option value="1"'.($access == 1 ? ' selected' : '').'>Streamer</option>';
echo '<option value="2"'.($access == 2 ? ' selected' : '').'>Moderator</option>';
echo '<option value="3"'.($access == 3 ? ' selected' : '').'>Manager</option>';
echo '<option value="4"'.($access == 4 ? ' selected' : '').'>Administrator</option>';
echo '<option value="5"'.($access == 5 ? ' selected' : '').'>Senior Administrator</option>';
}
You can loop over the values, which are held in an array, and set the selected value dynamically. This method makes it easy to add/remove options in the future by adjusting the array -
function access_option($access)
{
$options = ["Member", "Streamer", "Moderator", "Manager", "Administrator", "Senior Administrator"];
foreach($options as $key=>$value)
{
echo '<option value="'.$key.'"'.($key==$access ? ' selected' : '').'>'.$value.'</option>';
}
}
Ternary operators will reduce the size of it as mentioned as another answer. You can also use the switch statement. Similar to if and elseif but easier and will reduce your code.
Example:
<?php
function access_option($access)
{
switch ($access) {
case 0: echo "access equals 0";
case 1: echo "access equals 1";
case 2: echo "access equals 2";
}
}
?>

Echo 'selected' in html5 dropdown form using PHP

I have a form that fetches user info into the $row variable so forms are populated if the row exists if the database, but then saves any new input through the $_POST global variable, in case the user makes a mistake elsewhere on the form. It all works great.
<label for="qualification2">Grade:</label>
<input type="text" name="qualification2" id="grade2" class="form-control"
value="<?php if(isset($_POST['qualification2'])) {
echo $_POST['qualification2'];
} else { echo ($row ['qualification2']); } ?>">
I would like to do similar using a selection dropdown box but the code doesn't seem to work the same with dropdown boxes and I wonder what I'm doing wrong or if this is even possible.
I've also tried echo from the $row variable but that doesn't work either
<?php if($row['qualMonth2'] == 'February') { echo ' selected'; } ?>
This is my code:
<select input type="text" name="qualMonth2" id="qualMonth2" value="Select Month">
<option value="January" <?php if(isset($_POST['qualMonth2']) == 'January') { echo ' selected'; } ?>>January</option>
<option value="February" <?php if(isset($_POST['qualMonth2']) == 'February') { echo ' selected'; } ?>>February</option>
<option value="March" <?php if(isset($_POST['qualMonth2']) == 'March') { echo ' selected'; } ?>>March</option>
<option value="April" <?php if(isset($_POST['qualMonth2']) == 'April') { echo ' selected'; } ?>>April</option>
<option value="May" <?php if(isset($_POST['qualMonth2']) == 'May') { echo ' selected'; } ?>>May</option>
<option value="June" <?php if(isset($_POST['qualMonth2']) == 'June') { echo ' selected'; } ?>>June</option>
<option value="July" <?php if(isset($_POST['qualMonth2']) == 'July') { echo ' selected'; } ?>>July</option>
<option value="August" <?php if(isset($_POST['qualMonth2']) == 'August') { echo ' selected'; } ?>>August</option>
<option value="September" <?php if(isset($_POST['qualMonth2']) == 'September') { echo ' selected'; } ?>>September</option>
<option value="October" <?php if(isset($_POST['qualMonth2']) == 'October') { echo ' selected'; } ?>>October</option>
<option value="November" <?php if(isset($_POST['qualMonth2']) == 'November') { echo ' selected'; } ?>>November</option>
<option value="December" <?php if(isset($_POST['qualMonth2']) == 'December') { echo ' selected'; } ?>>December</option>
</select>
isset validates to true or false based on if variable is set or not. You can't check the value just with isset.
Use a small utility function to check condition:
function checkSelected($value) {
if(isset($_POST['qualMonth2']) && $_POST['qualMonth2'] == $value) {
return true;
} else {
return false;
}
}
Your select element should be then:
<select input type="text" name="qualMonth2" id="qualMonth2" value="Select Month">
<option value="January" <?php echo checkSelected('January') ? 'selected':'' ?>>January</option>
<option value="February" <?php echo checkSelected('February') ? 'selected':'' ?>>February</option>
<option value="March" <?php echo checkSelected('March') ? 'selected':'' ?>>March</option>
<option value="April" <?php echo checkSelected('April') ? 'selected':'' ?>>April</option>
<option value="May" <?php echo checkSelected('May') ? 'selected':'' ?>>May</option>
<option value="June" <?php echo checkSelected('June') ? 'selected':'' ?>>June</option>
<option value="July" <?php echo checkSelected('July') ? 'selected':'' ?>>July</option>
<option value="August" <?php echo checkSelected('August') ? 'selected':'' ?>>August</option>
<option value="September" <?php echo checkSelected('September') ? 'selected':'' ?>>September</option>
<option value="October" <?php echo checkSelected('October') ? 'selected':'' ?>>October</option>
<option value="November" <?php echo checkSelected('November') ? 'selected':'' ?>>November</option>
<option value="December" <?php echo checkSelected('December') ? 'selected':'' ?>>December</option>
</select>
Lets look at what you wrote..
<option value="February" <?php if(isset($_POST['qualMonth2']) == 'February') { echo ' selected'; } ?>>February</option>
What you're saying is.. If there is a value set for qualMonth2 (true / false), compare that against a string..
So you're comparing true || false against a string.
isset($_POST['qualMonth2']) returns true // false.
So none will ever be selected.
Refactor it..
<?php
$month = "";
if(isset($_POST['qualification2'])){
$month = $_POST['qualification2'];
}
?>
Then use $month as the comparative value.
<option value="July" <?php if($month == 'July') { echo ' selected'; } ?>>July</option>
I prefer to perform these checks in Javascript (I prefer JQuery), I think it cleans up the code, but you could also simplify this greatly by using a loop with generated months.
Loop through 1-12 for output
<select input type="text" name="qualMonth2" id="qualMonth2" value="Select Month">
<?php
for($i=1; $i<=12; $i++) {
$dateObj = DateTime::createFromFormat('!m', $i);
$monthName = $dateObj->format('F');
echo "<value>" . $monthName . "</option>";
}
?>
</select>
This approach takes a DRY (Do not repeat) approach. If you need to modify something, you only need to modify 1 line of code versus 12.
Jquery for choosing value (defaults to January if $_POST['qualMonth2'] is not set):
<script>
$('#qualMonth2').val("<?php echo (isset($_POST['qualMonth2'])) ? $_POST['qualMonth2'] : 'January' ?>");
</script>
Take what you will from this answer, I just showed you two possibilities, you can certainly avoid Javascript by adding a check in the loop if you want.

Sticky dropdown selected only if there's error on submit

I try to make my dropdown selected only if error happen, and this is my script
<select name="usertype" id="usertype" class="form-control">
<option value="">Please choose the user right</option>
<option value="admin"<?php if(isset($error)
&& $_POST['usertype'] == 'admin' ? ' selected="selected"' : '');?>>
Admin
</option>
<option value="author"<?php if(isset($error)
&& $_POST['usertype'] == 'author' ? ' selected="selected"' : '');?>>
Author
</option>
<option value="public"<?php if(isset($error)
&& $_POST['usertype'] == 'public' ? ' selected="selected"' : '');?>>
Public
</option>
</select>
can anyone tell me the right way? because it doesn't work.
You're mixing up your ternary, its (condition) ? true : false. Here's a revised one:
<?php $usertype = array('admin', 'author', 'public'); ?>
<select name="usertype" id="usertype" class="form-control">
<option disabled selected>Please choose the user right</option>
<?php foreach($usertype as $val): ?>
<option
value="<?php echo $val; ?>"
<?php echo (isset($error, $_POST['usertype']) && $_POST['usertype'] == $val) ? 'selected="selected"' : ''; ?>
>
<?php echo ucfirst($val); ?>
</option>
<?php endforeach; ?>
</select>
try out this code:
<select name="usertype" id="usertype" class="form-control">
<option value="">Please choose the user right</option>
<option value="admin" <?php echo ((isset($error) && $_POST['usertype'] == 'admin') ? ' selected="selected"' : '');?>>Admin</option>
<option value="author" <?php echo ((isset($error)&& $_POST['usertype'] == 'author') ? ' selected="selected"' : '');?>>Author</option>
<option value="public" <?php echo ((isset($error) && $_POST['usertype'] == 'public') ? ' selected="selected"' : '');?>>Public</option>
</select>

PHP, HTML filter

i would like this select filter to show up only if Language count is more than 4
Any ideas?
Here is html select filter code:
<p>
Select date:
</p>
<form method="get" action="">
<select id="training_session" name="date"onchange=this.form.submit()>
<option value=""<?php if($_GET['date'] === '') echo 'selected' ?>>[All dates]</option>
<option value="April"<?php if($_GET['date'] === 'April') echo 'selected' ?>>April</option>
<option value="May"<?php if($_GET['date'] === 'May') echo 'selected' ?>>May</option>
<option value="June"<?php if($_GET['date'] === 'June') echo 'selected' ?>>June</option>
<option value="July"<?php if($_GET['date'] === 'July') echo 'selected' ?>>July</option>
<option value="August"<?php if($_GET['date'] === 'August') echo 'selected' ?>>August</option>
<option value="September"<?php if($_GET['date'] === 'September') echo 'selected' ?>>September</option>
<option value="October"<?php if($_GET['date'] === 'October') echo 'selected' ?>>October</option>
<option value="November"<?php if($_GET['date'] === 'November') echo 'selected' ?>>November</option>
</form>
<noscript><input type="hidden" value="filter"></noscript>
Here is PHP code:
if (isset($_GET['date']) && $_GET['date']) {
foreach ($training_sessions as $key => $session) {
if (date('F', strtotime($session['ZCS_b_date'])) !== $_GET['date']) {
unset($training_sessions[$key]);
So how do i make it to show up only if language count is more than 4. I have language select filter which has to stay there all the time, but i want date filter to show up only if language count is more than 4.
This is the language select filter code:
<p>
Select language:
</p>
<form method="get" action="">
<select id="training_session" name="lang" onchange=this.form.submit()>
<option value=""<?php if($_GET['lang'] === '') echo 'selected' ?>>[All languages]</option>
<option value="English" <?php if($_GET['lang'] === 'English') echo 'selected' ?>>English</option>
<option value="Portuguese"<?php if($_GET['lang'] === 'Portuguese') echo 'selected' ?>>Portuguese</option>
<option value="French"<?php if($_GET['lang'] === 'French') echo 'selected' ?>>French</option>
<option value="Italian"<?php if($_GET['lang'] === 'Italian') echo 'selected' ?>>Italian</option>
<option value="Japanese"<?php if($_GET['lang'] === 'Japanese') echo 'selected' ?>>Japanese</option>
</form>
<noscript><input type="hidden" value="filter"></noscript>
This is the php code for language select filter:
if (isset($_GET['lang']) && $_GET['lang']) {
foreach ($training_sessions as $key => $session) {
if ($session['training_language'] !== $_GET['lang']) {
unset($training_sessions[$key]);
So does anyone knows what im trying to achieve here? What i want to do.
Ok here we go..
This should give you the results you want
<html>
<head></head>
<body>
<form id="submitquery" method="get" action="">
<?php
$month=array('[All Dates]','January','February','March','April',
'May','June','July','August',
'September','October','November','December');
$language=array('[All Languages]','English','Portuguese','French','Italian','Japanese');
$changeLanguagejs='changeForm();';
$changeDatejs='this.form.submit();';
writeSelect($language,'Language',$changeLanguagejs);
if (isset($_GET['Language'])){
if($_GET['Language']!='[All Languages]'){
$training_sessions=filterLanguage($training_sessions);}
if (count($training_sessions)>=4){
writeSelect($month,'Date',$changeDatejs);
if (isset($_GET['Date'])){
if($_GET['Date']!='[All Dates]'){
$training_sessions=filterDate($training_sessions);}
showResults($training_sessions);}}
else{showResults($training_sessions);}}
?>
</form>
</body>
</html>
<?php
function writeSelect($values,$name,$changejs){
echo "\n".'<p>Select '.$name.':</p>';
echo "\n".' <select id="training_sessions_'.$name.'" name="'.$name.'" onchange='.$changejs.'>'."\n";
foreach($values as $value){
echo ' <option value="'.$value.'" ';
if (isset($_GET[$name]) && $_GET[$name]==$value){echo 'selected ';}
echo '>'.$value.'</option>'."\n";}
echo ' </select><br/><br/>'."\n";}
function filterLanguage($training_sessions){
foreach($training_sessions as $key => $session){
if($session['training_language']!=$_GET['Language']){
unset($training_sessions[$key]);}}
return $training_sessions;}
function filterDate($training_sessions){
foreach($training_sessions as $key => $session){
if(date('F', strtotime($session['ZCS_b_date']))!=$_GET['Date']){
unset($training_sessions[$key]);}}
return $training_sessions;}
function showResults($training_sessions){
echo "\n".'Showing '.count($training_sessions).' ';
if(count($training_sessions)==1){echo 'Result';}
else{echo 'Results';}
echo'<br/>'."\n\n";
print_r($training_sessions);}
?>
<script>
function changeForm(){
var selectLang = document.getElementById("training_sessions_Language").value;
document.getElementById("submitquery").method="post";
document.getElementById("submitquery").action="?Language="+selectLang;
document.getElementById("submitquery").submit();}
</script>
I'm going to edit out the comments to make it more readable...
if you want to see the comments go back through the edits of this answer

Categories