cant remove a selection list - php

When run the code bellow shows a dropdown list with multiple choices where you need to select one option in it to be able to press the next button on the page where the code is.
I want to remove the code bellow from the page entirely, but if i just delete it i get an error saying "Please select x to continue", it behaves like nothing was selected from the dropdown list.
So, could i somehow to something like $option = "1"; to make the value of the string connected to the list always be 1 even if i remove the code ? i tried with $shortUrlDomain = "1"; but i still get "Please select x to continue if i remove the code.
Also, if i don't remove the code i don't get the error saying
""Please select x to continue" of course.
CODE:
<select id="shortUrlDomain" name="shortUrlDomain" style="width: 100%;">
<?php
foreach ($shortUrlDomains AS $k => $shortUrlDomain)
{
// active domains only
if($shortUrlDomain['status'] != 'enabled')
{
continue;
}
echo '<option value="' . (int) $k . '"';
// selected option
if ($k == (int) $_REQUEST['shortUrlDomain'])
{
echo 'SELECTED';
}
echo '>';
echo $shortUrlDomain['domain'];
'</option>';
}
echo '</optgroup>';
?>
</select>
OUTPUT:
<select id="shortUrlDomain" name="shortUrlDomain" style="width: 100%;">
<option value="1">someting.com
<option value="2">someting2.com
<option value="5">someting3.com
<option value="8">someting4.com
</optgroup>
</select>

// selected option
$selectedk = (int) $_REQUEST['shortUrlDomain'];
if ($k == $selectedk>0?$selectedk:1)
{
echo 'SELECTED';
}
OR BETTER
// selected option
$selectedk = (int) $_REQUEST['shortUrlDomain'];
if (($selectedk && $k == $selectedk) || (!$selectedk && array_key_first($shortUrlDomains)))
{
echo 'SELECTED';
}
This should help:
<select id="shortUrlDomain" name="shortUrlDomain" style="width: 100%;">
<?php
$forceAndShowOnlyK = 1;
foreach ($shortUrlDomains AS $k => $shortUrlDomain)
{
// active domains only
if($shortUrlDomain['status'] != 'enabled' || $k != $forceAndShowOnlyK)
{
continue;
}
echo '<option value="' . (int) $k . '"';
// selected option
if (($forceAndShowOnlyK && $k == $forceAndShowOnlyK) || (!$forceAndShowOnlyK && $k == (int) $_REQUEST['shortUrlDomain']))
{
echo 'SELECTED';
}
echo '>';
echo $shortUrlDomain['domain'];
echo '</option>';
}
echo '</optgroup>';
?>
</select>

I believe you have a validation condition somewhere else which is giving you that Please select x to continue message.
If you don't want to tweak it properly, you can try removing that code inside your <select> and add a default option like this:
<select id="shortUrlDomain" name="shortUrlDomain" style="width: 100%;">
<option value="1" selected>something.com</option>
</select>

Related

Select/Option values as NULL

What I need is if I choose first option from a list, which is --------------- and press submit button then they should return a NULL value in my database.
I tried to put this in my code:
if ($_POST['fk_KOMANDAid_KOMANDA'] === '') {
$_POST['fk_KOMANDAid_KOMANDA'] = NULL;
}
But it's not working, my page show me same problem, that fk_KOMANDAid_KOMANDA badly entered.
There is my code:
<p>
<label class="field" for="fk_KOMANDAid_KOMANDA">Komanda<?php echo in_array('fk_KOMANDAid_KOMANDA', $required) ? '<span> *</span>' : ''; ?></label>
<select id="fk_KOMANDAid_KOMANDA" name="fk_KOMANDAid_KOMANDA">
<option value="-1">---------------</option>
<?php
$kom = $asmuoObj->getkomanda();
foreach($kom as $key => $val) {
$selected = "";
if(isset($data['fk_KOMANDAid_KOMANDA']) && $data['fk_KOMANDAid_KOMANDA'] == $val['id']) {
$selected = " selected='selected'";}
echo "<option{$selected} value='{$val['id']}'>{$val['pavadinimas']}</option>";}
?>
</select>
</p>
UPDATE 1
That how my code looks like now, but my page showed me an errors.
<p>
<?php
if ($_POST['fk_KOMANDAid_KOMANDA'] == '-1') {
$_POST['fk_KOMANDAid_KOMANDA'] = NULL;}
?>
<label class="field" for="fk_KOMANDAid_KOMANDA">Komanda<?php echo in_array('fk_KOMANDAid_KOMANDA', $required) ? '<span> *</span>' : ''; ?></label>
<select id="fk_KOMANDAid_KOMANDA" name="fk_KOMANDAid_KOMANDA">
<option value="-1">---------------</option>
<?php
$kom = $asmuoObj->getkomanda();
foreach($kom as $key => $val) {
$selected = "";
if(isset($data['fk_KOMANDAid_KOMANDA']) && $data['fk_KOMANDAid_KOMANDA'] == $val['id']) {
$selected = " selected='selected'";}
echo "<option{$selected} value='{$val['id']}'>{$val['pavadinimas']}</option>";}
?>
</select>
</p>
You have set value -1 to the option.
Two options:
1) Change option value to blank "".
<option value="">---------------</option>
2) At PHP submit side, add if condition
if ($_POST['fk_KOMANDAid_KOMANDA'] == -1) {
$fk_KOMANDAid_KOMANDA = NULL;
}
You are not setting your condition right. You need to put -1, not empty in your if statement. Because when selected ------------ it's value is -1.
if ($_POST['fk_KOMANDAid_KOMANDA'] == '-1') {
$_POST['fk_KOMANDAid_KOMANDA'] = NULL;
}

