How to create query mysql in jquery like a clone - php

i have a query in php :
<div class="col-md-7" id="col_bahan">
<select name="bahan[]" id="bahan" class="form-control bahan_asli">
<option value="0">--pilih--</option>
<?php
$qry = $db->query("SELECT * FROM bahan");
while ($bahan = $qry->fetch_array()) {
$qry2 = $db->query("SELECT * FROM warna WHERE id_warna='$bahan[id_warna]'");
$warna = $qry2->fetch_array();
?>
<option value="<?php echo $bahan['id_bahan']; ?>"><?php echo $bahan['id_bahan']; ?> - <?php echo $bahan['nm_bahan'] . ' Warna ' . $warna['nm_warna']; ?></option>
<?php
}
?>
</select>
</div>
I want to copy/clone this code in jquery, then I render element in HTML. I don't like using .clone() for it,

Try this,
$("#div").html($("#col_bahan").html());

Related

how to get the data on script and load it in query

Why can't I get the value of the element? sorry I'm new at coding and trying to learn
<script>
function getdll() {
document.getElementById('lblmess').innerHTML =
(formid.listid[formid.listid.selectedIndex].value)
$element = document.getElementById("lblmess");
console.log($element.innerHTML)
}
</script>
<form name="formid">
<select class="custom-select" name="listid" onchange="getdll()">
<option value="0"></option>
<?php
$hall_qry = $conn->query("SELECT * FROM `office_hall` WHERE o_id !=0 order by `office_name` asc");
while ($row = $hall_qry->fetch_assoc()) : ?>
<option value="<?php echo $row['o_id'] ?>"><?php echo $row['office_name'] ?></option>
<?php
endwhile;
?>
</select>
<br><br>
<select class="custom-select">
<?php
$hall_qry = $conn->query("SELECT * FROM `assembly_hall` WHERE o_id = '$element' order by `room_name` asc");
while ($row = $hall_qry->fetch_assoc()) : ?>
<option value="<?php echo $row['id'] ?>"><?php echo $row['room_name'] ?></option>
<?php
endwhile;
?>
</select>
<label id="lblmess"></label>
</form>
this is where I use this code sorry if my coding id is like that .............................................................
Try:
onchange="getdll(this.value)"
THEN in your script, put a parameter
function getdll(val){
alert(val);
}
Should be able to get the value you selected

Duple select from database in same table but diffent proposes

First option of select must be the name referring to the ID. The remaining select options are the remaining names
<select class="input" name="client_id">
<?php
$sel_client_detail="Select * from client WHERE client_id=".$id."";
$result_detail = mysqli_query($con,$sel_client_detail);
while($new_record_row = mysqli_fetch_assoc($result_detail)) { ?>
<option selected><?php echo $row['nome'];?></option>
<?php };?>
<?php
$sel_client="Select * from client";
$result = mysqli_query($con,$sel_client);
?>
<option>-----------</option>
<?php while($new_record_row = mysqli_fetch_assoc($result)) { ?>
<option><?php echo $new_record_row['nome'];?></option>
<?php };?>
</select>
Output:
<select>
<option selected> Izzi (current ID name)</option>
<option> ____________</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
</select>
If you want the user to be first in your option list just run the query once and build the HTML parts in 2 seperate strings. Then once the loop is complete put them together and echo them
<?php
echo '<select class="input" name="client_id">';
$itsme = '';
$others = '<option>-----------</option>';
$sql = "Select * from client";
$result = $con->query($sql);
while($row = $result->fetch_assoc()){
if ( $id == $row['id'] ) {
$itsme = "<option selected='selected'>$new_record_row[nome]</option>";
} else {
$others += "<option>$new_record_row[nome]</option>";
}
}
// put the option tags together in the order you specified
echo $itsme . $others . '</select>';
Here's a different, but more conventional, approach to this common scenario:
Why not just make the chosen ID selected when you get to it in the list? Then it will still show to the user first. It's more efficient than having two separate queries.
Like this:
<select class="input" name="client_id">
<?php
$sel_client="Select * from client";
$result = mysqli_query($con,$sel_client);
?>
<option>-----------</option>
<?php while($new_record_row = mysqli_fetch_assoc($result)) { ?>
<option <?php echo ($new_record_row["client_id"] == $id ? "selected": ""); ?> ><?php echo $new_record_row['nome'];?></option>
<?php };?>
</select>

Set first value of dynamic HTML option to empty

