I want the value which I select from the listbox and want to store in php variable because I want to use that variable for further use. Please help..
<form name="myform" method="post">
<?php
include('dbconnect.php');
db_connect();
$query = "SELECT * FROM test_customer"; //Write a query
$data = mysql_query($query); //Execute the query
?>
<select id="cust_name" name="mylist" onchange="selectedvalue()">
<?php
while($fetch_options = mysql_fetch_array($data)) { //Loop all the options retrieved from the query
?>
//Added Id for Options Element
<option id ="<?php echo $fetch_options['test_id']; ?>" value="<?php echo $fetch_options['cust_name']; ?>"><?php echo $fetch_options['cust_name']; ?></option><!--Echo out options-->
<?php
}
?>
</select>
</form>
<select id="cust_name" name="mylist" onchange="selectedvalue(this.value)">
access that values selectedvalue(val) in parameter
get that selected value and store it in input type= hidden as u need it for further use..
If you want to store into a php-variable, you must post the form. I've added a submit-button below. You also must speciy an action to where the form is submitted to: action="whatever.php"
<form name="myform" method="post" action="whatever.php"> <!-- added action here -->
<?php
include('dbconnect.php');
db_connect();
$query = "SELECT * FROM test_customer"; //Write a query
$data = mysql_query($query); //Execute the query
?>
<select id="cust_name" name="mylist" onchange="selectedvalue()">
<?php
while($fetch_options = mysql_fetch_array($data)) { //Loop all the options retrieved from the query
?>
//Added Id for Options Element
<option id ="<?php echo $fetch_options['test_id']; ?>" value="<?php echo $fetch_options['cust_name']; ?>"><?php echo $fetch_options['cust_name']; ?></option><!--Echo out options-->
<?php
}
?>
</select>
<!-- added submit button -->
<input type="submit" value="ok" />
</form>
Create a php-file called whatever.php and in this file, store the value into $cust_name by doing this:
<?php
$cust_name = $_POST['mylist']; //mylist is the name of the select-list
?>
If you want to post the form without having to reload the page, you must use something called ajax.
This works for me.Try this example.
public function getAptTypesBySelectedKy($valKy) {
$stmt = "SELECT ap_typ_ky, ap_typ_desc FROM apt_types WHERE ap_stat=1 order by ap_typ_desc";
try {
$con = DataHandler::connect();
$values = $con->prepare($stmt);
if ($values->execute()) {
echo '<select class="form-control" name="type" id="apt_types_slc">';
while ($row = $values->fetch(PDO::FETCH_ASSOC)) {
if ($valKy === $row['ap_typ_ky']) {
echo '<option value="' . $row['ap_typ_ky'] . '" selected>' . $row['ap_typ_desc'] . '</option>';
} else {
echo '<option value="' . $row['ap_typ_ky'] . '">' . $row['ap_typ_desc'] . '</option>';
}
}
echo '</select>';
}
} catch (PDOException $ex) {
echo 'Error on apartment types';
$error = $ex;
print_r('<pre>' . $ex->getCode() . '</pre>');
print_r('<pre>' . $ex->getMessage() . '</pre>');
}
}
In your html form page.
$__typKy = $_RA['typeky'] //this line get the selected value from database and set as parameter to the function getAptTypesBySelectedKy()
<?php
$__typKy;
if (isset($_RA)) {
$__typKy = $_RA['typeky'];// you need to find this key from database.
//this is the selected value key of `<select>`
}
$var = new DataHandler();
$var->getAptTypesBySelectedKy($__typKy);
?>
Related
I have populated dropdown list from the sql result in php and now i am trying to get the selected value to a php variable in the same page , but it is not working. Can you help. Below is the code.
<?php
$mid="mario";
$sql = "SELECT * FROM tbl_prdy" ;
$result = mysqli_query($conn,$sql);
echo "<select name='list'>";
while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
echo "<option value='" . $row['col_of_fac'] . "'>" . $row['col_of_fac'] . "
</option>";
}
echo "</select>";
$varsel = $_POST['list'];
echo "hai";
echo $varsel;
?>
$varsel = $_POST['list']; is not working.
you should
1- use a form
2- send the variables with method "POST" to the same file php
<?php
// display the errors
error_reporting(E_ALL);
ini_set("display_errors", 1);
?>
<?php
// when the form submitted
if(!empty($_POST['list'])){
echo $_POST['list'];
}
?>
<?php
// connection test
$conn=mysqli_connect("localhost","user","pass","db"); //replace the (user, pass, db) with your parameters
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
//if your connection succeded
$sql = "SELECT * FROM tbl_prdy" ;
$result = mysqli_query($conn,$sql);
?>
<form method="post" action="<?= $_SERVER['PHP_SELF']; ?>">
<select name="list">
<?php while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)): ?>
<option value="<?= $row['col_of_fac']; ?>"><?= $row['col_of_fac']; ?></option>
<?php endwhile; ?>
</select>
<input type="submit" value="valider">
</form>
if you want to print the value selected and not without form
then you have to use jquery
<?php
// display the errors
error_reporting(E_ALL);
ini_set("display_errors", 1);
?>
<?php
// when the form submitted
if(!empty($_POST['list'])){
echo $_POST['list'];
}
?>
<?php
// connection test
$conn=mysqli_connect("localhost","user","pass","db"); //replace the (user, pass, db) with your parameters
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
//if your connection succeded
$sql = "SELECT * FROM tbl_prdy" ;
$result = mysqli_query($conn,$sql);
?>
<select name="list">
<?php while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)): ?>
<option value="<?= $row['col_of_fac']; ?>"><?= $row['col_of_fac']; ?></option>
<?php endwhile; ?>
</select>
<p id="value-selected"></p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(function(){
$("select[name=list]").on("change", function () {
var valueSelected = $(this).val();
$("#value-selected").html(valueSelected);
});
});
</script>
$varsel = $_POST['list']; is not working. : it will work only when the form is submitted by the post method ,
(with current code select box is printed and you are fetching post without submitting the form)
The logic should be like
In Form Select box is made using your below code and user select and click on submit:
<?php
$mid="mario";
$sql = "SELECT * FROM tbl_prdy" ;
$result = mysqli_query($conn,$sql);
echo "<select name='list'>";
while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
echo "<option value='" . $row['col_of_fac'] . "'>" . $row['col_of_fac'] . "
</option>";
}
echo "</select>";
?>
After form submitted below code should execute and in Network console , in page headers you can see if the required data is transferred to the requested page
I am stuck with small problem.
when i change my drop down list the page is getting reloaded.
Can any one help me .
This code is used to retrieve userid, based on userid get all the application id form drop down and populate it into dropdown list.
If i select value in drop down i am storing selected value
Here is my code.
<?php
$selected = " ";
function get_options($select) {
global $wpdb;
$user_ID = get_current_user_id();
echo $user_ID;
$sql = $wpdb->get_col("select Application_ID from wp3_cteTest where $user_ID = userid");
echo $sql;
//print_r($keysql);
$options = "";
while (list($k, $v) = each($sql)) {
if ($select == $v) {
$options.='<option value="' . $v . '" selected>' . $v . '</option>';
} else {
$options.='<option value="' . $v . '">' . $v . '</option>';
}
}
return $options;
}
if (isset($_POST['applications'])) {
$selected = $_POST['applications'];
echo $selected;
}
?>
<html>
<head>
</head>
<body>
<form action="" method="POST">
<?php $default_state = 'please Select your id'; ?>
<select name="applications" onchange=this.form.submit()>
<option value=' ' ><?php echo $default_state ?></option>
<?php echo get_options($selected) ?>
</select>
</form>
</body>
</html>
I Tried removing form, if i remove form drop down doesn't work, I wont get selected value.
Then what is wrong??
Thanks in advance!
You've got onchange=this.form.submit() in your select tag, so when you change the option selected javascript is submitting the form.
Just change...
<select name="applications" onchange=this.form.submit()>
...to...
<select name="applications">
So if you just need the selected id without submit the form then replace this line:
<select name="applications" onchange=this.form.submit()>
with this :
<select name="applications" onchange="getval(this)">
and Add little script :
<script type="text/javascript">
function getval(sel) {
alert(sel.value);
}
</script>
I have select option. this option has multiple value from database. I want to update something from database, this value i want to update is exist on the select option I have.
this is my option code
$id = $_GET['update'];
$query = mysql_query("SELECT * FROM transaction where id = '$id'") or die ("could not search");
$count = mysql_num_rows($query);
while ($rows = mysql_fetch_array($query)) {
$id = $rows['id'];
$tranid = $rows['tranid'];
$trandate = $rows['trandate'];
$patientid = $rows['patientid'];
$transactiontype = $rows['transactiontype'];
$trandescription = $rows['trandescription'];
$tranquantity = $rows['tranquantity'];
$tranunitprice = $rows['tranunitprice'];
$tranamount =$rows['tranamount'];
$gettrandescription = $rows['trandescription'];
}
}
if (isset($_POST['selectmedicine'])) {
$gettrandescription=$_POST['medicineid'];
}
if (isset($_POST['selectroomquantity'])) {
$tranquantity=$_POST['quantity'];
}
?>
<script type="text/javascript">
$('#collapseone').collapseone({
toggle: true
});
<option value="<?php echo $trandescription; ?>" <?php if($trandescription==$gettrandescription){ echo "selected";} ?> ><?php echo $gettrandescription; ?></option>
<option value="<?php echo $tranquantity; ?>" <?php if($tranquantity==$tranquantity){ echo "selected";} ?> ><?php echo $tranquantity; ?></option>
this has value results, but i cant fetch this value to my existing select option.
If you want to "make that variable an array" as aldrin27 said, append [] to the name attribute of the select tag. The selected value of the option with name selectroomquantity will be available in your script as $_POST["selectroomquantity"] (this is the varible).
<select multiple name="selectroomquantity[]">
<option value="...">...</option>
</select>
It should only be necessary if multiple options can be selected however.
Also, there seems to be a typo:
<?php if($tranquantity==$tranquantity)
That check will always return true. It should probably be:
<?php if($tranquantity==$gettranquantity)
hi all i just got the code on how to fecth the value to dropdown. actually i made a wrong word. pre-selected is the right one, sorry for that. here;s the working code.
<select name="selectmedicine" class="form-control col-sm-4" id="medicinename">
<option id="0" style="width:100px"></option>
<?php
$medicine = mysql_query("SELECT * FROM medicine");
while ($row = mysql_fetch_array($medicine)) {
echo '<option id="' . $row['medicinename'] . '"';
echo ' value="' . $row['medicineid'] . '"';
if($row['medicinename'] == $trandescription) {
echo ' selected="selected"';
}
echo '>';
echo $row['medicinename'];
echo '</option>';
}
?>
</select>
thanks everyone, whos trying to help me on this. actually this is my five revised question sorry for that. and finally i got the right one.
I want to fetch data from dropdown list. Like if I choose employee id 40 from the dropdown list it will fetch the data from database of that employee and will shown in the textboxes.
this is my dropdown code. please help me how can i get the selected value.
<?php
$con=mysqli_connect("localhost","root","","hct_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<label>Select Employee ID</label>
<select class="form-control" name="employee_id">
<?php
$result = mysqli_query($con,"SELECT employee_id FROM employee order by employee_id");
while($row = mysqli_fetch_array($result))
echo "<option value='" . $row['employee_id'] . "'>" . $row['employee_id'] . "</option>";
?>
</select>
First of all give some id to you select option like this:
<select class="form-control" name="employee_id" id='employee'>
Add you textbox like this:
<input type='text' name='emp_name' id='emp_name' />
Than use jquery and ajax something like this:
$('#employee').change(function(){
var selected_id = $(this).val();
var data = {id:selected_id};
$.post('getemp_name.php',data,function(data){
$('#emp_name').val(data);
});
});
getemp_name.php
if(isset($_POST['id'])){
//fire query using this id and get the name of employee and echo it
echo $emp_name;
}
<?php
if(isset($_REQUEST['submit']))
{
$value=$_POST['employee_id'];
$query = mysql_query($con,"SELECT employee_name FROM employee where employee_id=$value");
$result=mysql_fetch_array($query);
$emp_name=$result['employee_name'];
}
?>
<form action="" method="post" name="form">
<label>Select Employee ID</label>
<select class="form-control" name="employee_id">
<?php $result = mysqli_query($con,"SELECT employee_id FROM employee order by employee_id");
while($row = mysqli_fetch_array($result))
echo "<option value='" . $row['employee_id'] . "'>" .$row['employee_id'] . "</option>";
?>
</select>
<input type="submit" name="submit" value="submit">
</form>
<input type="text" value="<?=$emp_name?>" name="emp_name"/>
check this code as your need
To get your selected value, you have to reach the $_GET or $_POST supertables after the user submits the form.
So, after the submit, get it as, if you POST:
<?php
$employee_id = $_POST['employee_id']; ?>
If you GET:
<?php
$employee_id = $_GET['employee_id']; ?>
apply below function on onChange
function myFunction(mySelect) {
var x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = "You selected: " + x;
}
example
That depends on what you want. If you want to use the selected ID in your query AFTER DOING A POST, you can use the following query:
"SELECT *
FROM `employee`
WHERE `employee`.`employee_id` = " . (int) $_POST['employee_id'] . "
LIMIT 1"
Assuming you are using the post method in your form.
You can easily learn how to fetch data from dropdown using this example by clicking Here This repository contains all the codes
I want to know the error in this code
The following code retrieves the names of the members of the database query in the
dropdownlist
But how do I know who you selected.... I want to send messages only to the members that selected form dropdown list
<?php
include ("connect.php");
$name = $_POST['sector_list'];
echo $name ;
?>
<form method="POST" action="" >
<input type="hidden" name="sector" value="sector_list">
<select name="sector_list" class="inputstandard">
<option size ="40" value="default">send to </option>
<?php
$result = mysql_query('select * from members ')
or die (mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option size ="40" value=" '. $row['MemberID'] . '" name="' . $row['MemberName']. '">' . $row['MemberName']. '</option>';
}
?>
</select>
</form>
I hope somebody can help me
This should do the trick.
<?php
$member_id = intval($_POST['sector_list']);
if($member_id == 0) {
// Default choice was selected
}
else {
$res = mysql_query("SELECT * FROM members WHERE MemberID = $member_id LIMIT 1");
if(mysql_num_rows($res) == 0) {
// Not a valid member
}
else {
// The member is in the database
}
}
?>
<form method="post" action="">
<input type="hidden" name="sector" value="sector_list">
<select name="sector_list" class="inputstandard">
<option value="0">send to</option>
<?php
$result = mysql_query('SELECT * from members') or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="' . $row['MemberID'] . '">' . $row['MemberName']. '</option>';
}
?>
</select>
</form>
To get an input to change when you select someone try this:
<select onchange="document.getElementById('text-input').value = this.value;">
<!-- Options here -->
</select>
<input type="text" id="text-input">