How to give id to auto generated element automatically - javascript

I want to make a dropdownlist that contains something from my database, all going well until I need to give each of the option unique id
<div class="div3" >
<select id="input2" class="drop" name="a" required>
<option value="" selected disabled>a</option>
<?php while ($row1 = mysqli_fetch_array($result)) :; ?>
<option class="option"><?php echo $row1[0]; ?></option>
<?php endwhile; ?>
</select>
</div>

Put an id unique value in option tag, and order you code for make an array with html tag, for example if $row1[0] = uniqueid and $row1[1] = name
<?php while ($row1 = mysqli_fetch_array($result)){?>
<option id="<?php echo $row1[0]; ?>" class="option"><?php echo $row1[1]; ?></option>
<?php }?>
Best Regards

Related

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>

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';
}

How to remove an empty string from an array. I am not sure weather it is from PHP or Ajax

I am using Ajax in order to display drop-down list when a product group is selected. My Ajax returns a list from the query page(dropd.php). However the problem is the array consists of some empty space between each element. Hence when I did inspect in chrome below is the output I am getting,
<option value="1">Gear pumps</option>
<option></option>
<option value="2">Piston pumps</option>
<option></option>
How could I remove "<option></option>" from the drop-down list? If I take option tag out from html what I receive is;
Gear pumps^piston pumps^vane pumps...etc
If you can notice the space(^).
Ajax code
<script>
$(document).ready(function(){
$('#sel1').on('change', function(){
$.post("dropd.php",{vals:$("#sel1 option:selected").text()},
function(data){$.trim($("#sel2").html( data ));});
});
});
</script>
dropd.php
<?php
require('../config/connection.php');
if($_POST['vals']){
$values = mysqli_real_escape_string($dbc,$_POST['vals']);
$query = "SELECT * FROM prdct_categories WHERE product = '$values'";
$result = mysqli_query($dbc, $query);
?>
<option value="">Select subgroup</option>
<?php
foreach($result as $row)
{ ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['subgroup']; ?><option/>
<?php
}
}
mysqli_close($dbc);
?>
Ajax data goes here
<div class="form-group">
<label for="inputsubprdctgrp" class="col-sm-4 control-label" >Product Subgroup</label>
<div class="col-sm-8">
<select class="form-control" id="sel2" >
</select>
</div>
</div>
Thanks in advance.
There may be 2 issues:
The SQL result gives an empty value which can be fixed by using the array_filter() method:
$new_result = array_filter($result);
You wrote option/ instead of **/option
Hope this helps :)
just dont put those values in options if it is blank..
you can use below code.
$result = mysqli_query($dbc, $query);
?>
<option value="">Select subgroup</option>
<?php
foreach($result as $row)
{
if(trim($row['subgroup'])!=''){
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['subgroup']; ?><option/>
<?php
}
}
mysqli_close($dbc);
?>
Just use `
$('#sel2').find('option:empty').remove();`
after you load content from ajax,
if you want to do it in javascript, otherwise avoid it in PHP in the first place.

php html form select option to populate text field jquery

I'm editing a wordpress plugin:
It currently has a dropdown option list for a user, and based on that user, I want to fill a text field with that users email.
selection option dropdown:
<div>
<label for="Customer_ID"></label>
<select name="Customer_ID" id="Customer_ID" />
<option value='0'>None</option>
<?php foreach ($Customers as $Customer) { ?>
<option value='<?php echo $Customer->Customer_ID; ?>'><?php echo $Customer->Customer_Name; ?></option>
<?php } ?>
</select>
</div>
The customer email is stored in $Customer->Customer_Email
And Im trying to fill in the following value when a user changes the selected user:
<div class="form-field">
<label for="Order_Email"></label>
<input type='text' name="Order_Email" id="Order_Email" />
</div>
I've played around with filling the Order_Email with the following script, but it just sents the value as the customer_id. How do I change the value to the email
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#Customer_ID').on('change', function () {
var selection = $(this).val();
$('#Order_Email').val(selection);
});
});
</script>
You could just add a title attribute to the options to equal whatever the email value is:
Play around with it here: http://jsfiddle.net/7BUmG/3876/
This might not be the best solution and hacky, but it should do the trick. This will also break after the plugin is updated
<div>
<label for="Customer_ID"></label>
<select name="Customer_ID" id="Customer_ID" />
<option value='0'>None</option>
<?php foreach ($Customers as $Customer) { ?>
<option title='<?php echo $Customer->Customer_Email; ?>'value='<?php echo $Customer->Customer_ID; ?>'><?php echo $Customer->Customer_Name; ?></option>
<?php } ?>
</select>
</div>
and
$(document).ready(function(){
$('#Customer_ID').change(function() {
var email = $(this).find("option:selected").attr("title");
$('#Order_Email').val(email);
});
});

php script renders as a comment inside purchased template

I recently bought a template and started working on it. the template includes only html,css and javascript files. i have a problem that every php script i am trying to place inside the html filee, is being rendered as a comment. so when i try to add this script:
<?php
include_once "dbConnect.php";
//user name dropdown
$query = "SELECT user_name FROM users";
$result = mysql_query($query); ?>
<select name="selectedName" id="userSelected">
<option value="" disabled selected>user name</option>
<option value="" >All</option>
<?php while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
<option value="<?php echo $line['user_name'];?>">
<?php echo $line['user_name'];?>
</option>
<?php } ?>
</select>
<!--script name dropdown-->
<?php
$query = "SELECT script_name FROM scripts";
$result = mysql_query($query); ?>
<select name="selectedScript" id="scriptSelected">
<option value="" disabled selected>script name</option>
<option value="" >All</option>
<?php while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
<option value="<?php echo $line['script_name'];?>">
<?php echo $line['script_name'];?>
</option>
<?php } ?>
</select>
?>
using this line:
<?php include_once "dropdown.php"?>
in the developer tool it appears like this:
<!--?php include_once "dropdown.php"?-->
any idea what can cause that? this code already worked in an html project i created and which im trying to move into this template.
*the template name its metronic
the moment you place php in a HTML file, it is rendered as HTML. You should change the extension to .php then it will be able to run the PHP itself. (The HTML itself will not be affected)

Categories