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>
Related
<?php
$sql=mysqli_query($mysqli,"select * from calls where eid='$aid'")or die(mysqli_error($mysqli));
while($row2=mysqli_fetch_assoc($sql))
{
?><select name='city'>
<option value="">Select City</option>
<?php
$c=explode(',', $row2['city']);
foreach ($c as $c1)
{
?>
<option value='$c1' <?php if(isset($_POST['search']) || isset($_POST['next'])|| isset($_POST['submit']))
{
if(($c==$c1))
{ echo 'Selected';}
}?>><?php echo $c1;?></option>"
<?php }
echo"</select>";
}
?>
calls table city is stored as comma separated array. I have successfully get it into drop-down box. Now I want that whenever I submit or page refresh it holds se selected drop-down value.
You should check for isset($_POST["city"]) && $_POST["city"] == $city
foreach ($cities as $city)
{
$selection = (isset($_POST["city"]) && $_POST["city"] == $city ? 'selected':'' );
echo ' <option value="'.$city.'" '.$selection.' >'.$city.'</option>';
}
You only need to compare the value of $c1 against its previous value returned by the form submit, after checking that this execution is the result of the form being posted.
<?php
$sql = "select * from calls where eid=?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('s', $aid)
$stmt->execute();
$result = $stmt->get_result();
while($row2 = $result->fetch_assoc()) {
?>
<select name='city'>
<option value="">Select City</option>
<?php
$c=explode(',', $row2['city']);
foreach ($c as $c1) {
$sel = ''; // init value
if ( $_SERVER['REQUEST_METHOD'] == 'POST' && $c1 == $_POST['city'] ) {
$sel = "selected='selected'";
}
echo "<option $sel value='$c1' >$c1</option>";
}
echo "</select>";
}
?>
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>
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;
}
i already made a box in which skills are multiselected if we type something in text box,but i want to add those skills which are not inside my database that user type.
my question is how i make that?
my code is-
<span class="pf-title">skills</span>
<div class="pf-field no-margin">
<select id="lstFruits"multiple="multiple"class="chosen"name="skill[]"
data-placeholder="Please Select Skill">
<?php
$fetch="SELECT * FROM `skill`";
$record=mysqli_query($conn,$fetch);
while($records_row=mysqli_fetch_array($record))
{?>
<option value="<?php echo $records_row['skill_name'];?>"<?
php foreach($sk as $s){if($records_row['skill_name'] == $s)
{ echo 'selected="selected"';}}?>><?php echo
$records_row['skill_name'];?></option>
<?php }
?>
</select>
Try this way:-
<select id="lstFruits" multiple="multiple" class="chosen" name="skill[]" data-placeholder="Please Select Skill">
<?php
$fetch="SELECT * FROM `skill`";
$record=mysqli_query($conn,$fetch);
$flag = false;
while($records_row = mysqli_fetch_array($record)){
foreach($sk as $s){
if($records_row['skill_name'] == $s) {
echo '<option value="'.$records_row['skill_name'].'" selected="selected">'.$records_row['skill_name'].'</option>';
$flag = true;
break;
}
}
if(!$flag) {
echo '<option value="'.$records_row['skill_name'].'">'.$records_row['skill_name'].'</option>';
}
$flag = false;
}
?>
</select>
I know only this method. this method is assume that you know the values in all <option>
<select name="agama" id="agama">
<option value="Islam"<?php if ($rows['agama'] === 'Islam') echo ' selected="selected"'>Islam</option>
<option value="Khatolik"<?php if ($rows['agama'] === 'Khatolik') echo ' selected="selected"'>Khatolik</option>
<option value="Protestan"<?php if ($rows['agama'] === 'Protestan') echo ' selected="selected"'>Protestan</option>
<option value="Hindu"<?php if ($rows['agama'] === 'Hindu') echo ' selected="selected"'>Hindu</option>
<option value="Buddha"<?php if ($rows['agama'] === 'Buddha') echo ' selected="selected"'>Buddha</option>
<option value="Lain-Lain"<?php if ($rows['agama'] === 'Lain-Lain') echo ' selected="selected"'>Lain-Lain</option>
</select>
.... the above code is example from other people not mine.
but My case is the <option> is select from database too.
I have 2 table, oav_event and oav_album
the oav_album has foreign key (event_id) from oav_event table
I want to check if row['event_id'] from oav_album table is equal to option value (from oav_event table) if true, then set selected="selected"
while($row = mysqli_fetch_assoc($result)) { ?>
<option value="<?php echo $row['event_id']; ?>" >Event: <?php echo $row['event_date']; ?> </option>
<?php } ?>
the option will change depend on change in database table, so I don't know the value in option. How should I do?
<select name="event_id">
<?php
$sql = "SELECT * FROM oav_event";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
$selected = "";
if($row['event_id'] == $Yourmatchvalue)
{
$selected = "selected";
}
?>
<option value="<?php echo $row['event_id']; ?>" selected="<?php echo $selected; ?>" >Event: <?php echo $row['event_date']; ?> </option>
<?php } ?>
</select>
may this helps your. you need to replace $Yourmatchvalue variable with your variable.
You can use $_GET as the method on your form and pass the id of the record using it:
while($row = mysqli_fetch_assoc($result)) {
if (!empty($_GET['event_id']) && $row['event_id'] == $_GET['event_id']) {
$selected = 'selected = "selected"';
} else {
$selected = '';
}
echo '<option '.$selected.' value="'.$row["event_id"].'">'.$row["event_date"].'</option>';
}
Here is a solution,
$selected_value = 'Hindu'; // This will come from database
Change option tag with this
<option value="<?php echo $row['event_id']; ?>" <?php echo ($row['event_id'] == $selected_value) ? 'selected="selected"' : ''; ?> >Event: <?php echo $row['event_date']; ?> </option>
Create one function which will create options list like this:
function setDropdownValue($selectQueue_list,$selectedVal)
{
$queueVal = '';
$selectQueue_list_res=$db->query($selectQueue_list);
while($selectQueue_list_res_row=$db->fetchByAssoc($selectQueue_list_res))
{
$val = $selectQueue_list_res_row['id'];
$name = $selectQueue_list_res_row['name'];
if($val == $selectedVal)
{
$queueVal .= "<option value='$val' selected='selected' label='$name'>$name</option>";
}
else
{
$queueVal .= "<option value='$val' label='$name'>$name</option>";
}
}
return $queueVal;
}
Then create a query:
$get_value_query="SELECT id, name FROM table";
$dropdown_selected_value = !empty($dropdown_value) ? $dropdown_value: ''; // Pass value which you want to be selected in dropdown
Then call this function:
$dropdown_options = setDropdownValue($get_value_query, $dropdown_selected_value);
Later when you get dropdown options in $dropdown_options, use jquery to populate the dropdown, like this:
$('#dropdown_select_id').html("$dropdown_options");
Give it a try, and let me know.