Generate dropdown menu dynamically and change php variables on option select - php

I am trying to get dropdown menu to set a value to a php variables on option selected. I can print the menu dynamically with the code below:
<?php
$option_to_preselect = $package_name;
$ddown_options = $package_name;
$arrlength = count($ddown_options);
print '<select name = "package" id = "pack_select" class="select-submit2">';
for ($i=1; $i < $arrlength; $i++)
{
if ($ddown_options[$i]==$option_to_preselect)
{
print ' <option value ="' . $ddown_options[$i] . '"' . ' selected ="selected">' . $ddown_options[$i] . '</option>';
$packageID = $i;
$packagePrice = $packages_price[$i];
} else {
print ' <option value="' . $ddown_options[$i] . '">' . $ddown_options[$i] . '</option>';
$packageID = $i;
$packagePrice = $packages_price[$i];
}
}
print '</select>';
?>
Then when the user selects an option, the variables $packageID and $packagePrice should obtain the id and the amount of the selected package.
The idea is to pass the values in a href where the href is:
href="<?php echo site_url('package_purchase/'.$lang_code.'/'.$packageID.'/'.$packagePrice); ?>"
The output of the $package_name is:
array (size=4)
'' => string '' (length=0)
1 => string 'Free' (length=4)
2 => string 'Basic' (length=5)
3 => string 'Premium' (length=7)
and the result of the code looks like:
<select name="package" id="pack_select" class="select-submit2">
<option value="Free">Free</option>
<option value="Basic">Basic</option>
<option value="Premium">Premium</option>
</select>

Related

Isset with multi select dropdown and bootstrap

I have a wordpress site with a search form that searches for posts based on the form field selections, for custom fields etc. It works fine, however on the search results page I have an exact copy of the form except that I am trying to preset the form selections based on the search query/url string.
I am using a regular select dropdown and I have set it to "multiple" so that it can use bootstraps multiselect with the checkboxes. I've asked a similar question HERE but that was for checkboxes and even though the bootstrap multiselect uses checkboxes, I still have to work with the select dropdown first.
So after trying several things, I've come close but have ran into several problems. In the code below I made notes to further explain exactly what I mean.
<select name="property_type[]" id="pt-multi" class="form-control multi-select2" multiple="multiple">
<?php
$terms = get_terms( "property-type", array( 'hide_empty' => 0 ) );
$count = count($terms);
if ( $count > 0 ){
echo "<option value='Any'>All</option>";
foreach ( $terms as $term ) {
if (isset($_GET['property_type'])) {
foreach ($_GET['property_type'] as $proptypes) {
// FIRST EXAMPLE
$selected .= ($proptypes === $term->slug) ? "selected" : ""; // shows first correct selected value but also selects everything after it up until the second correct value, which it doesn't select.
//$selected = ($proptypes === $term->slug) ? "selected" : ""; // shows only last correct selected value
//if ($proptypes === $term->slug) { $selected = 'selected'; } // shows first correct selected value then selects every value after, even if it wasn't selected
// SECOND EXAMPLE
//$selected .= ($proptypes === $term->slug) ? "selected" : ""; // shows first correct selected value then selects every value after, even if it wasn't selected
//$selected = ($proptypes === $term->slug) ? "selected" : ""; // shows only last correct selected value
//if ($proptypes === $term->slug) { $selected = 'selected'; } // shows first correct selected value then selects every value after, even if it wasn't selected
}
}
echo "<option value='" . $term->slug . "' " . $selected . ">" . $term->name . "</option>"; // FIRST EXAMPLE
//echo "<option value='" . $term->slug . "' " . ($selected?' selected':'') . ">" . $term->name . "</option>"; // SECOND EXMAPLE
}
}
?>
</select>
Create and array and use in_array() to check.
<select name="property_type[]" id="pt-multi" class="form-control multi-select2" multiple="multiple">
<?php
$terms = get_terms("property-type", array('hide_empty' => 0));
$count = count($terms);
// Setup an array of $proptypes
$proptypes = array();
if (isset($_GET['property_type'])) {
foreach ($_GET['property_type'] as $proptype) {
$proptypes[] = $proptype;
}
}
if ($count > 0) {
echo "<option value='Any'>All</option>";
foreach ($terms as $term) {
$selected = (in_array($term->slug, $proptypes)) ? 'selected' : '';
echo "<option value='" . $term->slug . "' " . $selected . ">" . $term->name . "</option>";
}
}
?>
</select>

