Javascript - How to change a new value on drop down - php

When we change the name="quantity" and the $product['price'] value will changing too. Here will have dynamic quantity and price. How to do that using jquery/javascript.
<?php
$select = "SELECT * FROM `products` ORDER BY `id` DESC LIMIT 10";
$query = mysql_query($select) or die(mysql_error());
?>
<ul>
<?php while($product = mysql_fetch_array($query)) { ?>
<li>
<p><?php echo $product['name'];?></p>
<p> Quantity
<select name="quantity">
<?php for($i=1;$i<=$product['quantity'];$i++) { ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
</select>
</p>
<p><?php echo $product['price'];?></p>
</li>
<?php } ?>
</ul>
Let me know :)

Using jQuery:
$(document).ready(function() {
$("#quantity_select").bind("change", function() {
selected_option = $("option:selected").val();
$("#counter").html(selected_option);
});
});
...
<select id="quantity_select">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<div id="counter"></div>
You can use text() instead of val() if you want to see actual text instead of value attribute .

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

PHP dropdown populated from database

I need to pull down a column of color codes for a color picker site I'm making but it won't work:
<select name="select" id="dropdown">
<option id="0">-- Select Color --</option>
<?php
$getColors = mysql_query("SELECT * FROM color_codes");
while($list = mysql_fetch_array($getColors)){
?>
<option id="<?php echo $list ['colorID']; ?>">
<?php echo $list['color_code'] ?></option>
<?php } ?>
</select>
Any ideas?
just did a couple of changes to your code and it worked for me, Hope it works for you.
<?php
$mysqli= new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME) or die(mysqli_connect_error());
$sql="select * from test";
$result=$mysqli->query($sql);
?>
<select name="select" id="dropdown">
<option id="0">-- Select Color --</option>
<?php
while($list = $result->fetch_array(MYSQLI_ASSOC)){
?>
<option id="<?php echo $list['id']; ?>">
<?php echo $list['name'] ?></option>
<?php } ?>
</select>

How do I Populate Multi Select drop dwon using php

Here I am listing all cars.customers want to compare car so they will select from this drop down. A person can select multiple cars. At the first time he is selecting 'Audi' and Saab' I will store it into data base next if he came I need to populate Saab and audi as select how I can do this using php
<select name="cars" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
Here is my code
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
?> <option value="<?php echo($entry['ID']); ?>" ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
Following code I tried $resources contain select cars
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
if($resources->num_rows() >0)
{
foreach($resources->result_array() as $car):
if($entry['ID'] == $employee['car_id'])
{
$select = 'selected="selected"';
}
else
{
$select = '';
}
endforeach;
}
?> <option value="<?php echo($entry['ID']); ?>" <?php echo $select;?> ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
but it showing error
Here, try something like this, and see if it works:
Here is the controller:
<?php
function something(){
$data = array();
$data['cars'] = $this->some_model->some_function_to_return_cars_array();
$data['selected'] = $some_array_of_selected_cars();
$this->load->view('some_view', $data);
}
?>
And this is the view:
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<option value="">Select:</option>
<?php
foreach( $cars as $key => $val ){
?>
<option value="<?php echo $val['some_id'] ?>"
<?php
if( in_array( $val['some_id'], $selected ) ) echo ' selected';
?>
><?php echo $val['some_name'] ?></option>
<?php
}
?>
</select>

Insert MySQL table column array into new MySQL table

