in an update, how to select an option from a select box? - php

Good afternoon,
Using cakephp, how can I select a value in a select box in an update operation?
in the view I get the variables like this for example:
<?php $category = $itemEditar['Anuncio']['category']; ?>
and I need to select an option from a select box:
<select id="select_category" name="enquiry">
<option value="" >selecione</option>
<option value="Category1">Categoria1</option>
<option value="Category2">Categoria2</option>
<option value="Category3">Categoria2</option>
</select>
to be an update operation, I need to mark the category that was saved in the database, and am not getting how to do this.

You want to check the $category against each or the options and if they match set the selected attribute .
<select id="select_category" name="enquiry">
<option value="" >selecione</option>
<option<?= $category == "Category1"?" selected = 'selected'":"" ?> value="Category1">Categoria1</option>
<option<?= $category == "Category2"?" selected = 'selected'":"" ?> value="Category2">Categoria2</option>
<option<?= $category == "Category3"?" selected = 'selected'":"" ?> value="Category3">Categoria2</option>
</select>

the correct answer is:
<select id="select_category" name="enquiry" >
<option value="Category1" <?php echo $category == "Category1"?" selected = 'selected'":"" ?> >Categoria1</option>
<option value="Category2" <?php echo $category == "Category2"?" selected = 'selected'":"" ?> >Categoria2</option>
<option value="Category3" <?php echo $category == "Category3"?" selected = 'selected'":"" ?> >Categoria2</option>
</select>

Related

How to pick a selected option from database

I have a select box like this:
<select name="id_category" id="">
<?php foreach($categorys as $category): ?>
<option value="<?= $category['id']?>"><?= $category['name']?></option>
<?php endforeach?>
</select>
There are three categorys: Residential, Comercial and Institutional.
How can I put it in a way that it already appears in the current selected category?
Having the selected category:
<option <? echo $selected == $category['id'] ? "selected " : "" ?>....

How to set the value for dropdown box?

i want to display the value for all the textbox with the help of this code
value="<?php `if(!empty($result) && !empty($result['part'])){ echo $result['part']; } ?>">
Where part is the name of the database field...same thing i have to display for dropdown box how can i do this..In dropdown box the options are displayed with the help of another table
<select class="bootstrap-select show-tick" data-size="5" data-live-search="true" data-width="100%" name="TName1" required>
<option value="" disabled selected>Select Sub Account Name</option>
<?php foreach ($accName as $row ): ?>
<option value="<?=$row['id']?>" <?php echo set_select('accName', $row['id']); ?>>
<?=$row['accName']?>
</option>
<?php endforeach ?>
</select>
where accName is the name stored in another table..i want to display the name here..but it had to be stored as code(predefined integer value) in my database field...How can i did this..??
Value for drop down is set different way than that of text box or text area.
Drop down has options and one of its options (multiple in case it is a multiple dropdown) is set to be selected one.
So, <option> syntax:
<option value="value">Text</option>
If we want to make an option selected, use selected' attribute of`
<option value="value" selected="selected">Text</option>
So, in your case, solution would be:
1) Take value from database.
2) In the options loop, compare database value with current iteration <option> value.
3) If any of options' values are matching, use selected attribute of <option>
<select class="bootstrap-select show-tick" data-size="5" data-live-search="true" data-width="100%" name="TName1" required>
<option value="" disabled selected>Select Sub Account Name</option>
<?php foreach ($accName as $row ):
$selected = ($dbValueForDropDown == $row['id']) ? 'selected="selected"' : '';
?>
<option value="<?=$row['id']?>" <?php echo set_select('accName', $row['id']); ?> <?php echo $selected;?>><?=$row['accName']?></option>
<?php endforeach ?>
</select>
you should bring id and name of the accounts in the same results set by join in the query you send to the database and then run on the results and put the id in the value and the name in the display
<select class="bootstrap-select show-tick" data-size="5" data-live-search="true" data-width="100%" name="TName" required placeholder="select account name">
<option value="" disabled selected>Select Account Name</option>
<?php foreach ($accName as $row ):
$selected = '';
if($row['id'] == $result['code']){
$selected = 'selected = selected';
}
?>
<option <?php echo $selected; ?> value="<?=$row['id']?>" <?php echo set_select('accName', $row['id']); ?>>
<?=$row['accName']?></option>
<?php endforeach ?>
</select>

Dropdownlist validation not working

I've try several times for dropdownlist validation,
my codes are in PHP and HTML, the the validation part is not working though and i've refer some of the solutions in stackoverflow which has similar case like mine.
Declare variable
$call_department = $db->escape((int)$_POST['call_department']); //where i declare this variable
HTML files
<tr><td>Department</td><td><select name='call_department'>
<option></option>
<?php $call_dept = $db->get_results("select type_id,type_name from site_types where type=1 order by type_name;");
foreach ($call_dept as $dept )
{?>
<option value='<?php echo $dept->type_id;?>'><?php echo $dept->type_name; required?></option>
<?php } ?>
</select></td></tr>
Validation part:
<?php
if(isset($_REQUEST['call_department']) && $_REQUEST['call_department'] == '0') {
echo 'Please select a department.';
}
?>
1) Set first option value="0" like this
<option value="0">select</option>
2) Required attribute should be set to select tag not to option tag <select name='call_department' required>
<select name='call_department' required>
<option value="0">select</option>
<?php $call_dept = $db->get_results("select type_id,type_name from site_types where type=1 order by type_name;");
foreach ($call_dept as $dept )
{
?>
<option value='<?php echo $dept->type_id;?>'><?php echo $dept->type_name; ?></option>
<?php } ?>
</select>

