'selected value from the listbox in php
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>
Solution 1:[1]
<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..
Solution 2:[2]
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.
Solution 3:[3]
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);
?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | bestprogrammerintheworld |
Solution 3 | Elshan |