How Add select box Dynamically, which retrieve data from Database - javascript

I have problem with Web Development,
i have added drop down list using PHP, MySQL and HTML. Now i want to use button and dynamically generate same Select box again and again. How can i do this, please help me..
Here is my Select Box Code.
<Select name="txt-computer_sn" class="form-control" id="txt-computer_sn">
<?php
include ('../svr/connection.php');
$sql = "SELECT * FROM supplier";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result)) {
echo '<option value=' . $row['supplier_id'] . '>' . $row['supplier_name'] . '</option>';
}
?>
</select>

if the data of dropdown will remain same then you can do that in this way
html and php
<Select name="txt-computer_sn" class="form-control dynm_drop" id="txt-computer_sn">
<?php
include ('../svr/connection.php');
$sql = "SELECT * FROM supplier";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result)) {
echo '<option value=' . $row['supplier_id'] . '>' . $row['supplier_name'] . '</option>';
}
?>
</select>
jquery
$("#buttonid").click(function(){
$(".dynm_drop").clone().appendTo("class or id of element");
});

Try this:
<div class="select_wrapper">
<select name="txt-computer_sn[0]" class="form-control" id="txt-computer_sn_0">
<?php
include ('../svr/connection.php');
$sql = "SELECT * FROM supplier";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result)) {
echo '<option value=' . $row['supplier_id'] . '>' . $row['supplier_name'] . '</option>';
}
?>
</select>
<input type="button" name="add_select" class="add_select" value="Add New">
</div>
<script type="text/javascript">
$(document).ready(function() {
var s=1;
$('.add_select').click(function(e){ //Once add button is clicked
e.preventDefault();
s++;
$('.select_wrapper').append('<select class="form-control" name="txt-computer_sn['+s+']" id="txt-computer_sn_'+s+'">'+$('#txt-computer_sn_0').html()+'</select>');
});
});
</script>

there is 3 method to do this.i am not familiar with php.but i tell the method how to work it out.
1.you have to set an hidden field for count
2.
<select class="form-control" name="txt-computer_sn--" id="txt-computer_sn_--">
3.in the script write this var rep=/--/gi;
4. take your count from hidden field
var Count= $("#Count").val()
Count =++Count
("#div1").append($("#div2").html().replace(rep,Count));
on click the add button you have append the div2 with select to another
div1
on remove the
$("#div3"+id).remove()
in the order
<div1></div>
<div2 id="name1--"style=display:none>
<div3 id="name2--"></div3>
</div2>
it will work.use this concept .you can do this in php also..

Related

PHP set dropdown value after page refresh

I'm having trouble setting the value of the dropdown after the form input refreshes the page. I can get the value but no matter what I try I'm unable to set the dropdown after the page refreshes. I've tried a number of different ideas I've found online too. I've tried both JavaScript and PHP solutions and all I can do is get the value but not set it. This is the code I have so far, which returns the drop down ID, I just need to know how to use it. I appreciate any help, thanks!
<?php
$pdo = new PDO('mysql:host=localhost; dbname=db', 'root', 'password')'
$sql = "SELECT divid, division FROM divisions ORDER BY division ASC";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$divs = $stmt->fetchAll();
?>
<form method="post">
<select id="divi" name="divisions">
<?php foreach($divs as $div): ?>
<option value="<?= $div['divid'];?>"><?= $div['division']; ?></option>
<?php endforeach; ?>
</select>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['divisions'])){
$selected = $_POST['divisions'];
echo 'Selected: " . $selected;
} else {
echo 'Select division.';
}
}
?>
It's not very clear to me what you really want, however it looks like you want to select default:
<select id="divi" name="divisions">
<?php foreach($divs as $div): ?>
<option <?php echo ("mycondition ex: 'id == 1'") ? "selected" : NULL ?> value="<?= $div['divid'];?>"><?= $div['division']; ?></option>
<?php endforeach; ?>
</select>
You are simply missing any code that sets 'selected="selected"' in the HTML for the select field.
Also, your code is very hard to read so I've cleaned up the loop a little bit.
<form method="post">
<?php
echo '<select id="divid" name="divid">';
foreach ($divs as $div) {
$selected = '';
if (isset ($_POST['divid']) && ($_POST['divid'] == $div['divid'])) {
$selected = 'selected="selected"';
}
echo '<option value="' . $div['divid'] . '" ' . $selected . '>' . $div['division'] . '</option>';
}
echo '</select>';