I have a php form that gets dumped into a MySQL table. I create an array that is populated from a Product table using php.
Queries the table to get the values for the array - works fine.
<?php
function getProducts ($id)
{
$sql =("SELECT * FROM products where products.CAT_ID = $id AND DISABLED ='NO'");
$result = mysql_query($sql);
if ($result > 0)
{
$products = array();
$x=0;
while($row = mysql_fetch_array($result))
{
$products[$x]= $row;
$x += 1;
}
return $products;
}
else return false;
}
function newProd(){
global $products;
}
?>
Then I use the above array to create labels in a javascript accordion. Next to the labels -are select boxes. (Removed unnecessary page elements.. ) Html/php form below that displays the above array.
<?php include('includes/query.php');?>
Inventory
<body onload="toggleField()">
<div id="container" class="container_16">
<form name="form1" id="form1" action="insert.php" method="post" enctype="multipart/form-data" style="height: inherit">
<div id="Info" class="grid_16">
<fieldset>
<label id="Label1">
<span class="Labels">Store Information</span> <br>
</label>
<div>
<label for="name">
<span class="Labels"> Name :</span> </label>
<input id="name" name="name" type="text">
</div>
</fieldset>
</div>
<hr id="SpacePadding">
<hr class="SpacePadding">
<div class="clear"></div>
<!-- Start of the Accordion Container-->
<!-- Start array, make sure Category Array isn't false -->
<?php if($categoryArray !=false):?>
<!-- create the variables for modulus -->
<!-- Start the foreach loop for the category Array -->
<?php $cataCounter = 0; ?>
<?php $closeSection = false; ?>
<?php foreach($categoryArray as $row):?>
<?php if($cataCounter % 2 == 0): ?>
<div class="push_1 grid_8">
<?php $closeSection = false; ?>
<?php else: ?>
<div class="grid_8">
<?php $closeSection = true; ?>
<?php endif; ?><!-- insert modulus-->
<div class="AccordionTitle"><?php echo $row['CATEGORY_NAME'];?></div>
<div class="AccordionContent">
<table>
<?php foreach(getProducts($row['CAT_ID']) as $row):?>
<tr>
<th class="boldLabels"><?php echo $row['PRODUCT_NAME'];?>
<span class="label"></span></th>
<th><span class="Labels">Count:</span></th>
<td>
<select name="selCount[]" onChange="toggleField(this.value);" class="Listbox">
<option value="Select">Select One</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="Other">Other(Specify)</option>
</select>
<input type="text" name="otherCount[]" class ="Listbox" style="display: none;">
</td>
<td><span class="Labels">Need:</span></td>
<td>
<select name="selNeed[]" onChange="toggleField2(this.value);" class="Listbox">
<option value="Select">Select One</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="Other">Other(Specify)</option>
</select>
<input type="text" name="otherNeed[]" class ="Listbox" style="display: none;">
</td>
</tr>
<?php endforeach;?>
</table>
</div>
</div>
<?php if($closeSection): ?>
<div class="clear"></div>
<?php endif; ?>
<?php $cataCounter += 1; ?>
<?php endforeach;?>
<?php endif;?>
<input name="Submit" type="submit" value="submit">
</div>
</form>
<br>
</div>
<footer>
<div align="center">
<ul>
<li>Home</li>
<li>Daily Inventory Counts</li>
<li>Contact System Admin</li>
</ul>
</div>
</footer>
</body>
When submitted, I want the Product_Name to go inside a new table with the correlating select option.
(i took out the database connection info out.)
<?php
//Assign array
$SelCount = $_POST['selCount'];
$SelNeed = $_POST['selNeed'];
$otherNeed = $_POST['otherNeed'];
$otherCount = $_POST['otherCount'];
$limit = count($SelCount);
for($i=0;$i<$limit;$i++) {
$SelCount[$i] = mysql_real_escape_string($SelCount[$i]);
$SelNeed[$i] = mysql_real_escape_string($SelNeed[$i]);
$otherNeed[$i] = mysql_real_escape_string($otherNeed[$i]);
$otherCount[$i] = mysql_real_escape_string($otherCount[$i]);
if (($SelCount[$i]) !="Select" ) {
$sql = ("INSERT INTO inventory ( STORE_ID, REQUESTOR, ITEM_COUNT, ITEM_NEED, NEED_COUNT, OTHER_COUNT)
VALUES ('$_POST[store]', '$_POST[name]', '".$SelCount[$i]."', '".$SelNeed[$i]."', '".$otherNeed[$i]."', '".$otherCount[$i]."')");
if(mysql_query($sql ,$db))
echo "$i successfully inserted.<br/>";
else
echo "$i encountered an error. <br/>";
}
}
?>
Here's what I would do:
function getProducts($id)
{
$sql = 'SELECT * FROM products where products.CAT_ID = ' . $id . ' AND DISABLED = "NO"';
$result = mysql_query($sql);
if($result !== false) {
while($row = mysql_fetch_array($result)) {
$products[]= $row;
}
} else {
$products = false;
}
}
Then you can print it out via:
if($products = getData(id)) {
foreach($products as $product) {
echo $product['database_field_name'];
}
}
To see them all for debugging purposes, you can use this:
echo '<pre>' . print_r($products, true) . '</pre>';
Also, you have to sanitize $id somewhere using:
http://php.net/manual/en/function.mysql-real-escape-string.php and perhaps use intval() if it's an integer.
Store data in session:
session_start();
$_SESSION['name'] = $product['database_field_name'];
And then on the insert.php page you can read the data via:
echo $_SESSION['name'];
Then after you don't need it anymore, you can unset it:
unset($_SESSION['name']);

PHP while in edit mode show selected value in to drop down

This question was asked already, but my question is very simple.
In the my account page, I have the employee country in a dropdown.
How to select a value in the combo, when in edit mode?
Let's assume you have the user's country in $user_country and the list of all countries in $all_countries array:
<select id="country">
<?php
foreach ( $all_countries as $country ):
$selected = "";
if ( $country == $user_country )
$selected = "selected";
?>
<option value="<?php echo $country; ?>"
selected="<?php echo $selected; ?>">
<?php echo $country; ?>
</option>
<?php
endforeach; ?>
</select>
should work.
An option tag will be the default for a select list when the selected attribute is set. In the following code option 2 will show up as the current selected option when the page loads:
<select>
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
</select>
To achieve this in your PHP code conditionally display the selected attribute on your options against what the current value is:
<option value="1"<?php if($user['country'] == '1') { ?> selected="selected"<?php } ?>>1</option>
<option value="2"<?php if($user['country'] == '2') { ?> selected="selected"<?php } ?>>2</option>
<option value="3"<?php if($user['country'] == '3') { ?> selected="selected"<?php } ?>>3</option>
function p_edit_combo($cCurstatus,$h_code_default,$h_name=NULL){
<select name="<?php echo $cCurstatus;?>" id="<?php echo $cCurstatus;?>" class="main_form_select">
<option value="">Select</option>
<?php
$sql_h = "SELECT h_code,h_name FROM med_hl WHERE status = 1";
$sql_h_result = mysql_query($sql_h);
while($row=mysql_fetch_array($sql_h_result)){
$h_code = $row['h_code'];
$h_name = $row['h_name'];
?>
<option <?php if($h_code_default==$h_code){ ?> selected="selected" <?php }?> value='<?php echo $h_code; ?>' >
<?php echo $h_code."|".$h_name; ?>
</option>
<?php } ?>
</select>
<?php
}
**i have two table
" users" colmns(fname,lname,...as on ohther_infomation,hobbies datatype(int))
"info" columns (id (primary_key),hobbies(varchar 200)); in which i stored for hobbies name
In my case i am storing values in from (1,2,3,4) in hobbies (int) filled of users table which i matached them through join after time of fetch them,
in my info table i stored hobbies by their name (reading, writing,playing,gyming)
$row has our users selected hobbies (int)
$rows has list of our hobbies(varchar)
edit.php i need Dropdown value selected :==== And i am Doing Like this :--- (100% Working)**
<div class="form-control">
<label for="hobbies">Hobbies</label>
<select name="hobbies">
<?php
$query = "SELECT * FROM info";
$results = mysqli_query($connect, $query);
while ($rows = mysqli_fetch_array($results)) {
?>
<option <?php if ($rows['id'] == $row['hobbies']) { ?> selected="selected" <?php } ?> value='<?php echo $rows['id']; ?>'>
<?php echo $rows['hobbies']; ?>
</option>
<?php
}
?>
</select>
<span class="text-danger"><?php if (isset($err_hobbies)) echo $err_hobbies; ?></span>
</div>

Categories