Could set selected value in drop down dynamically using php

I need to select value dynamically using PHP in drop down list but in my case its not working as expected. I am explaining my code below.
<?php
$citySelected = "";
if(!isset($_GET['city'])){
$citySelected = 'selected="selected"';
}
?>
<select id="selectedLoc" name="selectedLoc" class="chosen-select form-control">
<option value="">Select City</option>
<option value="0" <?php echo $citySelected; ?>>Global</option>
<?php
foreach ($locationArr as $key => $value) {
$id = $value['id'];
$city = $value['city'];
$location = $value['location'];
$selected = "";
if(isset($_GET['city']) && $_GET['city']== $value['id']) {
$selected ='selected="selected"';
}
echo "<option value='$id' $selected>$location</option>";
}
?>
</select>
Here I need when there is any query string value then it will match with respective id and select that option and if there is no query string value at all then the global option will select.
Inside if condition ';' is missing. It should be like
if(isset($_GET['city']) && $_GET['city']== $value['id']){echo ' selected="selected"';}else{echo '';}
You can also use conditional operator for code simplicity, eg:-
<?php echo (isset($_GET['city']) && $_GET['city']== $value['id'])? ' selected="selected"':''; ?>

how to keep selected item in selectbox after page refreshing in PHP

I have made many select box in PHP and I want to keep selected item as selected after refreshing the page. (when selecting same select box or another) here is my code.
$selectbox='<select class="form-control" name="estate_id" onchange="this.form.submit()" style="width: 200px" >';
$est_name = $client ->call('get_estate'); // call method from web services
$_SESSION['estname'] = array();
$_SESSION['estname'] = $est_name;
$count = count($_SESSION['estname']);
$i = 0;
foreach ($_SESSION['estname'] as $row)
{
$id = $_SESSION['estname'][$i]['est_id'];
$name = $_SESSION['estname'][$i]['est_name'];
if($id == isset($_POST['estate_id']))
{
$isSelected = ' selected="selected"';
}
else {
$isSelected = '';
}
$selectbox.= "<option value=".$id.$isSelected.">".$name."</option>";
$i++;
}
$selectbox.='</select>';
echo $selectbox;
<select name="name">
<option <?php if ($_GET['name'] == 'a') { ?>selected="true" <?php }; ?>value="a">a</option>
<option <?php if ($_GET['name'] == 'b') { ?>selected="true" <?php }; ?>value="b">b</option>
</select>

Using a PHP variable to determine a HTML select box choice