Preset currency value on select codeigniter

I have a select box with currency 1 and 2, currency 1 is for Euro. I want my select box to preset the value 1 which is euro.
I am using controller to get the currency information like this.
$data = array(
'sexList' => $this->localization->getTextParamValues(null, 'user', 'Sex'),
'countryList' => $this->localization->getCountryList(),
'currencyList' => $this->localization->getCurrencyList(),
'languageList' => $this->localization->getLanguageList(),
'user' => $userExist,
'userinfo' => $this->user_profile->getUserInfo(),
'Currency' => $this->localization->getCompanyCurrency()
);
$this->load->view('header', $this->history->getPreviousPageInArray());
$this->load->view('borrow_registerpage_view',$data);
The currency is pushing the value one to the data array, but in the html file it doesnt preset automatically. Dont know what is wrong.
Here is the HTML code, I need to preset the value 1 in this table so it selects 1 normally which is EURO.
<?php
echo '<select name="currency_select" id="my_profile_select_currency" class="form-control country_style">';
if(!($user['Currency'] > 0)){
echo '<option value="0">'.lang("myprofile_pinfo_no_currency_set").'</option>';
foreach ($currencyList as $currency){
echo '<option value="' . $currency->ID. '">' . $currency->Name . '</option>';
}
}else{
foreach ($currencyList as $currency){
echo '<option value="' . $currency->ID. '"';
if($currency->ID==$user['Currency']){
echo 'selected';
}
echo '>' . $currency->Name .'';
echo '</option>';
}
}
echo '</select>'
?>
You Need to change your view
$opt = array();
foreach ($currencyList as $currency){
$opt[$currency->ID] = $currency->Name;
}
echo form_dropdown('currency_select', $opt, set_value('currency_select', $user['Currency'], 'id="my_profile_select_currency" class="form-control country_style"');
Try this. It will show EURO selected only if $user['Currency'] is greater than 0
<?php
echo '<select name="currency_select" id="my_profile_select_currency" class="form-control country_style">';
if(!($user['Currency'] > 0)){
echo '<option value="0">'.lang("myprofile_pinfo_no_currency_set").'</option>';
foreach ($currencyList as $currency){
echo '<option value="' . $currency->ID. '">' . $currency->Name . '</option>';
}
}else{
foreach ($currencyList as $currency){
echo '<option value="' . $currency->ID. '" '.$currency->Name == 'EURO' ? 'selected="yes"' : ''.'>' .$currency->Name. '</option>';
}
}
echo '</select>'
?>
If you need in both case you may use $currency->Name == 'EURO' ? 'selected="yes"' : '' in both foreach

Country list selection box - Set one as selected

I have a country list of every country in a form to get a parcel quote.
When a user presses the "Get Quote" button, all the text forms retain the information previously entered using PHP.
How can I do this with the country list box? As I can't have PHP on every option checking if that is the country selected and adding "Selected" to the html.
Is there a better way other than generating the country list from a file in a loop?
EDIT:
Going for the method of looping through a file, and checking..
This is what I have so far:
$countries = fopen("includes/countries.txt", "r");
$countries = explode(";", $countries);
Then in the HTML:
<select id="countries" name="countries">
<?php
foreach ($countries as $country){
echo("<option value=\"" . $country . "\">" . $country . "</option>");
}
?>
</select>
Not yet finished.
I assume you have an array with your countries stored. You could try something like this:
$countries = array('Albania', 'Egypt');
$selected_country_id = $_GET['c_id']; // You may need to change this to match with your code
$country_selected = array();
foreach($countries as $country) {
if($country['id'] == $selected_country_id) {
$country_selected[ $country['id'] ] = ' selected ';
} else {
$country_selected[ $country['id'] ] = '';
}
}
Then, assuming that you dynamically add your Select-Options, do this:
// In your each-fn
echo '<option value="' . $country['id'] . '" ' . $country_selected[ $country['id'] ] . '>' . $country['name'] . '</option>';
Something like this would be better
foreach ($countries as $country) {
?>
<option value="<?php echo $country" <?php echo ($country == $_POST['country'] ? 'selected' : ''; ?>><?php echo $country; ?></option>
<?php
}
<select id="countries" name="countries">
<?php
foreach ($countries as $country){
if(isset($_POST["country"]) && $_POST["country"] == $country){
$sel = "selected";
}else { $sel= ""; }
echo("<option value=\"" . $country . "\"" .$sel.">" . $country . "</option>");
}
?>
</select>
Get All Country-State-City Selectbox ....!!!
See Link : GitHub

PHP Get value from drop down option (int) type

I'm having problem with capturing the value from drop down list. This is my code for drop down list which is product id.
<select name="id" id="id" ONCHANGE="location =this.options[this.selectedIndex].value;" >
<option value="---">---</option>
<?php
foreach( $products as $ID => $info ){
echo '<option value="new_purchase.php?id=' . $ID . '"';
echo isset( $id ) && $id == $ID ? ' selected="selected"' : '';
echo '>' . $info['id'] . '</option>'; }
?>
</select>
And this is my code for capture the value.
$id = $mysqli->real_escape_string ($_POST['$("#id:selected").val()'] );
The query run but it stores 0 as product id value no matter what I chose.
Any helps would be appreciated.
You're mashing up PHP and JavaScript. You just need the name of the form field in your $_POST array:
$id = $mysqli->real_escape_string($_POST['$("#id:selected").val()']);
should be:
$id = $mysqli->real_escape_string($_POST['id']);
UPDATE
and change
echo '<option value="new_purchase.php?id=' . $ID . '"';
to
echo '<option value="' . $ID . '"';
UPDATE Part Deaux
and change
ONCHANGE="location =this.options[this.selectedIndex].value;"
to
ONCHANGE="location = 'new_purchase.php?id=' + this.options[this.selectedIndex].value;"
Since you are not submitting a form and just changing a window.location your POST array will be empty. Instead you should read GET parameter, not POST:
$id = $mysqli->real_escape_string($_GET['id']);
try this :
<select name="id" id="id" ONCHANGE="location ='http://site.com/new_purchase.php?id='+this.options[this.selectedIndex].value;" >
<option value="---">---</option>
<?php
foreach( $products as $ID => $info ){
echo '<option value="'.$ID .'"';
echo isset( $id ) && $id == $ID ? ' selected="selected"' : '';
echo '>' . $info['id'] . '</option>'; }
?>
</select>
i guess your database is expecting integer for the column of product id, right? so it is a mismatch of data type.

Refilling a select box with POST data (php)

I have a select box that shows 3 options: option1, option2, option3. When a user hits submit, then in $_POST I do have the value selected. Is there an easy way to redisplay the select box with the chosen option highlighted WITHOUT it being repeated in the options?
In other words, if option2 is selected and submit is clicked, the page should display again with option2 selected, and option1 and option 3 underneath.
Thanks.
<?php
$arrValues = array(...);
$selectedValue = (isset ($_POST['selectName']) ? $_POST['selectName'] : "");
?>
<select name="selectName">
<?php
for ($i = 0; $i < count($arrValues); $i++)
{
$opts = ($arrValues[$i] == $selectedValue) ? ' selected="selected"': '';
echo '<option value="' . $arrValues[$i] . '"' . $opts . '>' . $arrValues[$i] . '</option>';
}
?>
</select>
Create your options like this.
$options = array("optionvalue" => "Option Name");
foreach($options as $value => $name)
{
if(isset($_POST['select_box']))
{
if($_POST['select_box'] == $value)
{
echo '<option selected="selected" value="'.$value.'">'.$name.'</option>';
continue;
}
}
echo '<option value="'.$value.'">'.$name.'</option>';
}
When you generate the select box, use the POST data (if available) to pick the item that's selected (and/or to sort the items).
Kind of like:
if($_POST["optval"] == $opt) $sel = "selected='selected'"; else $sel = "";
print "<option value='$opt' " . $sel . ">$opt</option>";
Naturally you'd want to verify that the POST data is valid and that it exists (isset). Assuming of course that you generate your select box from data accessible by PHP, rather than statically define it.

Categories