retaining drop down value after submit in PHP

I have a drop down pull from an oracle database. when I select a drop down value and click a show details button , the details show but the drop down defaults back to the first one in the list. I need it to stay on the selected value.
I am doing this in PHP
I have tried this but it cannot recognize the
<form name= "fund" method="post" >
<label id= "fund" for="fund">Fund:</label>
<Select name="fund" id="fund">
<option value="--Select A Fund--">--Select a Fund--</option>
<?php
$sql = 'SELECT Account_name ||\' - \'|| Fund_id as FUND, FUND_ID FROM FUND_ACCOUNTS';
$stid = oci_parse($conn, $sql);
$success = oci_execute($stid);
while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC))
{
$selected = (!empty($_POST['fund']) && $_POST['fund'] == $row['FUND']) ? 'selected' : '';
echo '<option value="' . $row['FUND'] . '" ' . $selected . '>' . $row['FUND'] . '</option>';
}
?>
</select>
<input type="submit" name="fund"
value="Show Current Fund Investors"/>
</form>
<BR>
<?php
echo 1 . $row['FUND'];
echo 1 . $_POST['fund'];
?>
But $selected is never populated. Not sure where to go from here, and I am not a web developer. Any ideas where I am going wrong ?
the output of the final echos is 11Show Current Fund Investors
Likely this line just needs to do this:
$selected = (!empty($_POST['fund']) && $_POST['fund'] == $row['FUND'])) ? 'selected' : '';
What you have is matching the default option with || ($row['FUND'] == '--Select A Fund--') and probably also adding selected to the one you actually do select from the drop down. View the page source in the browser, there might be two options with the selected attribute.
By default, if nothing is selected, it will just show the first item in the dropdown which is --Select A Fund-- anyway.
Also you probably should have a space before the $selected variable and after the quote:
echo '<option value="' . $row['FUND'] . '" ' . $selected . '>' . $row['FUND'] . '</option>';
Should come out to:
<option value="whatever" selected>Whatever</option>
Edit
Based on your edit, you need to remove the name attribute from the submit button, or rename it. It’s conflicting with your select name.
<input type="submit" value="Show Current Fund Investors"/>
A tip:
You should encapsulate your fetching of that list in a function (at the very least) and include it in the page at the top:
/functions/fetchFundAccounts.php
<?php
function fetchFundAccounts($conn)
{
$stid = oci_parse($conn, 'SELECT Account_name ||\' - \'|| Fund_id as FUND, FUND_ID FROM FUND_ACCOUNTS');
$success = oci_execute($stid);
$results = [];
while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC)) {
$results[] = $row;
}
return $results;
}
To use:
<?php include(__DIR__.'/functions/fetchFundAccounts.php') ?>
<form name= "fund" method="post" >
<label id= "fund" for="fund">Fund:</label>
<select name="fund" id="fund">
<option value="--Select A Fund--">--Select a Fund--</option>
<?php foreach(fetchFundAccounts($conn) as $row): ?>
<option value="<?php echo $row['FUND'] ?>" <?php echo (!empty($_POST['fund']) && $_POST['fund'] == $row['FUND']) ? 'selected' : '' ?>><?php echo $row['FUND'] ?></option>
<?php endforeach ?>
</select>
<input type="submit" value="Show Current Fund Investors"/>
</form>

How i obtain 2nd parameter from the datalist in jquery style?