I have a select box that when the page loads it sets a value selected based upon a variable. I also have 2 IF statements that checks the variable and returns the results. Here is my code I am trying to do:
<select id="satbreak1" name="satbreak1">
<?php if ($trimmed_title[0] == "Guest")
{
echo ('<option selected value="#">Select One</option>');
echo ('<option value="y">Yes</option>');
echo ('<option value="n">No</option>');
}
if ($trimmed_title[0] != "Guest")
{
echo ('<option <?php if($trimmed_satBreakfast[0] == "") echo ("selected ") ; ?> value="#">Select One</option>');
echo ('<option <?php if($trimmed_satBreakfast[0] == "y") echo ("selected ") ; ?> value="y">Yes</option>');
echo ('<option <?php if($trimmed_satBreakfast[0] == "n") echo ("selected ") ; ?> value="n">No</option>');
}
?>
</select>
What I need to do is if $trimmed_title[0] == "Guest" then echo out the first part, but if $trimmed_title[0] != "Guest") then echo out the second part and also check what the status of $trimmed_satBreakfast[0] is and set the selected value.
What is the best / correct way to handle this?
The first part works fine but having an php script in a echo seems to fail.
I think this might do the trick - not tested but theoretically looks ok
<select id="satbreak1" name="satbreak1">
<?php
if ( $trimmed_title[0] == "Guest" ) {
echo '
<option selected value="#">Select One</option>
<option value="y">Yes</option>
<option value="n">No</option>';
} else {
$tsb=$trimmed_satBreakfast[0];
echo '
<option selected value="#" '.( $tsb=='' ? 'selected' : '' ).'>Select One</option>
<option value="y" '.( $tsb=='y' ? 'selected' : '' ) .'>Yes</option>
<option value="n"'.( $tsb=='n' ? 'selected' : '' ).'>No</option>';
}
?>
</select>
Something like this:
<?php
$options = array(
'values' => array('', 'y', 'n'),
'titles' => array('Select One', 'Yes', 'No')
)
$index = $trimmed_title[0] == 'Guest' ? 0 : array_search($trimmed_satBreakfast[0], $options['values']);
//if ($index === false) $index = 0;
$optHtml = array();
foreach($options['values'] as $i => $val) {
$selected = $index == $i ? 'selected' : '';
$optHtml[] = "<option $selected value=\"$val\">{$options['titles'][$i]}</option>";
}
$optHtml = implode("\n", $optHtml);
?>
<select id="id" name="name">
<?php echo $optHtml?>
</select>
or any other style to split html from php:
//if ($index === false) $index = 0;
...
?>
<select id="id" name="name">
<?php foreach($options['values'] as $i => $val) {?>
<option <?=$index==$i?'selected':''?> value="<?=$val?>">
<?=$options['titles'][$i]?>
</option>
<?php } ?>
</select>
In this case HTML not used in the common php code, but php used in HTML (only general template features: loop through data and echo it).
Common purpose of this is possibility to extract template part to separate file.
It is already PHP so you have to remove the php tags
echo ('<option '.($trimmed_satBreakfast[0] == "") ? : '"selected"' : ''.' value="#">Select One</option>');
echo ('<option '.($trimmed_satBreakfast[0] == "y") ? : '"selected"' : ''.' value="y">Yes</option>');
echo ('<option '.($trimmed_satBreakfast[0] == "n") ? : '"selected"' : ''.' value="n">No</option>');

Remember Dropdown Selection

I've got a few select lists on my form which look a bit like this:
<option value="400000" <?php if($_GET['MaxPrice'] == "400000") { echo("selected"); } ?>>£400,000</option>
As you can see (above) I've got a bit of PHP in there telling the form to remember it's selection upon submit.
Is there a short bit of PHP that would remember every selection without the rather heavy method I'm using above?
UPDATE:
<select name="MaxPrice" id="MaxPrice">
<option value="9999999" selected>Price (Max)</option>
<option value="100000" <?php if($_GET['MaxPrice'] == "100000") { echo("selected"); } ?>>£100,000</option>
<option value="200000" <?php if($_GET['MaxPrice'] == "200000") { echo("selected"); } ?>>£200,000</option>
<option value="300000" <?php if($_GET['MaxPrice'] == "300000") { echo("selected"); } ?>>£300,000</option>
<option value="400000" <?php if($_GET['MaxPrice'] == "400000") { echo("selected"); } ?>>£400,000</option>
</select>
UPDATE 2: Is there a way to implement this technique into some JavaScript?
if (BuyRent=='buy')
document.SearchForm.MaxPrice.options[9999999]=new Option("Price (Max)","150000000");
document.SearchForm.MaxPrice.options[100000]=new Option("\u00A3100,000","100000");
document.SearchForm.MaxPrice.options[200000]=new Option("\u00A3200,000","200000");
document.SearchForm.MaxPrice.options[300000]=new Option("\u00A3300,000","300000");
document.SearchForm.MaxPrice.options[400000]=new Option("\u00A3400,000","400000");
Usually, you use some kind of loop. Here's an example :
$my_values = array(100000, 200000, 300000, 400000);
foreach($my_values as $value) {
echo "<option value=\"{$value}\"";
echo ($_GET['MaxPrice'] == $value) ? 'selected="selected"';
echo ">" . number_format($value, 0, '.', ',') . "</option>";
}
Which would print 4 tags such as <option value="100000" selected="selected">100,000</option>.
This code also uses the ternary operator, but it's obviously not mandatory, you can write the whole if/else statement if wanted.
Edit:
<select name="MaxPrice" id="MaxPrice">
<option value="9999999">Price (Max)</option>
<?php
$my_values = array(100000, 200000, 300000, 400000);
foreach($my_values as $value) {
echo "<option value=\"{$value}\"";
echo ($_GET['MaxPrice'] == $value) ? 'selected="selected"' : "";
echo ">£ " . number_format($value, 0, '.', ',') . "</option>";
}
?>
</select>
good question, it made me wonder if there exists a js plugin to remember form field out there.
And the answer seems to be "Yes!"

Categories