Spit data to the select field from the database - php

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>

Related

How to change my <select> out with <input> to use with barcode scanner

I'm about to change a script, and right know the script are using an <select> with one options and echo to be used to show the products.
Now I'll like to change the <select> to a <input> so I can scan a barcode, and the information from the product should be added to the DB as it is right now, but I'm lost, I have used several hours to figuere out how to change it, I thought the solution was to use a hidded but then I can't add the first knowen product to my list.
Could someone please help me? :)
I've tried to use more boxes to get the products, but that was a big mess.
<div class="col-md-6">
<div class="form-group">
<label for="date">Product Name</label>
<select class="form-control select2" name="prod_name" tabindex="1" autofocus required>
<?php
$branch=$_SESSION['branch'];
$cid=$_REQUEST['cid'];
include('../dist/includes/dbcon.php');
$query2=mysqli_query($con,"select * from product where branch_id='$branch' order by prod_name")or die(mysqli_error());
while($row=mysqli_fetch_array($query2)){
?>
<option value="<?php echo $row['prod_id'];?>"><?php echo $row['prod_name']." Available(".$row['prod_qty'].")";?></option>
<?php }?>
</select>
<input type="hidden" class="form-control" name="cid" value="<?php echo $cid;?>" required>
</div><!-- /.form group -->
</div>
<div class=" col-md-2">
<div class="form-group">
<label for="date">Quantity</label>
<div class="input-group">
<input type="number" class="form-control pull-right" id="date" name="qty" placeholder="Quantity" tabindex="2" value="1" required>
</div><!-- /.input group -->
</div><!-- /.form group -->
</div>
<div class="col-md-2">
<div class="form-group">
<label for="date"></label>
<div class="input-group">
<button class="btn btn-lg btn-primary" type="submit" tabindex="3" name="addtocart">+</button>
</div>
</div>
The code should be changed to use a <input> or something simular, so I can use a barcode scanner to add my items to a list.
What I did to get my result was:
I changed the prod_id to be the barcode, and on scan the form is submitted to a temp_db, to show every items, and then added a "finish db" called inStock, and everything was as it should be! :)
/Kenneth

How to display selected drop down's ID and Name in input box using jquery?

