Display Dropdown in HTML with PHP - php

I am android developer and making one admin panel for one of my android application. I know very basic PHP. currently I am displaying input field with value and user can change with text input and can save value with submit button. Its like below
<div class="form-group">
<label class="col-md-3 control-label">Admob On/off :-</label>
<div class="col-md-6">
<input type="text" name="ads_status" id="ads_status" value="<?php echo $settings_row['ads_status'];?>" class="form-control">
</div>
</div>
Instead of input value I want show dropdown menu with two item called on and off. When user load page it should show value based on previous save. Like if user have made setting on it must display on. I am displaying value now from database. Let me know if anyone can suggest me what Should I do for it. Thanks

<div class="form-group">
<label class="col-md-3 control-label">Admob On/off :-</label>
<div class="col-md-6">
<select class="form-control " type="text" name="ads_status" >
<option value="on" <?php if($settings_row['ads_status']== 'on') echo "selected = 'selected'" ?> >On</option>
<option value="off" <?php if($settings_row['ads_status'] == 'off') echo "selected = 'selected'" ?> >Off</option>
</select>
</div>
</div>
I hope this will hep you.

Related

Different ways to display the value of a selected dropdown option in laravel

I am currently working with this site wherein if you are not a local from that country, multiple input fields will be disabled. So, what I am trying to do is to display the selected option from a dropdown that has been queried from a database table. ill provide the code for it and some pictures for you to better understand it.
Here is the Controller:
public function get_citizenship()
{
$citizenships = CitizenshipModel::orderBy('citizens')->get();
$opt="<option selected value''>Select Citizenship</option>";
foreach($citizenships as $citizenship)
{
if($citizenship -> id > 0)
$opt.="<option value={$citizenship->id}>{$citizenship->citizens}</option>";
}
return $opt;
}
public function citizenship(){
$data = UserModel::where('seq_id','=',Session::get('loginId'))->first();
$data['displayCitizenship']=$this->get_citizenship();
$data['displayProvince']=$this->get_province();
$data['displayMunicipality']=$this->get_municipality();
return view ("countries", $data);
}
And here is the Blade file. I have two types of select. one is locally where i coded the value, and the other one is the one i queried from a database table.
<div class="col-sm-12 col-lg-4">
<div class="form-group row">
<label for="step1_gender" class="col-sm-3 text-right control-label col-form-label">Gender</label>
<div class="col-sm-9">
<select class="form-control" type="date" id='step1_gender' >
<option>Select Gender</option>
<option value="male" >Male</option>
<option value="female" >Female</option>
</select>
</div>
</div>
</div>
<div class="col-sm-12 col-lg-4">
<div class="form-group row">
<label for="step1_citizenship" class="col-sm-3 text-right control-label col-form-label">Citizenship</label>
<div class="col-sm-9">
<select class="form-control" type="text" required="" placeholder="Last Name" id='step1_citizenship' >
{!! $displayCitizenship !!}
</select>
</div>
</div>
</div>
This is the one i chose and inserted in my users info table. but whenever i refresh the page, it returns to Select Citizenship option. how can i show the selected option for each logged in user?
I tried googling and looking for youtube vids but i can't find the nearest answer.
If you want the option of citizenship to be selected based on the current user's data (assuming you $this->get_citizenship() retrieves the correct data from the current user), you can simply add a turnery condition for the selected option.
$current_citizen = $this->get_citizenship();
foreach($citizenships as $citizenship)
{
if($citizenship -> id > 0) {
$selected = $citizenship->citizens == $current_citizen ? 'selected' : '';
$opt.="<option value={$citizenship->id} ${selected}>{$citizenship->citizens}</option>";
}
}

Refresh the page with new request on change select option

I am trying to achieve something via php.
Here there are couple of options.
<select class="custom-select" id="inputGroupSelect01">
<option selected="">Select Clusters ...</option>
<option value="math-related">Math Related</option>
<option value="science-related">Science Related</option>
</select>
There are Group of subject fields which will appear when the options are changed respectively.
<div class="form-row pb-5" id="math-related"">
<div class="form-group col-md-3">
<input type="number" class="form-control" id="num1" placeholder="English">
</div>
<div class="form-group col-md-3">
<input type="number" class="form-control" id="num2" placeholder="Math">
</div>
<div class="form-group col-md-3">
<input type="number" class="form-control" id="num3" placeholder="Science">
</div>
<div class="form-group col-md-3">
<input type="number" class="form-control" id="num4" placeholder="Computer">
</div>
</div>
When the option is changed to Math related OR Science Related option, the related 5-6 subject fields will be populated like image below. Some might have 5, some might have 8.
How to refresh the page and load the new sets of input fields according to selection done via PHP?
Example Image for fields
First of all change your select dropdown with your page url + parameter and make sure to replace YOUR_PAGE_URL with actual url as follows -
<select onChange="window.location.href=this.value" class="custom-select" id="inputGroupSelect01">
<option value="">Select Clusters ...</option>
<option value="YOUR_PAGE_URL?topic=math-related">Math Related</option>
<option value="YOUR_PAGE_URL?topic=science-related">Science Related</option>
</select>
After then just do a GET parameter checking on your page to populate your fields like -
if( issset($_GET['topic']) ){
if( $_GET['topic'] == 'math-related' ){
echo "Add your math related above 5-8 fields html here.";
}elseif( $_GET['topic'] == 'science-related' ){
echo "Add your science related above 5-8 fields html here.";
}
}