<select id="section" name="section">
<?php
include("Nethost.php");
$section = "";
$yr = "";
$sql = mysql_query("SELECT DISTINCT * FROM section ORDER BY yrlvl, section");
while ($row = mysql_fetch_array($sql)){
$section = $row['section'];
$yr = $row['yrlvl'];
?>
<option value="">Select</option>
<option <?php $result2 = mysql_query("SELECT section FROM student WHERE idnumber = '$idnumber'");
if(mysql_num_rows($result2) > 0) { ?>
selected="selected" <?php } ?> value="<?php print $section; ?>"><?php print $yr; ?> - <?php print $section; ?></option>
<?php } ?>
</select>
Above is the php code of the select option. Populated with data from database table, how can I set that the first value if empty. I tried adding a Select but this is the result.
The option select keeps repeating. What to do with this?
Try this
<select id="section" name="section">
<?php
include("Nethost.php");
$section = "";
$yr = "";
$sql = mysql_query("SELECT DISTINCT * FROM section ORDER BY yrlvl, section");
?>
<option value="">Select</option>
<?php
while ($row = mysql_fetch_array($sql)){
$section = $row['section'];
$yr = $row['yrlvl'];
?>
<option <?php $result2 = mysql_query("SELECT section FROM student WHERE idnumber = '$idnumber'");
if(mysql_num_rows($result2) > 0) { ?>
selected="selected" <?php } ?> value="<?php print $section; ?>"><?php print $yr; ?> - <?php print $section; ?></option>
<?php } ?>
</select>
Just move that option line "Select" out of your php code:
<select id="section" name="section">
<option value="">Select</option>
<?php
...your php code
?>
</select>

Throw select php variable into a form and convert it to a POST variable

I'm trying to use a drop down menu to load data from a select value. I want it passed into a php document in order to use the data that I need. What should I do? Thanks in advance.
This is my code for the select menu:
$sqlz = "SELECT * FROM content_temp1 WHERE user_uname='$uname'";
$resultz = mysql_query($sqlz);
$checkz = mysql_numrows($resultz);
$count = 0;
?>
<select name="ltemp">
<?php
while($count<$checkz){
$selectname=mysql_result($resultz,$count,"temp1_name");
?>
<option value="<?php echo "$selectname";?>"><?php echo $selectname;?></option>
<?php
$count++;
}
?>
</select>
<input type="submit" value="Load Template" class="ufbutton"><br></center>
</form>
and this is my php page
$uname = $_GET['username'];
$loadtemp = $_POST['ltemp'];
header("Location:editing1.php?username=$uname&tempname=$loadtemp");
it seems that you have use " inside " in this line :
<option value="<?php echo \"$selectname\";?>"><?php echo $selectname;?></option>
try this:
$sqlz = "SELECT * FROM content_temp1 WHERE user_uname='$uname'";
$resultz = mysql_query($sqlz);
$checkz = mysql_numrows($resultz);
$count = 0;
?>
<select name="ltemp">
<?php
while($count<$checkz){
$selectname=mysql_result($resultz,$count,"temp1_name");
?>
<option value="<?php echo \"$selectname\";?>"><?php echo $selectname;?></option>
<?php
$count++;
}
?>
</select>
Alternative :
$sqlz = "SELECT * FROM content_temp1 WHERE user_uname='$uname'";
$resultz = mysql_query($sqlz);
?>
<select name="ltemp">
<?php
while ($row = mysql_fetch_array($resultz, MYSQL_ASSOC)) {
$selectname=$row["temp1_name"];
?>
<option value="<?php echo \"$selectname\";?>"><?php echo $selectname;?></option>
<?php } ?>
</select>
Be careful this is ripe for sql injection without validating the input;
SQL Injection with GET

Populate html <select> with array data from mysql in PHP

I can see the query returning results, but I can't seem to be able to put them into a html dropdown box. Also, the dropdown box has just as many entries as the query returns, but THEY ARE ALL WHITE SPACES. HOWEVER, the page source shows correct option values such as
<option value="3 John"></option>
<option value="Jude"></option>
<option value="Revelation"></option>
Can somebody help me out? Why dont they actually show in the dropdown box?
<html>
<?php
//Connect to the database
$mysqli = new mysqli("localhost", "root", "", "bible");
//Return an error if we have a connection issue
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
//Query the database for the results we want
$query = $mysqli->query("select distinct bname as Name from kjv limit 1");
//Create an array of objects for each returned row
while($array[] = $query->fetch_object());
array_pop($array);
//Print out the array results
print_r($array);
?>
<h3>Dropdown Demo Starts Here</h3>
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option->Name; ?>"></option>
</select>
<?php endforeach; ?>
Try This
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option['Name']; ?>"><?php echo $option['Name']; ?></option>
<?php endforeach; ?>
</select>
After the query is executed use the while loop to add the options to select
$query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>
<select>
<?php while($option = $query->fetch_object()){ ?>
<option><?php echo $option->Name; ?></option>
<?php } ?>
</select>
Not sure what the array_pop is doing in the code
AS TIM WAX SAID THIS IS THE SOLUTION
$query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>
<select>
<?php while($option = $query->fetch_object()){ ?>
<option><?php echo $option->Name; ?></option>
<?php } ?>
</select>
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option->Name; ?>"></option>
<?php endforeach; ?>
</select>
You ended your loop in a way that it also create <select> tag again and again. Change it and try again. I don't know much about .php but it could be a problem in showing your dropdown box.
here is mine .. im a beginner but it works for me,
$query = $mysqli->query("SELECT * FROM `student_type_db`"); //table of student type
echo "<select>";
while($row = $query->fetch_array()){
echo "<option>";
echo $row['student_type'] . " - " . $row['student_description'];
echo "</option>";
}
echo "</select>";
// student type = 1 | student description = regular
// output : 1 - regular

Categories