I have one dynamic dropdown list where i get ID and Name.
And i have 2 input box above this.
If user will select any value from dropdown then its ID and Name should be display in input boxes.
How to get this in jquery ?
Below is my code for getting dropdown list from database.
<div class="col-md-12">
<?php
$m_option_selectedvalue = !empty($m_option)?$m_option:"";
$m_option_selectbox = '<select " name="id_menu" id="id_menu" class="form-control" required ><option value=""></option>';
foreach ($main_menu_list as $row):
$m_option_value = $row["id_menu"];
$m_optione_desc = $row["name_menu"];
$m_option_selected = ($m_option_selectedvalue == $m_option_value)?" selected ":"";
$m_option_selectbox = $m_option_selectbox .'<option value="'.$m_option_value.'" '.$m_option_selected.'>'.$m_optione_desc.'</option>';
endforeach;
$m_option_selectbox .= "</select>";
?>
</div>
$m_option_value = $row["id_menu"];
$m_optione_desc = $row["name_menu"];
I am getting id from $m_option_value this variable.
And name from $m_optione_desc this variable.
Below is my code where i want this 2 values:
<div class="col-md-12">
<label>Role Code </label>
<div>
<input type="text" name="role" id="role" class="form-control"/>
</div>
<label>Role Name </label>
<div class="col-md-4">
<input type="text" name="role_name" id="role_name" class="form-control"/>
</div>
</div>
try this
$('#id_menu').change(function(){
var opt = $(this).find('option:selected');
$('#role').val(opt.val());
$('#role_name').val(opt.html());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="id_menu" id="id_menu" class="form-control" required ><option value=""></option>
<option value="1"> item 1</option>
<option value="2"> item 2</option>
<option value="3"> item 3</option>
</select>
<div class="col-md-12">
<label>Role Code </label>
<div>
<input type="text" name="role" id="role" class="form-control"/>
</div>
<label>Role Name </label>
<div class="col-md-4">
<input type="text" name="role_name" id="role_name" class="form-control"/>
</div>
</div>

How to display many set of two values from database in select box in ajax?

I have developed a ayurvedic website with a lot of packages with their package type(like 7days,14 days etc) with their corresponding prices. I have stored the packages in one table in the database and stored their corresponding package types and prices in another table. For each package there will be a number of package types and their prices.
I have a select box as shown below to store the package type of the particluar package and their prices.
<select>
<option value="" disabled="disabled" selected="selected">Select Your Package Type</option>
<option value="1"> 14 Days - Rs.20000 </option>
<option value="2">21 Days - Rs.25000</option>
</select>
This select is in a modal window as shown below :
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Enquiry / Booking / Payment</h4>
</div>
<div id="contact_form_bg">
<div class="form_header">
</div>
<div class="contact_form body">
<form action="#contactForm" id="contactForm" method="post" enctype="multipart/form-data">
<ul>
<li>
<p class="left">
<input type="date" name="date" placeholder="Date" required>
</p>
<p class="left">
<input type="time" name="time" placeholder="Time" required>
</p>
<p class="left">
<input type="text" name="name" placeholder="Full Name" required>
</p>
<p class="left">
<input type="text" name="name" placeholder="Contact Number" required>
</p>
</li>
<li>
<p class="left">
<input type="email" name="mail" placeholder="Your Email" required>
</p>
<p class="left">
<input type="text" name="no-of-people" placeholder="Selected Package">
</p>
<p class="left">
<select>
<option value="" disabled="disabled" selected="selected">Select Your Package Type</option>
<option value="1"> 14 Days - Rs.20000 </option>
<option value="2">21 Days - Rs.25000</option>
</select>
</p>
<p class="left">
<select>
<option value="" disabled="disabled" selected="selected">Gender</option>
<option value="1">Female</option>
<option value="2">Male</option>
</select>
</p>
</li>
<li>
<p class="txtarea-style">
<textarea cols="46" rows="3" name="comments" placeholder="Message With Us ...."></textarea>
</p>
<p>
<input class="btn btn-submit" type="submit" value="Enquiry">
</p>
<p>
<input class="btn btn-submit" type="submit" value="Booking">
</p>
<!-- <p>
<a data-toggle="modal" href="#myModal2" class="btn btn-submit" >Booking with Payment </a>
</p>-->
</li>
</ul>
</form>
</div>
</div>
</div>
</div>
</div>
The select box is also shown in the above code.
I want to display all the package types and their prices in the format shown in the select box. I have the id of the particular package and using that id , i want to retrieve all the package types and prices in that select box.
Can anyone suggest how to do this ?
You need to make an array of package id and combination of package type and its price.
Make an array such as key will be your package id and value would combination of package type and its price.
Example : $selectArray = array('1'=>'14 Days - Rs.20000','2'=>'15 Days - Rs.30000'). like wise.
You can concat days and price using php in array.
After that create loop in view file for select options.
So you can do this like this and provide all info at infinite level,
<option value="14:20000"> 14 Days - Rs.20000 </option>
and when it will be get subbmitted you just need to export it like :
$selectedPackageArray = export(':', $_POST['select']);
if( isset($con) && !empty($con) && $con!="" ){ // checking the database connection is established or not, here im selecting three fields fro database. the primary key ie the serial no, price and the package name . here im using the id as the value of an option in the to save the database.
$qryPackage="SELECT id,price,name FROM tablename "; // mysql query to fetch the data.
if( $strPackageStmt=mysqli_query($con,$qryPackage) )
{
while( $strPackageResult=mysqli_fetch_array($strPackageStmt,MYSQLI_ASSOC) )
{ $ArrPackage[]=$strPackageResult; }
if( isset($strPackageResult) ){ unset($strPackageResult); }
mysqli_free_result($strPackageStmt);
if( isset($strPackageStmt) ){ unset($strPackageStmt); }
}
}
// drawing the select or dropdown using php.
echo "<select name='package' id='package'>";
echo "<option value='-1'>Select one package</option>";
if( isset($ArrPackage) && is_array($ArrPackage) && count($ArrPackage)>0 ) {
foreach( $ArrPackage as $tempPackage )
{
echo "<option value='". $tempPackage['id'] ."'>". $tempPackage['name'] ."--".$tempPackage['price'] ."</option>";
}
}
echo "</select>";
this is one of the way to populate the select box or drop down using php. please have try on this.This is working on my case.

How to let a button automatically enter values from the database into a form?

This is an image of how the page looks like,
The 2 big blue buttons at the top are buttons that represent a value from the database. The main function of this button is to help the user to fill in the form in a convenient way leaving only the "Description" to be filled in.
This is my code for the page,
<div class="bodycontainer">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">My Records</h3>
</div>
<div class="panel-body">
<?php
require 'dbfunction.php';
$con = getDbConnect();
$day = date("l");
if (mysqli_connect_errno($con)) {
"Failed to connect to MySQL: " . mysqli_connect_error();
} else {
$result = mysqli_query($con, "SELECT * FROM timetableschedule WHERE day='" . $day . "'");
while ($schedule = mysqli_fetch_array($result)) {
?>
<div class="col-md-4">
<div class="admininfobox">
<a class="btn btn-primary">
<?php
echo "<br/>";
echo $schedule['academicInstitution'] . "<br />";
echo $schedule['startTime'] . "-" . $schedule['endTime'] . "hrs<br />";
echo "<br/>";
?>
</a>
</div>
</div>
<?php
}
mysqli_close($con);
}
?>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Record Activity</div>
<div class="panel-body">
<form name="Create New Admin" class="form-horizontal" method="post" action="handlerecord.php">
<div class="form-group">
<div class="col-sm-4">
<label>Academic Institution</label>
<input list="AcadInst" type="text" class="form-control" placeholder="Institution Name" name="academicInstitution">
<datalist id="AcadInst">
<option value="Singapore Polytechnic (SP)">
<option value="Ngee Ann Polytechnic (NP)">
<option value="Temasek Polytechnic (TP)">
<option value="Republic Polytechnic (RP)">
<option value="Nanyang Polytechnic (NYP)">
<option value="Others (Please specify)">
</datalist>
</div>
<div class="col-sm-4">
<label>Level of Teaching</label>
<input list="LvTeaching" type="text" class="form-control" placeholder="Teaching Stage" name="levelofteaching">
<datalist id="LvTeaching">
<option value="Undergraduate Teaching">
<option value="Postgraduate Teaching">
<option value="Continuing Education">
<option value="Others (Please specify)">
</datalist>
</div>
<div class="col-sm-4">
<label>Type of Teaching</label>
<input list="TyTeaching" type="text" class="form-control" placeholder="Teaching Type" name="typeofteaching">
<datalist id="TyTeaching">
<option value="Clinical Teaching">
<option value="Academic Teaching">
<option value="Talk">
<option value="Others (Please specify)">
</datalist>
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<label for="startdate">Start Time</label>
<input type="text" class="form-control" placeholder="Select Time" name="starttime">
</div>
<div class="col-sm-4">
<label for="enddate">End Time</label>
<input type="text" class="form-control" placeholder="Select Time" name="endtime">
</div>
<div class="col-sm-4">
<label for="enddate">Description</label>
<input type="text" class="form-control" placeholder="Optional" name="Description">
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<input type="submit" value="Add" class="btn btn-primary">
</input>
</div>
</div>
</form>
</div>
</div>
An overview of the database,
I am not sure on how to code it. Like Once the user click onto the button, the button goes through the database and input the data accordingly into the form ready for the user to submit.
My suggestion is to create an AJAX request on click.
Create a page loadFormData.php
Accept key data to search in database. For eg: id. Then search in database with this key and get the results. Finally encode this to json (json_encode()) and echo the result.
When the user clicks on the button, trigger an ajax (Javascript) function and send the key values to the loadFormData.php
From the returned json result, extract institution name, start time, end time etc and fill this to form fields using JavaScript
$("#blueButton").click(function() {
$.ajax({
type: "GET",
url: "loadFormData.php",
dataType: "json",
success : function(data) {
// extract each item from the variable 'data'
}
});
});
You should create a form and use action to state the method of entering the values that you want.
For example
http://www.w3schools.com/php/php_forms.asp
You can use this to enter the data easily.

Retain Select Box selection even after the form is submitted in php

I have a page which contains a select box which is used to select a particular user. I also have two radiobuttons for selecting date range. This page is for generating user reports based on the selections.
At present when I select the user and preferred date ranges and clicks on submit button reports are generated properly but my selection in select box will be reset to default after submitting.Here My select box is populated from mysql table.
My task is to retain the select box value even after form submit. I have tired so many ways but nothing worked. Can anyone please help me out with this..
Here is my code:
HTML
<div class="box-container-toggle">
<form class="form-horizontal" name="reportform" id="reportform" action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" >
<div class="box-content">
<fieldset>
<legend>Developer Reports</legend>
<div class="control-group">
<label class="control-label" for="select01">Select Developer</label>
<div class="controls">
<select id="select01" class="chzn-select" name="selectdev" onChange="getapp(this.value)">
<option value="">Select developer</option>
<?php
$qry="select user_id from tab_user_login where type='Developer' and status='approved'";
$res=mysql_query($qry);
while($row=mysql_fetch_array($res))
{
$devid=$row[0];
$qry1="select name from tab_user where user_id='$devid'";
$res1=mysql_query($qry1);
while($row1=mysql_fetch_array($res1))
{
?>
<option value="<?php echo $devid;?>" <?php if($dev!="") { if($dev==$devid) {echo 'selected'; } } ?> ><?php echo $row1[0];?></option>
<?php
}
}
?>
</select>
</div>
</div>
<h3> Select Date Range </h3>
<div class="control-group">
<div class="controls">
<label>
<input type="radio" name="selectdateradio" value="selectdaterange" onChange="enableblock()"> Select a Date Range
<div class="control-group" id="daterange" style="display:none">
<label class="control-label" for="select01">Select Date</label>
<div class="controls">
<select id="select01" name="selectdate" id="selectdate" onChange="getYesterdaysDate(this.value)">
<option value="Today" >Today</option>
<option value="Yesterday" >Yesterday</option>
<option value="Last 7 Days" >Last 7 Days</option>
<option value="Last 30 Days" >Last 30 Days</option>
<option value="This Week" >This Week</option>
<option value="This Month" >This Month</option>
<!--<option value="Last 6 Months" >Last 6 Months</option>-->
</select>
<input type="hidden" name="hiddendate" value="" >
<input type="hidden" name="hiddennext" value="" >
</div>
</div>
</label>
<label>
<input type="radio" name="selectdateradio" value="selectcustomdate" onChange="enablecustomblock()"> Select Custom Date
<div class="control-group" id="customdaterange" style="display:none">
<div class="control-group">
<label class="control-label" for="date01">Start input</label>
<div class="input-append date datepicker" data-date="2012-02-12" data-date-format="yyyy-mm-dd">
<input name="start_date" id="start_date" type="text"/><span class="add-on "><i class="icon-calendar"></i></span>
</div>
<!--<div class="controls">
<input type="text" class="input-xlarge datepicker" name="start_date" id="start_date" onChange="show()"/>
</div>-->
</div>
<div class="control-group">
<label class="control-label" for="date01">End Date</label>
<div class="input-append date datepicker" data-date="2012-02-12" data-date-format="yyyy-mm-dd">
<input name="end_date" id="start_date" type="text"/><span class="add-on "><i class="icon-calendar"></i></span>
</div>
<!-- <div class="controls">
<input type="text" class="input-xlarge datepicker" name="end_date" id="end_date" onChange="show()"/>
</div> -->
</div>
</div>
</label>
</div>
</div>
<div class="form-actions">
<input type="submit" class="btn btn-primary" name="submit" value="Run Reports" >
<button type="reset" class="btn">Reset</button>
</div>
</fieldset>
</div>
</form>
<?php
if(isset($_POST['submit']))
{
$dev=$_POST['selectdev'];
$qryy1="select name from tab_user where user_id='$dev'";
$ress1=mysql_query($qryy1);
$roww1=mysql_fetch_array($ress1);
$devname=$roww1[0];
$selected=$_POST['selectdateradio'];
$customstart=$_POST['start_date'];
$customend=$_POST['end_date'];
$postdate=$_POST['selectdate'];
$hide=$_POST['hiddendate'];
$hidden=date("Y-m-d", strtotime($hide));
$hide1=$_POST['hiddennext'];
$hidden1=date("Y-m-d", strtotime($hide1));
date_default_timezone_set("Asia/Kolkata");
$today=date('Y-m-d');
/*$click=0;
$imp=0;
$install=0;
$rev=00.00;
$active=0;*/
}
else
{
}
if($_POST['selectdev']=='')
$devname='All Developer';
if($_POST['selectdate']=='')
$postdate="";
?>
</div>
You must set posted variable before generate the select elements. then you can check the current developer
please try this code before generate select element (for example above the <div class="box-container-toggle"> ) :
<?php
if(isset($_POST['selectdev']) && $_POST['selectdev']!='') {
$selectdev = $_POST['selectdev'];
}
?>
Then Use your checking condition as below
<select id="select01" class="chzn-select" name="selectdev" onChange="getapp(this.value)">
<option value="">Select developer</option>
<?php
$qry="select user_id from tab_user_login where type='Developer' and status='approved'";
$res=mysql_query($qry);
while($row=mysql_fetch_array($res))
{
$devid=$row[0];
$qry1="select name from tab_user where user_id='$devid'";
$res1=mysql_query($qry1);
while($row1=mysql_fetch_array($res1))
{
?>
<option value="<?php echo $devid;?>" <?php if(isset($selectdev) && $selectdev==$devid) echo 'selected="selected"'; ?> ><?php echo $row1[0];?></option>
<?php
}
}
?>
</select>

Categories