I am using below admin pannel:
https://github.com/harsh4870/phpcoreadmin
I have below code in the form (\forms\customer_form.php) where I can see static array ( <?php $opt_arr = array("India", "Middle East", "Nepal", "South Indian"); ?>), I would like to replace this static array to be picked from the MySql database (Table name: CUSTOMER| Column_name: STATE).
Thanks in advance
<div class="form-group">
<label>State</label>
<?php $opt_arr = array("Maharashtra", "Kerala", "Madhya pradesh"); ?>
<select name="state" class="form-control selectpicker" required>
<option value=" ">Please select your state</option>
<?php
foreach ($opt_arr as $opt) {
if ($edit && $opt == $customer['state']) {
$sel = 'selected';
} else {
$sel = '';
}
echo '<option value="'.$opt.'"' . $sel . '>' . $opt . '</option>';
}
?>
</select>
</div>
You'll need to start a MySQL connection and then submit a query.
Here's a quote from W3Schools.
The SELECT statement is used to select data from one or more tables:
SELECT column_name(s) FROM table_name
or we can use the * character to select ALL columns from a table:
SELECT * FROM table_name
Below is an example.
<?php
// Connection Info
$host = "localhost";
$user = "myuser";
$pass = "mypass";
$db = "mydb";
// Start Connection
$link = new mysqli($host, $user, $pass, $db);
// Check Connection
if ($link->connect_error) {
// Error Handler
die("MySQL Connection Error: ".$link->connect_error);
}
// Prepare Query
$query = "SELECT 'AREA_NAME' FROM 'REIGONAL_AREA'";
// Submit Query
$result = $link->query($query);
// Check for Errors
if ($result) {
// Set counter
$i = 0;
// Set data variable
$opt_arr = [];
// Loop through data
while($current = $result->fetch_assoc()) {
$output[$i] = $current[0];
$i++;
}
?>
<!DOCUMENT html>
<html>
<head>
<!-- Header Elements Go Here -->
</head>
<body>
<div class="form-group">
<label>State</label>
<?php $opt_arr = array("Maharashtra", "Kerala", "Madhya pradesh"); ?>
<select name="state" class="form-control selectpicker" required>
<option value=" ">Please select your state</option>
<?php
foreach ($opt_arr as $opt) {
if ($edit && $opt == $customer['state']) {
$sel = 'selected';
} else {
$sel = '';
}
echo '<option value="'.$opt.'"' . $sel . '>' . $opt . '</option>';
}
?>
</select>
</div>
</body>
</html>
<?php
} else {
// Error Handler
die("MySQL Error: ".$link->error);
}
?>
Related
I have a drop down that is enabled by default and shows options populated from the database. When an option is selected that is now blank it enables the drop down to the side if it.
echo '
<script>
function check(){
if(document.getElementById("company").value!="")
document.getElementById("stores").disabled=false;
else
document.getElementById("stores").disabled=true;
}
</script>
<label class="form-control-label" for="input-last-name">Company</label>
<select type="text" id="company" name="company" class="form-control form-control-alternative" onchange="check()">
<option></option>';
$sql = "SELECT * FROM companies WHERE CompanyID != '4'";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<option value='.$row['CompanyID'].'>'.$row['CompanyName'].'</option>';
}
}
echo'
</select>
<label class="form-control-label" for="input-last-name">Store </label>
<select id="stores" name="stores" class="form-control form-control-alternative" disabled>
<option></option>';
$sql = "SELECT * FROM stores";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<option value='.$row['storeid'].'>'.$row['storename'].'</option>';
}
}
echo '
</select>';
As you can see selecting an item from the companies dropdown enables the stores dropdown to be enabled. However at the moment it shows all stores - not stores assigned to that company the SQL needs to be
SELECT * FROM store WHERE StoreID = $SelectedCompanyID
and not
SELECT * FROM store
I cannot work out a way to populate a variable to complete the query and reload the drop down correctly with correct stores without reloading the page and loosing the rest of the inputs already completed in the form.
Any help would be appreciated.
Created a new page called fetch_data.php with the following code below
<?php
if(isset($_POST['get_option']))
{
include('includes/config.php');
$companies = $_POST['get_option'];
$sql = "SELECT * FROM stores WHERE companyid = '$companies'";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<option value='.$row['storeid'].'>'.$row['storename'].'</option>';
}
}
exit;
}
?>
Changed my HTML / PHP to look like this
echo '
<script>
function check(){
if(document.getElementById("company").value!="")
document.getElementById("stores").disabled=false;
else
document.getElementById("stores").disabled=true;
}
</script>
<label class="form-control-label" for="input-last-name">Company</label>
<select type="text" id="company" name="company" class="form-control form-control-alternative" onchange="check()">
<option></option>';
$sql = "SELECT * FROM companies WHERE CompanyID != '4'";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<option value='.$row['CompanyID'].'>'.$row['CompanyName'].'</option>';
}
}
echo'
</select>
<label class="form-control-label" for="input-last-name">Store </label>
<select id="stores" name="stores" class="form-control form-control-alternative">
</select>';
Then finally implemented in the JavaScript to do it.
<script type="text/javascript">
function fetch_select(val)
{
$.ajax({
type: "post",
url: "fetch_data.php",
data: {
get_option:val
},
success: function (response) {
document.getElementById("stores").innerHTML=response;
}
});
}
</script>
I have a form with one Select option and 3 input text box. I fetch the value of select option from database. I want to change data of the 3 input field based on what i select. I try many ways but faild.
<Select onchange="update(this)">
<?php
$query = "SELECT * FROM wheel1 ORDER BY chair_no ASC";
$run = mysqli_query($connect, $query);
$i = 0;
while ($chair = mysqli_fetch_assoc($start)) {
$i++;
?>
<option value="<?php echo $chair['chair_no'];?>"><?php echo $i ?></option>
<?php
$name = $chair['name'];
$phone = $chair['phone'];
$detail = $chair['detail'];
}
?>
It fetch data perfectly for Select menu, But only store the last value for name phone and detail variable. Here is javascript code.
function update( elem ) {
var name = "<?php echo $name; ?>";
var phone= "<?php echo $phone; ?>";
var detail = "<?php echo $detail; ?>";
document.getElementById("name").innerHTML = name;
document.getElementById("phone").innerHTML = phone;
document.getElementById("detail").innerHTML = detail;
}
Any help is appricated. Thanks in advance.
Set diffirent attribute in options for name, phone and detail
Demo: https://jsfiddle.net/sjhhv4pw/
<select onchange="update(this)">
<?php
$query = "SELECT * FROM wheel1 ORDER BY chair_no ASC";
$run = mysqli_query($connect, $query);
$i = 0;
while ($chair = mysqli_fetch_assoc($start)) {
$i++;
?>
<option data-name="<?php echo $chair['name']; ?>" data-phone="<?php echo $chair['phone']; ?>" data-detail="<?php echo $chair['detail']; ?>" value="<?php echo $chair['chair_no'];?>"><?php echo $i ?></option>
<?php
}
?>
</select>
jQuery:
function update( elem ) {
var name = $(elem).find("option:selected").attr("data-name");
var phone= $(elem).find("option:selected").attr("data-phone");
var detail = $(elem).find("option:selected").attr("data-detail");
}
Try below code
<Select onchange="update(this)">
" data-phone="" data-chair-detail="" value="">
javascript code, considering you are using jQuery
function update( elem ) {
var name = ele.data('chair-name');
var phone= ele.data('phone');
var detail = ele.data('chair-detail');
}
I have a search box where search is done through database. In the code I have, the search is done in one input box and the dynamic search output is shown in a text area below it.
What I want is a search like Google, where when the user stars typing, it should show similar items from the db table.
For example, if I have two organizations named "Dummy 1" and "Dummy 2" and the user types in "du", the search bar should show the 2 results and user should be able to select one.
The code I have is:
<form action="newbrand.php" method="post">
<br>
Brand Name: <input type="text" name="bname" /><br><br>
Search for an Organization: <input type="text" name="search" onkeyup="searchq()" id="output"><
Selected Organization:<textarea id="output"></textarea>
</form>
The js is like this:
<script type="text/javascript">
function searchq(){
var searchTxt = $("input[name='search']").val();
$.post("search.php", {searchVal: searchTxt},function(output){
$("#output").html(output);
}
}
</script>
This is the search.php file:
<?php
include 'db_connect.php';
$link = mysqli_connect($host, $username, $password, $db);
if(!link){
echo "DB Connection error";
}
$output = '' ;
$output2 = '' ;
if (isset($_POST['searchVal'])){
$searchq = $_POST['searchVal'];
//$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysqli_query($link, "SELECT * FROM `organisations_info` WHERE `Organisation_Name` LIKE '%$searchq%'")or die("Could not search!");
$count = mysqli_num_rows($query);
if($count == 0){
$output = '<div>No results!</div>';
}else{
while($row = mysqli_fetch_array($query)){
$orgname = $row['Organisation_Name'];
$orgid = $row['Organisation_Id'];
$subs = $row['Subscription_Type'];
//$output = echo "<option value='".$orgname."'>" . $orgname . "</option>";
$output = $orgname;
$output2 = $orgid;
$output3 = $subs;
//$output = '<div>'.$orgname.'</div>';
}
}
}
echo ($output);
?>
How can I achieve that?
In the JS code...
<script type="text/javascript">
function searchq(){
var searchTxt = $("input[name='search']").val();
$.post("search.php", {searchVal: searchTxt},function(output){
$("#output").html(output);
}
}
</script>
you have given the id(#output) of a input type element to display(or to return) the HTML statements and the js script also not closed properly (syntax error).So the valid statement will be...
<form action="newbrand.php" method="post">
<br>
Brand Name: <input type="text" name="bname" /><br><br>
Search for an Organization: <input type="text" name="search" onkeyup="searchq()" id="output"><
Selected Organization:<textarea id="output"></textarea>
</form>
<br>
<div id="mydiv"></div>
<script type="text/javascript">
function searchq(){
var searchTxt = $("input[name='search']").val();
$.post("search.php", {searchVal: searchTxt},function(output){
$("#mydiv").html(output);
});
}
</script>
Just change your query :
$query = mysqli_query($link, "SELECT * FROM `organisations_info` WHERE `Organisation_Name` LIKE '%".$searchq."%' ")or die("Could not search!");
And the query will work fine :)
Then output the response in HTML in your search.php (manage the css accordingly) :
<?php
include 'db_connect.php';
$link = mysqli_connect($host, $username, $password, $db);
if(!link){
echo "DB Connection error";
}
$output = '' ;
$output2 = '' ;
if (isset($_POST['searchVal'])){
$searchq = $_POST['searchVal'];
//$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysqli_query($link, "SELECT * FROM `organisations_info` WHERE `Organisation_Name` LIKE '%".$searchq."%' ")or die("Could not search!");
$count = mysqli_num_rows($query);
if($count == 0){
$output = '<div>No results!</div>';
}else{
while($row = mysqli_fetch_array($query)){
$orgname = $row['Organisation_Name'];
$orgid = $row['Organisation_Id'];
$subs = $row['Subscription_Type'];
?>
<div><?php echo $orgname; ?></div>';
<div><?php echo $orgid ; ?></div>';
<div><?php echo $subs ; ?></div>';
<?php
} // while
} // else
} // main if
?>
I hope this is what you required !!
I'm trying to use AJAX and PHP to update the options of a drop down menu based on the previous selection from another drop down menu.
I have this script in the head of my document:
<script>
function show_districts(str)
{
if (str.length==0)
{
document.getElementById("districts_dropdown").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("districts_dropdown").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","get_districts.php?q="+str,true);
xmlhttp.send();
}
</script>
... which, when onchange() is triggered, passes the value of this drop down:
<label for="banner" class="medium">Banner</label>
<select name="banner" id="banner" class="textbox short_field" onchange="show_districts(this.value)">
<option value=""></option>
<?php
$result = mysql_query("SELECT name FROM banners", $connection);
if (!result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row["name"] . '"';
if($row["name"] == $banner) { echo 'selected';} ;
echo '>' . $row["name"] . '</option>';
}
?>
</select>
... to this php file:
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
$q = $_REQUEST['q'];
if($q = 'TBOOTH') {$id = 2;}
$result = mysql_query("SELECT dist.name FROM districts AS dist JOIN int_bd AS ibd ON dist.id = ibd.districts_id WHERE banners_id = '".$id."' GROUP BY dist.id ORDER BY dist.id ASC", $connection);
if (!result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row["name"] . '"';
if($row["name"] == $district) { echo ' selected';} ;
echo '>' . $row["name"] . '</option>';
}
mysqli_close($connection);
?>
... which should return the values from the php file to this drop down menu with <span id="districts_dropdown">:
<label for="district" class="medium">District</label>
<select name="district" class="textbox short_field">
<option value=""></option>
<span id="districts_dropdown"></span>
</select>
This works nicely if <span id="districts_dropdown"> is outside of the <select> tags, but not within. Any insight as to why would be greatly appreciated.
Thanks for you help Popnoodles! I fixed part of the problem by having the php code generate the entire <select> tag like this:
<?php
$q = $_REQUEST['q'];
$id = "";
if ($q = 'TBOOTH') {
$id = "2";
} else {
if ($q = 'WIRELESS ETC') {
$id = "3";
}
}
echo '<select name="district" class="textbox short_field">
<option value=""></option>';
$query = "SELECT dist.name FROM districts AS dist JOIN int_bd AS ibd ON dist.id = ibd.districts_id WHERE banners_id = '".$id."' GROUP BY dist.id ORDER BY dist.id ASC";
$result = mysql_query($query, $connection);
if (!result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row["name"] . '"';
if($row["name"] == $district) { echo ' selected';} ;
echo '>' . $row["name"] . '</option>';
}
mysql_close($connection);
?>
But now the $id variable I have at the top of that code isn't updating when the onchange() is triggered.
It looks like you want to repopulate the options in a select tag. First of all you cannot put a span in there. Please adhere to the specs - <select> tags can only contain <option> and <optgroup> tags.
This is what you're changing
document.getElementById("districts_dropdown").innerHTML=xmlhttp.responseText;
So what will happen if you target this instead
<select name="banner" id="banner" class="textbox short_field" onchange="show_districts(this.value)">
with
document.getElementById("banner").innerHTML=xmlhttp.responseText;
You get options. http://jsfiddle.net/Da26e/1/
Why my script doesn't work? I need to hide the sem dropdown when branch dropdown is select ALL. I try script below but it doesn't work.
And this is my script
<script>
$(document).ready(function () {
$('#branch').change(function () {
if (this.value != "ALL") {
$('#sem').show();
} else {
$('#sem').hide();
}
});
});
</script>
</head>
<body onload=showCourses(str="ALL")>
<select name="branch" id="branch" onchange="showCourses()">
<option value="ALL" selected='ALL'>ALL</option>
<?php
$mysqli = new mysqli("localhost", "root", "", "app");
$result = $mysqli->query("SELECT * FROM app GROUP BY app_cn ORDER BY app_cn");
while($row = $result->fetch_assoc())
{
echo '<option value="'.$row['app_cn'].'">'.$row['app_cn'].'</option>';
}?>
</select>
<select name="sem" id="sem" onchange="showCourses()">
<?php
$mysqli = new mysqli("localhost", "root", "", "app");
$result = $mysqli->query("SELECT * FROM app GROUP BY app_plan_no ORDER BY app_plan_no");
while($row = $result->fetch_assoc())
{
echo '<option value="'.$row['app_plan_no'].'">'.$row['app_plan_no'].'</option>';
}?>
</select>
check for this in if statement:
$('option:selected', this).val()