Dynamic Dropdowns POST - javascript

I'm trying to post the values that I get from each dropdown menu, this is an n-level, however I'm going to use them maximum of 3. How can I post every value i get from each selection?
<?php
include('dbcon.php');
if($_REQUEST)
{
$id = $_REQUEST['parent_id'];
$query = "select * from ajax_categories where pid = ".$id;
$results = #mysql_query( $query);
$num_rows = #mysql_num_rows($results);
if($num_rows > 0)
{?>
<select name="sub_category" class="parent">
<option value="" selected="selected">-- Sub Category --</option>
<?php
while ($rows = mysql_fetch_assoc(#$results))
{?>
<option value="<?php echo $rows['id'];?>"><?php echo $rows['category'];?></option>
<?php
}?>
</select>
<?php
}
else{echo '<label style="padding:7px;float:left; font-size:12px;">No Record Found !</label>';}
}
?>
http://www.99points.info/2010/12/n-level-dynamic-loading-of-dropdowns-using-ajax-and-php/

change the select option of your like this
<select name="sub_category" class="parent">
<option value="" selected="selected">-- Sub Category --</option>
while ($rows = mysql_fetch_assoc(#$results))
{?>
echo '<option value="'.$row['id'].'">'.$rows['category'].'</option>';
<?php
}?>
</select>
<?php
}
else{echo '<label style="padding:7px;float:left; font-size:12px;">No Record Found !</label>';}
}
</select>

Related

Set Button id from javascript

I have a dropdown list which filled with the data fetched from db. I need to assign the selected index value of this dropdown list as the ID of a button.
Is it really possible?
my code
<select name="customer" onchange="getCustomer()" id="customer" class="form-control" style="width: 180px !IMPORTANT;">
<option value="">--Select Customer--</option>
<?php
$sql = mysqli_query($db,"SELECT * FROM customer ");
while($row= mysqli_fetch_array($sql))
{
?>
<option value="<?php echo $row['custid'];?>"><?php echo $row['customername'];?></option>
<?php
}
?>
</select>
</div>
<button name="custbt" id="1" class="label-text col-form-label userinfo">Show customer details</button>
<script>
function getCustomer()
{
var e = document.getElementById("customer");
var cust = e.options[e.selectedIndex].value;
// alert(cust);custbt
//document.getElementByName("custbt").id=cust;
document.getElementByName(custbt).id=cust;
}
</script>
Might be this will solve your problem. I have added a class on btn call CustomerModalBttn .
Try this
<select name="customer" id="customer" class="form-control" style="width: 180px !IMPORTANT;">
<option value="">--Select Customer--</option>
<?php
$sql = mysqli_query($db,"SELECT * FROM customer ");
while($row= mysqli_fetch_array($sql)){
?>
<option value="<?php echo $row['custid'];?>"><?php echo $row['customername'];?></option>
<?php
}
?>
</select>
<button name="custbt" id="1" class="label-text col-form-label userinfo CustomerModalBttn">
Show customer details
</button>
<script>
$(document).ready(function(){
$('#customer').on('change',function(){
$('.CustomerModalBttn').attr('id',$(this).val());
});
});
</script>
IDs must begin with a letter.
<button name="custbt" id="a1" class="label-text col-form-label userinfo">Show customer details</button>
var customer = document.getElementById("customer");
var a1 = document.getElementById("a1");
customer.addEventListener("change",function(){
a1.removeAttribute("id");
a1.setAttribute("id",customer.value);
});
Just add some condition if ($row['custid'] == $_GET['custid']) echo 'selected', if you pass the customer id from url, and then get it with $_GET param.
...
while($row= mysqli_fetch_array($sql))
{ ?>
<option <?php if ($row['custid'] == $_GET['custid']) echo 'selected'; ?> value="<?php echo $row['custid'];?>">
<?php echo $row['customername'];?>
</option>
<?php } ?>
...

How to check ajax overlapping and through them out?

I had created a simple on change event in Ajax and trying to get data on the change of select tag [HTML] in another select tag.
All's work fine I got the result on test.php
test.php
<div class="row">
<div class="input-field col s12">
<select name="category" id="category">
<option value="" disabled selected>Choose Category</option>
<?php
include "db-connect/db-connect.php";
$qry = mysqli_query($conn, "SELECT * FROM `category`");
while ($row=mysqli_fetch_assoc($qry)) {
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['category']; ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<select name="subcategory" id="subcategory">
<option value="">Choose Sub Category</option>
</select>
</div>
</div>
and the ajax code for this html :-
<script type="text/javascript">
$(document).ready(function(){
$('#category').on('change',function(){
var categoryID = $(this).val();
if(categoryID){
$.ajax({
type:'POST',
url:'ajax/category.php',
data:'category_id='+categoryID,
success:function(html){
$('#subcategory').html(html);
//$('#city').html('<option value="">Select state first</option>');
}
});
}else{
$('#subcategory').html('<option value="">Choose category first</option>');
//$('#city').html('<option value="">Select state first</option>');
}
});
});
</script>
When I run it I got the result but after that, I want to implement it into my project template I got nothing.
But, I get data in the inspect element [F12] -> Network
Ajax data file ajax/category.php:-
<?php
//Include the database configuration file xD
include "../db-connect/db-connect.php";
if(!empty($_POST["category_id"])){
//Fetch all data
$query = mysqli_query($conn,"SELECT * FROM `sub_category` WHERE `category_id` = ".$_POST['category_id']."");
//option list
if(mysqli_num_rows($query) > 0){
echo '<option value="">Select state</option>';
while($row = mysqli_fetch_assoc($query)){
echo '<option value="'.$row['sub_category'].'">'.$row['sub_category'].'</option>';
}
}else{
echo '<option value="">State not available</option>';
}
}
?>

PHP query of columns with value from <select> MySQL

I am trying to create a list populated with the columns from a table previously selected from another list of tables. But, I can't get the query to work.
I've appended a sample of my code that is not working. If i replace ".$search_query." with a table name it works. But, I want it to use the table selected by the user.
<!-- Table Select -->
<form name="myform" action="" method="post">
<select id="parentTable_{{$index}}" name="parentTable_{{$index}}" ng-model="parentTable_$index" type="selectable" onchange="submitform();">
<option style="display:none" value="users">select a table</option>
<?php
$count=1;
$sel_query="SHOW TABLES";
$result = mysqli_query($con,$sel_query);
while($row = mysqli_fetch_assoc($result)) { ?>
<option value="<?php echo $row["Tables_in_yamanagolddb"]; ?>"><?php echo $row["Tables_in_yamanagolddb"]; ?></option>
<?php $count++; } ?>
</select>
</form>
<!-- Index reference -->
<?php $search_query = mysqli_real_escape_string($con, $_POST['parentTable_{{$index}}']); ?>
<!-- Field Select -->
<select ng-model="parentField_$index" type="selectable">
<option style="display:none" value="">select a field</option>
<?php
$count=1;
$sel_query="select * from information_schema.columns where table_name = '".$search_query."' and table_schema = 'yamanagolddb'";
$result = mysqli_query($con,$sel_query);
while($row = mysqli_fetch_assoc($result)) { ?>
<option value="<?php echo $row["COLUMN_NAME"]; ?>"><?php echo $row["COLUMN_NAME"]; ?></option>
<?php $count++; } ?>
</select>
<script>
function submitform()
{
document.myform.submit();
}
</script>

Populate a text box using a selected value in a dependent dropdown

I have this two tables in my database
1 - tbl_category
2 - tbl_shelf_place
(I want to achieve this kind of output where the selected category_name from the drop-down (book_category) with a value of category_id will show the shelf_code in a textbox instead of a DROPDOWN, based on the equivalent shelf_id).
-------------------------- -------------------------
tlb_category tbl_shelf_place
--------------------------- -------------------------
category_id | category_name shelf_id| shelf_code
--------------------------- -------------------------
1 | General Works 1 | General Works Shelf
2 | History 2 | History Shelf
Below is my current code for the dependent dropdown.
My dropdown code in HTML
<select id="book_category" name="book_category" class="selectpicker" data-style="btn btn-danger" onChange="getState(this.value);" required>
<option disabled selected value=""> Choose Category
</option>
<?php
$query = "SELECT category_id , category_name FROM tbl_category";
$stmt = $mysqlconnection->prepare($query);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($category_id , $category_name);
while($stmt->fetch())
{
$getcategoryID = $category_id;
$getcategoryname = $category_name;
echo "<option value='{$getcategoryID}'>{$getcategoryname}</option>";
}
?>
</select>
<select id="book_shelf" name="book_shelf" class="selectpicker" data-style="btn btn-danger" required>
<option disabled selected value="">
</option>
</select>
My script code
<script>
function getState(val) {
$.ajax({
type: "POST",
url: "dropdownrequest.php",
data:'category_id='+val,
success: function(data){
$("#book_shelf").html(data);
}
});
}
</script>
dropdownrequest.php
<?php
include_once ("../database/dbconnect.php");
if(!empty($_POST["category_id"])) {
$query ="SELECT * FROM tbl_shelf_place WHERE shelf_id = '" . $_POST["category_id"] . "'";
$results = $mysqlconnection->query($query);
?>
<?php
foreach($results as $shelf) {
?>
<option value="<?php echo $shelf["shelf_id"]; ?>"><?php echo $shelf["shelf_code"]; ?></option>
<?php
}
}
?>
You can use this code.
<select id="book_category" name="book_category" class="selectpicker" data-style="btn btn-danger" onChange="getState(this.value);" required>
<option disabled selected value=""> Choose Category
</option>
<?php
$query = "SELECT category_id , category_name FROM tbl_category";
$stmt = $mysqlconnection->prepare($query);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($category_id , $category_name);
while($stmt->fetch())
{
$getcategoryID = $category_id;
$getcategoryname = $category_name;
echo "<option value='{$getcategoryID}'>{$getcategoryname}</option>";
}
?>
</select>
<textarea id="book_shelf" name="book_shelf"></textarea>
In your JS.
<script>
function getState(val) {
$.ajax({
type: "POST",
url: "dropdownrequest.php",
data:'category_id='+val,
success: function(data){
$("#book_shelf").val(data);
}
});
}
</script>
In you PHP code.
<?php
include_once ("../database/dbconnect.php");
if(!empty($_POST["category_id"])) {
$query ="SELECT * FROM tbl_shelf_place WHERE shelf_id = '" . $_POST["category_id"] . "'";
$results = $mysqlconnection->query($query);
$shelf_text = '';
foreach($results as $shelf) {
$shelf_text .= $shelf["shelf_id"]." - ".$shelf["shelf_code"];
}
echo $shelf_text;
exit;
}
?>
Hope this will give you and Idea. I haven't tested this code so please pardon if there is any syntax error. Also tweak the code in PHP file so you can show the shelf_code and shelf_id as per your requirements.
You can do it like this, one way to achieve this
PHP
<?php
include_once ("../database/dbconnect.php");
if(!empty($_POST["category_id"])) {
$query ="SELECT * FROM tbl_shelf_place WHERE shelf_id = '" .
$_POST["category_id"] . "'";
$results = $mysqlconnection->query($query);
foreach($results as $shelf){
echo $shelf["shelf_code"];
}
}
?>
No need to return option now. Just read the data returned and add it to your textbox element in HTML
HTML
<select id="book_category" name="book_category" class="selectpicker" data-
style="btn btn-danger" onChange="getState(this.value);" required>
<option disabled selected value=""> Choose Category</option>
<?php
$query = "SELECT category_id , category_name FROM tbl_category";
$stmt = $mysqlconnection->prepare($query);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($category_id , $category_name);
while($stmt->fetch())
{
$getcategoryID = $category_id;
$getcategoryname = $category_name;
echo "<option value='{$getcategoryID}'>{$getcategoryname}
</option>";
}
?>
</select>
// Add HTML Textbox here
<input type="text" id="book_shelf">
Rest of the code should be same.

Content changeable Dropdownlist from database data

Am loading my user records from database to the table, Am using:
foreach ($userArray as $key => $value) {
I have isAdmin row which contains 'Y', 'N' data.I need to make dropdownlist from this row.If data in Db is 'Y' other dropdownlist option should be 'N' and v/v.
Am using <select id="<?php echo $userArray[$key]["isAdmin"]?>">
I tried something like that, but if my data is Y it gives two more options , Y and N, it should be N, how to repair that?Here is my code:
<select id="<?php echo $userArray[$key]["isAdmin"]?>">
<option value="<?php echo $userArray[$key]["isAdmin"]?>">
<?php echo $userArray[$key]["isAdmin"];
if (strcmp($userArray[$key]["isAdmin"],"Y")==0){
?></option>
<option value="N">N</option>
<option value="Y" style="display:none;">Y</option>
<?php
}
else
?>
<option value="Y">Y</option>
<option value="N" style="display:none;">N</option>
</select>
I found a solution, here is my code:
<option value="<?php echo $userArray[$key]["isAdmin"]?>">
<?php echo $userArray[$key]["isAdmin"];
if (strcmp($userArray[$key]["isAdmin"],"Y")==0){
?></option>
<option value="N">N</option>
<?php
}
if (strcmp($userArray[$key]["isAdmin"],"N")==0){
?>
<option value="Y">Y</option>
<?php
}
?>
</select>
Create a if condition in your nav bar checking isAdmin and then echo the other dropdownlist.
if($isadmin == "Y"){
echo '?>
//Put Html code here for admin
<?php ';
}else{
echo'?>
//Put Html code here for non admin
<?php';
}

Categories