Whats the best way to show selected options on a form when reloaded using PHP

I would like the select option the user selected to be shown when the form is reloaded. I am currently using bootstrap select picker to style the drop down. Right now I am creating an array and echoing out a selected class based on the value stored in the database.
This is not a major problem with a small amount of options like the code provided below, however with a select box with larger amounts of options this obviously amounts to lots of extra code.
There must be a smarter way to do this than the code i'm using below. Can anyone help?
<select class="selectpicker" name="Do_You_Have_A_Job_Spec_You_Can_Email">
<option value="" <?php if ($user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email'] == '') echo "selected='selected'"; ?>>Please Select</option>
<option value="Yes" <?php if ($user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email'] == 'Yes') echo "selected='selected'"; ?>>Yes</option>
<option value="No" <?php if ($user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email'] == 'No') echo "selected='selected'"; ?>>No</option>
</select>
If the option's are static you could use ternary operators, so it will be something like :
<?php $selected_option = $user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email']; ?>
<select class="selectpicker" name="Do_You_Have_A_Job_Spec_You_Can_Email">
<option value="" <?php echo $selected_option==''?'selected':''; ?>>Please Select</option>
<option value="Yes" <?php echo $selected_option=='Yes'?'selected':''; ?>>Yes</option>
<option value="No" <?php echo $selected_option=='No'?'selected':''; ?>>No</option>
</select>
Hope this helps.
<?php
$text = $user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email'];
?>
<select class="selectpicker" name="Do_You_Have_A_Job_Spec_You_Can_Email">
<option value="">Please Select</option>
<option value="Yes" <?php echo $text == 'Yes' ? 'selected' : ''; ?>> Yes </option>
<option value="No" <?php echo $text == 'No' ? 'selected' : ''; ?>> No </option>
</select>
Alternative Way
"..however with a select box with larger amounts of options this
obviously amounts to lots of extra code.".
In this case, when you are having lots of data for dropdown. You can create a table where you can have both option name and option value.
job_specification_table
id option_value option_name
1 Please Select
2 Yes Yes
3 No No
.
.
.
Then, retreive those values and check for user_data and according to it, option will get selected.
<?php
$sql = "SELECT * FROM job_specification_table";
$result = mysqli_query($db_con,$sql);
$text = $user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email'];
?>
<select class="selectpicker" name="Do_You_Have_A_Job_Spec_You_Can_Email">
<?php
while ($row = mysqli_fetch_array($result)){
$option_value = $row['option_value'];
$selected = ($option_value == $text) ? "selected" : "";
?>
<option value="<?=$option_value;?>" <?=$selected;?>>
<?=$row['option_name'];?>
</option>
<?php }?>
</select>
Here is more readable solution:
<select class="selectpicker" name="Do_You_Have_A_Job_Spec_You_Can_Email">
<option value=
<?php
$choise = $user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email'];
switch ($choise) {
case 'Yes':
echo '"Yes" selected="selected" >Yes';
break;
case 'No':
echo '"No" selected="selected" >No';
break;
case '':
echo '"" selected="selected" >Please Select';
break;
}
?>
</option>
</select>

Html Select Box Get value from mysql

Hi I have a stored value in mysql from a select box. When i call a edit page I would like the select box to populate with the value i have stored in mysql
$taskCompany = $mysqli->real_escape_string($_POST['taskCompany']); This returns 1
<select id="taskCompany" required name="taskCompany" class="form-control">
<option value="" disabled selected>Select a Company</option>
<option value="1">building</option>
<option value="2">maintenace</option>
</select></div>
I would like the select box to show the correct option when loading the edit page. I do not store any select option in the database values or text just the value that is select when job was created.
Thanks Jon
I'd I have understood correctly, you have he value/values in your MySQL database and you want them to be shown in your drop down list:
You could do it like:
<option value = "1" <?php if($taskCompany == 1) echo "select='selected'";?>>building</option>
Or if you have the names coming from the database too.
<select id="taskCompany" required name="taskCompany" class="form-control">
<option>SELECT</option>
<?php
$res = musql_query("SELECT * FROM yourtablename");
while($row = mysql_fetch_array($res))
{
$taskCompany = $row['taskCompany'];
$co_name = $row['taskCompanyName'];
?>
<option value = "<?php echo $taskCompany; ?>" ><?php echo co_name; ?></option>
<?php } ?>
Here I have used $co_name to be displayed as a name assuming that you have building, maintenance etc stored in your database.
This is for the time being, as I wanted to demonstrate but Please
consider mysqli/PDO as mysql_* is deprecated.
<?php $taskCompany = $mysqli->real_escape_string($_POST['taskCompany']); ?>
<select id="taskCompany" required name="taskCompany" class="form-control">
<option value="" disabled selected>Select a Company</option>
<option value="1" <?php if($taskCompany == 1) echo "selected='selected'"; ?>>building</option>
<option value="2" <?php if($taskCompany == 2) echo "selected='selected'"; ?>>maintenace</option>
</select></div>
You can do this
<?php foreach($taskCompany as $row){ ?>
<option value="unique id for select options">name for select options</option>
<?php } ?>

Categories