Spit data to the select field from the database

enter image description hereI am new to coding. I just started building a website and I am currently working on the profile page. I wanted the input fields to be populated by the information the user has provided (see attached picture). This I have achieved.
My problem now is... I can't spit information from the database to the select tag.
For the other input fields, I just had to echo the data spooled from the database as seen in my HTML below:
<div class="col-sm-6 white-bg">
<div class="row">
<div class="col-sm-6">
<label>
<span class="text-left">FIRST NAME</span>
<input type="text" name="fname" value="<?php echo $first_name ?>" disabled>
</label>
</div>
<div class="col-sm-6">
<label>
<span class="text-left">LAST NAME</span>
<input type="text" name="lname" value="<?php echo $last_name ?>" disabled>
</label>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<label>
<span class="text-left">GENDER</span>
<select name="gender">
<option value="" disabled hidden selected>--Select--</option>
<option value="Female">Female</option>
<option value="Male">Male</option>
<option value="Others">Others</option>
</select>
</label>
</div>
<div class="col-sm-6">
<label>
<span class="text-left">DATE OF BIRTH</span>
<input type="DATE" name="dob" value="<?php echo $dob ?>">
</label>
</div>
</div>
</div>
But I can't do the same for the select tag. How do I have an option selected based on the information the user has provided such that the Male option is selected if the user entered male or the the Female option is selected if the user entered Female.
You should add the keyword based on the value returned from your DB.
So in your code add something like
-- For your default select option do
<?php echo(empty($theGender)?" selected":""); ?>
-- For Male/Female do, checking for value you stored
<option value="Female"<?php echo(($theGender === "female")?" selected":""); ?> >Female</option>

Show variable on frontend based on if checkbox is checked

I hope I can explain this right but the image should make it clear (hopefully).
In the admin side of my site, I have different fields for products, but I do not want the fields shown to the frontend user if the show checkbox isn't checked. As seen in my image below.
Now my issue I am not getting is should I have a row name for every checkbox or can I use just one like show.
simplified can I use something like this
<div class="form-group">
<label class="col-md-3">Sales Price</label>
<div class="col-md-8">
<input type="text" name="price" value="<?php echo $row['price']; ?>" class="form-control" />
</div> <!-- /.col -->
<input type="checkbox" value="1" <?php echo ($row['show'] == 1) ? 'checked="checked"' : ''; ?> name="show">
</div> <!-- /.form-group -->
or do I have to use something like this
<div class="form-group">
<label class="col-md-3">Sales Price</label>
<div class="col-md-8">
<input type="text" name="price" value="<?php echo $row['price']; ?>" class="form-control" />
</div> <!-- /.col -->
<input type="checkbox" value="1" <?php echo ($row['showprice'] == 1) ? 'checked="checked"' : ''; ?> name="showprice">
</div> <!-- /.form-group -->
Here is what I am after using an image.
As for making the content show on the frontend Here is the code for that. This is set up now to NOT show if the data is missing. (I haven't set it up to show about the checkboxes until I figure out what to use)
<?php if (isset($model) && !empty($model)) : ?>
<li class="element element-text">
<strong>Model #: </strong>
<?php echo $model;?>
</li>
<?php endif; ?>
I thought about using something like this, but again I am not sure if I can use a single checkbox (show) for all the variables.
<div style="display:<?= ($offer === 1) ? 'block' : 'none'; ?>"></div>
Again, I hope I explained this good enough and any help would be greatly appreciated.

PHP MySQL submitting dynamic fields, arrays in $_REQUEST

I've got a form that has users fill out information for request. In a section of the form, I have an "Add More Phone" button that dynamically adds more text field and drowdown selections so users can add multiple types of phone numbers. Since the number of telephones that will be submitted is unknown, I am trying to figure out how to send all the data into the server so it can be inserted into the database.
Here's my html:
<div class="form-group">
<label class="col-md-4">Tel:</label>
<div class="col-md-8">
<input class="form-control required" name="phones[]" maxlength="14" type="text" placeholder="(888) 888-8888">
</div>
</div>
<div class="form-group">
<label class="col-md-4">Phone Type:</label>
<div class="col-md-8">
<select class="form-control" name="phonetypes[]">
<option value="0"><b>Choose a phone type</option>
<option value="1">Main</option>
<option value="2">Fax</option>
<option value="3">Mobile/Direct</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4"></label>
<div class="col-md-8">
<input onclick="addRow(this.form);" type="button" value="Add Another Phone" name="addphone" class="btn btn-info"></input>
</div>
</div>
The addRow(this.form) appends a new set of fields where you can enter more phone number and the phone types. This is where I'm not sure how the request is going to take the field values since we don't know how many phones will be added.
I have the request as:
if(isset($_REQUEST['action'])) {
$phones[] = $_REQUEST['phone'];
foreach($_REQUEST['phone'] as $phone) {
$phone['phone'];
}
$phonetypes[] = $_REQUEST['phonetype'];
foreach($_REQUEST['phonetype'] as $phonetype) {
$phone['phonetype'];
}
I definitely have no clue on where to go about this from here. Any help appreciated!
Every request parameter can only be sent once with a given name, if it is not an array. So you need to adapt your code to
either use a different name for each phonetype and phone
or use a name as phonetype[] to mark is as an array

Categories