Here below html code of the field form that shot datalist of customer nr like this: 0001 - Custmer test
<label>Nr. Customer</label>
<input type="text" class="form-control" name="nr" placeholder="Choose customer" list="list_nr" id="nr" >
<datalist id="list_nr">
<?php
$sql = mysql_query("SELECT nr, name FROM database.table WHERE status='M'");
while ($row = mysql_fetch_assoc($sql)) {
//echo "<option value=".$row['name'].">" . $row['nr'] . "-" . $row['name'] . "</option>";
echo "<option value=".$row['name'].">" . $row['name'] . "</option>";
}
?>
</datalist>
// here below show only 1st
$('#nr').change(function(){
var secondOptionValue = $('#nr').val();
$('#test').val(secondOptionValue);
});

How can I get a value from a select box on the same page

I'm trying to use PHP and Javascript to make a category selector box.
I've set it up so that the Javascript will show the steps in order of being selected, and hide after being deselected.
However, I can't figure out how to take the selected options "id" or "value" and pass it to the next line. (once the chosen id or value is passed on, the next list can load)
Here is my code, Thanks in advance for looking. And please, if I'm doing something wrong or not the right way. Let me know and/or show me the right way to do it.
<?php
include($_SERVER["DOCUMENT_ROOT"] . "/inc/header.php");
include($_SERVER["DOCUMENT_ROOT"] . "/inc/search.php");
?>
<div class="content">
<form>
<select name="categorys" class="newcatediv" id="step1" size="3" onchange="mine(this.value)">
<?php
$result_c = mysqli_query($con,"SELECT * FROM categories ORDER BY category_name ASC");
while($row = mysqli_fetch_array($result_c))
{
echo '<option class="mso" id="'. $row['category_nameid'] .'"value="';
echo $row['category_nameid'] .'">' . $row['category_name'] . '</option>';
}
?>
</select>
<select name="sections" class="newcatediv" id="step2" size="3" onchange="mine2(this.value)">
<?php
$var_c = ????
$result_s = mysqli_query($con,"SELECT * FROM sections WHERE category_nameid='$var_c' ORDER BY section_name ASC");
while($row = mysqli_fetch_array($result_s))
{
echo '<option class="mso" id="'. $row['section_nameid'] .'"value="';
echo $row['section_nameid'] .'">' . $row['section_name'] . '</option>';
}
?>
</select>
<select name="subsections" class="newcatediv" id="step3" size="3">
<?php
$var_s = ????
$result_ss = mysqli_query($con,"SELECT * FROM subsections WHERE section_nameid='$var_s' ORDER BY subsection_name ASC");
while($row = mysqli_fetch_array($result_ss))
{
echo '<option class="mso" id="'. $row['subsection_nameid'] .'"value="';
echo $row['subsection_nameid'] .'">' . $row['subsection_name'] . '</option>';
}
?>
</select>
</form>
</div>
<?php
include($_SERVER["DOCUMENT_ROOT"] . "/inc/footer.php");
?>
By default, the first option in a <select> is selected, so this would work:
<select name="categorys" class="newcatediv" id="step1" size="3" onchange="mine(this.value)">
<?php
$result_c = mysqli_query($con,"SELECT * FROM categories ORDER BY category_name ASC");
$var_c = null;
while($row = mysqli_fetch_array($result_c))
{
if($var_c == null) $var_c = $row['category_nameid'];
echo '<option class="mso" id="'. $row['category_nameid'] .'"value="';
echo $row['category_nameid'] .'">' . $row['category_name'] . '</option>';
}
?>
</select>
<select name="sections" class="newcatediv" id="step2" size="3" onchange="mine2(this.value)">
<?php
$result_s = mysqli_query($con,"SELECT * FROM sections WHERE category_nameid='$var_c' ORDER BY section_name ASC");
$var_s = null;
while($row = mysqli_fetch_array($result_s))
{
if($var_s == null) $var_s = $row['section_nameid'];
echo '<option class="mso" id="'. $row['section_nameid'] .'"value="';
echo $row['section_nameid'] .'">' . $row['section_name'] . '</option>';
}
?>
</select>
<select name="subsections" class="newcatediv" id="step3" size="3">
<?php
$result_ss = mysqli_query($con,"SELECT * FROM subsections WHERE section_nameid='$var_s' ORDER BY subsection_name ASC");
while($row = mysqli_fetch_array($result_ss))
{
echo '<option class="mso" id="'. $row['subsection_nameid'] .'"value="';
echo $row['subsection_nameid'] .'">' . $row['subsection_name'] . '</option>';
}
?>
</select>
Cheers
Hi :) You Can't Process that at the same page using Php. But you can do that with this jquery including 3 pages.
First Page:
$(document).ready(function(){
$("#step1").change(function(){
var id=$("#step1").val();
alert(id); //shouts the value of the selected step1
$.post("select_step2.php", {id:id}, function(data){
$("#step2").empty();
$("#step2").append(data);
$("#step2").change(function(){
var id2=$("#step2").val();
alert(id2); //shouts the value of the selected step2
$.post("select_step3.php", {id:id2}, function(data){
$("#step3").empty();
$("#step3").append(data);
});
});
});
});
});
The above code is for jquery where you can call each data's that depends on each step.
<?php
include($_SERVER["DOCUMENT_ROOT"] . "/inc/header.php");
include($_SERVER["DOCUMENT_ROOT"] . "/inc/search.php");
?>
<form>
First Step: <select name="categorys" class="newcatediv" id="step1" size="3">
<?php
$result_c = mysqli_query($con,"SELECT * FROM categories ORDER BY category_name ASC");
while($row = mysqli_fetch_array($result_c))
{
echo '<option class="mso" id="'. $row['category_nameid'] .'"value="';
echo $row['category_nameid'] .'">' . $row['category_name'] . '</option>';
}
?>
</select>
Second Step: <select name="sections" class="newcatediv" id="step2" size="3"></select>
Third Step: <select name="subsections" class="newcatediv" id="step3" size="3"></select>
Code for you select_step2.php:
<?php
//Please include the connection to your database here :)
$var_c = trim($_POST['id']);
$section = "";
$result_s = mysqli_query($con,"SELECT * FROM sections WHERE category_nameid='$var_c' ORDER BY section_name ASC");
while($row = mysqli_fetch_array($result_s))
{
$section.="<option value='$row[section_nameid]'>$row[section_name]</option>";
}
echo $section;
?>
Code for your select_step3.php:
<?php
//database connection here
$var_s = trim($_POST['id']);
$subsection= "";
$result_ss = mysqli_query($con,"SELECT * FROM subsections WHERE section_nameid='$var_s' ORDER BY subsection_name ASC");
while($row = mysqli_fetch_array($result_ss))
{
$subsection.="<option value='$row[subsection_nameid]'>$row[subsection_name]</option>";
}
echo $subsection;
?>

set a particular option in a select box using jquery

I'm unable to perform the desired event.
<?php
include_once 'includes/db.php';
$result = mysql_query('SELECT country,code FROM countries') or die(mysql_error());
echo '<select id="CountryCode">';
echo '<option value="Select">Select</option>';
while ($row = mysql_fetch_array($result))
{
echo '<option value=$row["country"]>'.$row['country'].'</option>';
}
echo '<option value="Other">Other</option>';
echo '</select>';
?>
<input id="country" type="hidden" value="IN"/>
<script>
$(function()
{
$('#CountryCode').val($('#country').val());
});
</script>
Everything works fine. But the desired item is not selected in the select box
You need to set the Attribute "selected" of the option to "selected". Try this (untested):
$("#CountryCode").val($('#country').val()).attr('selected','selected');
The echo in your while() loop should look like this:
echo '<option value="'.$row["country"].'">'.$row['country'].'</option>';
Your string above was printing a literal $row["country"] instead of the value that it contained.

Categories