The options for the select menu are created from a database. I want to print the value of the selected option on the input. The code below only prints the first option value. I want to print them all. thanks.
<?php
$sql2 = "select * from add_meta";
$sonuc2= $conn->query($sql2);
if($sonuc2->num_rows>0){
while($kayitlar2 = $sonuc2->fetch_object()){
if($kayitlar2->isim!="Renk")
{
?>
<select id="selectid" name="<?php $kayitlar2->isim; ?>" class="sec" onchange="degergoster()" >
<option id="barkodd" value="" style="display:none;"><?php echo $kayitlar2->isim; ?> seçin</option>
<?php
$sql = "select * from add_barkod where kat_list in('$kayitlar2->isim')";
$sonuc= $conn->query($sql);
if($sonuc->num_rows>0){
while($kayitlar = $sonuc->fetch_object()){
?>
<option name="selectname1" value="<?php echo $kayitlar->ekle_hane;?>"><?php echo $kayitlar->ekle_isim; }} ?></option>
</select>
<?php
} }}
?>
<input id="e" name="varyantkod" class="sec">
Javascript code:
function degergoster() {
var selectkutu = document.getElementById("selectid");
var selectkutu_value = selectkutu.options[selectkutu.selectedIndex].value;
document.getElementById("e").value=selectkutu_value;
}
just get the value of the select object and add that to the value of the input
change
document.getElementById("selectid");
to
document.getElementById("selectid").value;
This should work
Related
I am trying to get the value of the selected option from my select. And I am trying to see it's output through a javascript echo. Here's what I've got so far. I am not getting the value
<form method="post" action="">
<select class="form-control" name="empSel" id="empSel">
<?php
$sql2 = "SELECT * FROM employee";
$result = mysql_query($sql2) or die("Couldn't execute sql2");
while ($row2 = mysql_fetch_assoc($result)) {
?>
<option value="<?php echo $row2['lastname'] ?>"><?=
/*$row2['user_surname']." ".*/
$row2['id']."-".$row2['lastname'] ?></option>
<?php
}
?>
</select>
<Label> Confirm</Label>
<div class="form-group col-md-6">
<input type="submit" class="btn btn-block btn-info"
name="submit"/>
</div>
</form>
<?php
if (isset($_POST['submit'])){
$userid = $_POST['empSel'];
echo '<script type="text/javascript"> alert('.$userid.')</script>';
$userid = preg_replace('/\D/', '', $userid);
$sql2 = "SELECT * FROM employee where id ='userid'";
$result = mysql_query($sql2) or die("Couldn't execute sql2");
while ($row = mysql_fetch_assoc($result)) {
echo '<script type="text/javascript"> alert("")</script>';
}
}
?>
An javascript alert doesn't pop out on this code. However, when I switch the value in the echo to a different variable an alert pops up. What does it mean? Do I properly get the value of my select and the page refreshed instantly that I didn't get to see it? Thanks
Edit:
An example of the option value would be 1-Lastname
And here's what I've tried.
<?php
if (isset($_POST['empSel'])){
$userid = $_POST['empSel'];
$userid = intval($userid);
echo '<script type="text/javascript"> alert('.$userid.')</script>';
}
?>
Now the javascript alert shows, but it echo 0. I think I am still not getting the value of my selected option
<option value="<?php echo $row2['id'] ?>">
This fixed it. In my previous code the option value was the surname...
I'm using a select form element to render multiple options and then show corresponding text from a database in the text area using an onchange event.
The value of the select option's are very long, so I'd like to pass the id, or name of the option, to my form target favorite.php, but I don't know how to do it.
HTML:
<div class="container">
<div class="had">
<form name="det" action="vispage.php" method="POST">
<label for="hadeath">Selct Title of the Hadeath</label>
<select name="header" onchange="document.getElementById('txt').innerHTML=this.value">
<?php foreach($hadeeth as $i) {?>
<option id="<?php echo $i['id']; ?>" value="<?php echo $i['short']; ?>"><?php echo $i['name']; ?></option>
<?php } ?>
</select><br><br>
<label for="a">tree :</label><br>
<textarea id="txt" name="tree" rows="15" cols="30"></textarea><br><br>
Add to Favorites </form>
</div>
</div>
PHP:
<?php
$title = "hadeeth";
include_once ("header.php");
include 'connect.php';
//CREATE THE QUERY
$query = "SELECT * FROM `search`";
//RUN THE QUERY
$result = mysqli_query($con,$query);
$hadeeth = array();
// RETRIEVE VALUES FROM RESULT
while($row = mysqli_fetch_assoc($result))
{
$hadeeth[$row['name']] = array(
'id'=>$row['id'],
'name'=>$row['name'],
'short'=>$row['short']
);
}
?>
I know this is a stupid question but i am new with AJAX and
i tried many of the code i got from internet but still not able to do this simple thing
so at last i am posting my question here
something is wrong and when i select option in first drop down list
nothing happens
database table name is sub_menu and its fields are as follows
id,sub_item_name,item_name,price
this is my script i put on additem.php file
<script>
function showSubItem(sel) {
var item_name = sel.options[sel.selectedIndex].value;
$("#subItemTr").html( "" );
if (item_name.length > 0 ) {
$.ajax({
type: "POST",
url: "subitem.php",
data: "item_name="+item_name,
cache: false,
success: function(html) {
$("#subItemTr").html( html );
}
});
}
}
</script>
and he is my both drop down menu both are in a table form
My master menu is still working and displaying all option from database so i am not posting that
part here
<tr>
<td width="251">Select Master Menu Name</td>
<td width="47">:</td>
<td width="342"><select name="iname" id="iname" class="form-control" onChange="showSubItem(this);">
<option selected="selected">Select Main Menu</option>
<?php
$abc = "select * from main_menu";
$lkg = mysql_query($abc);
while($ukg = mysql_fetch_row($lkg))
{
?>
<option><?php echo $ukg[1]; ?></option>
<?php } ?>
</select> </td>
</tr>
<tr>
<td width="251">Sub Item Name</td>
<td width="47">:</td>
<td width="342" id="subItemTr" ><select name="subname" id="subname" class="form-control">
<option selected="selected">Select Sub Item</option>
</select></td>
</tr>
And here is my subitem.php file code as follows
<?php
include 'config.php';
$item_name = ($_POST["item_name"] <> "") ? trim( addslashes($_POST["item_name"])) : "";
if ($country_id <> "" ) {
$sql = "SELECT * FROM sub_menu WHERE item_name = ".$item_name ." ORDER BY sub_item_name";
$count = mysql_num_rows( mysql_query($sql) );
if ($count > 0 ) {
$query = mysql_query($sql);
?>
<select name="sub_item">
<option value="">Please Select</option>
<?php while ($rs = mysql_fetch_array($query)) { ?>
<option><?php echo $rs["item_name"]; ?></option>
<?php } ?>
</select>
<?php
}
}
?>
This should
<option><?php echo $rs["item_name"]; ?></option>
to
<option value="<?php echo $rs["item_id"]; ?>"><?php echo $rs["item_name"]; ?></option>
I given $rs["item_id"]; whatever field you want in value fix it.
And also,
change this line also
Your selectbox option should have value attributes. Until your condition if (item_name.length > 0 ) { should be fail.
<option><?php echo $ukg[1]; ?></option>
Should be
<option value="<?php echo YOUR_VALUE ?>"><?php echo $ukg[1]; ?></option>
<option><?php echo $rs["item_name"]; ?></option>
Should be
<option value="<?php echo YOUR_VALUE ?>"><?php echo $rs["item_name"]; ?></option>
Also currently you are sending the object instead on value.
<select name="iname" id="iname" class="form-control" onChange="showSubItem(this);">
Should be
<select name="iname" id="iname" class="form-control" onChange="showSubItem(this.value);">
So you can access the selected value directly from your variable sel
var item_name = sel;
You not defined the $country_id in subitem.php file, Thats why code fails to execute inside if statement. You define something for $country_id or remove the condition if not necessary. Then it will work
Example:
<?php
include 'config.php';
$country_id = 'something';
$item_name = ($_POST["item_name"] <> "") ? trim( addslashes($_POST["item_name"])) : "";
if ($country_id <> "" ) {
$sql = "SELECT * FROM sub_menu WHERE item_name = ".$item_name ." ORDER BY sub_item_name";
$count = mysql_num_rows( mysql_query($sql) );
if ($count > 0 ) {
$query = mysql_query($sql);
?>
<select name="sub_item">
<option value="">Please Select</option>
<?php while ($rs = mysql_fetch_array($query)) { ?>
<option><?php echo $rs["item_name"]; ?></option>
<?php } ?>
</select>
<?php
}
}
?>
I need the code for getting the addr and pnum of the patient when I choose the pname in the combo box.
How can I do that?
<script>
function getVal() {
document.getElementById("text2").value = document.getElementById("model").value;
}
</script>
<body>
//code in opening and getting my addr and pnum in dbase
<?php
include('connect.php');
$pname=$_GET['tpname'];
$result = mysql_query("SELECT * FROM tblnpatient where pname='$pname'");
while($row = mysql_fetch_array($result))
{
$pnum=$row['pnum'];
$addr=$row['addr'];
}
?>
//code for choosing pname
<tr><td>Patient Name:
<div id="ma">
<select name="pname" class="textfields" id="model" style="width:180px;" onchange="getVal();">
<option id="0" >--Select Patient Name--</option>
<?php
$con=mysqli_connect("localhost","root","","dbnpatient");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$pnum=$_GET['pnum'];
$query = mysqli_query($con, "SELECT * FROM tblnpatient");
while($row = mysqli_fetch_array($query)){
$pnum = $row['pnum'];
$pname = $row['pname'];
?>
<option id="<?php echo $pnum; ?>" value="<?php echo $pname; ?>"><?php echo $pname; ?> </option>
<?php } ?>
</select>
//code for getting pname and addr
Address:<input type="text" name="ename" size="20" id="ma" value="<?php echo $addr ?>"/>
Name:<input type="text" name="ename" size="20" id="ma" value="<?php echo $pname ?>"/>
In your while loop add the following (if you have that column otherwise replace with something similar)
$paddress = $row['paddress'];
You can store the needed information by using the data attribute in your options
<option id="<?php echo $pnum; ?>" data-pname="<?php echo $pname; ?>" data-paddress="<?php echo $paddress; ?>" value="<?php echo $pname; ?>"><?php echo $pname; ?></option>
Then change your getVal() function
function getVal() {
var options = document.getElementById("model").options;
if (options.selectedIndex != -1) {
var addr = document.getElementById('paddress');
var name = document.getElementById('pname');
addr.value = options[options.selectedIndex].getAttribute('data-paddress');
name.value = options[options.selectedIndex].getAttribute('data-pname');
}
}
Now change the id's of the input fields for the address and the name to paddress and pname. It is important to know to never have duplicate id's
I hope that helped
I am creating a dropdown list in php. how can i put the selected item when someone selects an item.
my php code:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<select name="app" id="dropdown" value="" onchange="this.form.submit()" ><option>--select-app--</option>
<?php
$sql="select * from application";
$result=mysqli_query($con, $sql) or die("ereor selecting app ".mysqli_error($con));
while($row=mysqli_fetch_array($result))
{
$selected = $row['name'];
echo "<option id=". $row['id']."value = ".$row['id'].">".$row['name']."</option>";
}
echo "</select>";
?>
i want this: if I select an item it will show it as selected. how can I do this
You can do it in php like
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<select name="app" id="dropdown" value="" onchange="this.form.submit()" >
<option>--select-app--</option>
<?php
$sql="select * from application";
$result=mysqli_query($con, $sql) or die("ereor selecting app ".mysqli_error($con));
$selected_val = $_POST['app']; //Should be $_GET, $_POST, $_SESSION whatever your selected value is
while($row=mysqli_fetch_array($result))
{
if(trim($row['id']) == trim($selected_val)) //<== Change this line
$selected = 'selected="selected"';
else
$selected = '';
echo '<option id="'. $row['id'].'" value="'.$row['id'].'" '. $selected.'>'. $row['name'] .'</option>';
//^Change this line
}
echo "</select>";
?>
In jQuery you can do it like
$('#dropdown').val('<?php echo "My val"; //The value goes here ?>');
Assuming that you mean you want to retain the selection after the form is submitted, you can do this inside the while loop:
$selected = (isset($_POST['app']) && $_POST['app'] == $row['id'] ? 'selected' : '');
echo "<option id=".$row['id']." value = ".$row['id']." ".$selected.">".$row['name']."</option>";