Change textbox value using dropdown selected in php and mysql - php

I have texbox and dropdown list which is populated from mysql database.I want to change textbox value using dropdown list, without refreshing the page.
Here is my code and Thanks in Advance.
<select name="select" id="dropdownlist1">
<option id="0">-- Select the Company --</option>
<?php
require("dbcon.php");
$getallcompanies = mysql_query("SELECT * FROM ifcandetails6");
while($viewallcompanies = mysql_fetch_array($getallcompanies)){
?>
<option id="<?php echo $viewallcompanies['tcuid']; ?>"><?php echo $viewallcompanies['tcname'] ?></option>
<?php
}
?>
</select>
Here is my Input field code:
<input type="text" id="field1" value="<?php echo $viewallcompanies['tcname']?>" disabled/>

Use following
$(document).ready(function(){
$("#dropdownlist1").change(function(){
$("#field1").val($(this).val());
});
});
As you can do it on front side itself, you dont need to change in your PHP code. Add the following code on DOM ready.

Related

Select value from the dropdown in which the data are taken from database

I need to post the value from the drop down, in which the dropdown contains items which are retrieved from the database. To display the item of the table I use echo in the option. But then, I need to get that value of item selected to be updated in the database. As be seen below, I've tried the code which (surely) will not work. How is it possible to get the the value of selected item?
Your suggestion will be appreciated.
Thank you.
<select name="user" class="form-control" id="user">
<?php while ($hasil = mysqli_fetch_array($result2)){ ?>
<option name="user_name"><?php echo $hasil['user_name'] ?></option>
<option name="user_name" value="<?php echo $hasil['user_name'] ?>" hidden></option><?php }?>
</select>
<select name="user" class="form-control" id="user">
<?php while ($hasil = mysqli_fetch_array($result2)){ ?>
<option><?php echo $hasil['user_name'] ?></option>
<?php } ?>
</select>
This code solves your problem, Check at your side and let me know in case of any issue.
We need to give name only once in select tag and no need to add a extra option only for value.
You must fill your dropdown from base table after that you must check value or id from second table that stored in database. If id or value is equal you must set selected='selected' in your option element
Hope it is help you to resolve you problem
" >
" >
" >
*

Get the value from a listbox in php in the same page

I searched and searched the internet for help but I didn't get it so I'm posting here. I've got a select option thing in my code and I want to get the value of it so I can use it in a query, but I don't know how to get the value from the select option in the same page. So how can I get the value from this select? I'll give you the code to my select.
<select name="tamanho">
<?php
$sqltamanhos="SELECT * FROM detalhes_produtos WHERE cod_produto ='$codigo'";
$resultadoo=odbc_exec($ligaBD,$sqltamanhos);
while (odbc_fetch_row($resultadoo))
{
$tamanho = odbc_result($resultadoo,2);
?>
<option value ="<?php echo $tamanho; ?>"> <?php echo $tamanho; ?> </option>
<?php } ?>
you can get the value of select box via posting that value in the form(get/post method).
<form method="get" action="">
<select name="tamanho">
<?php
$sqltamanhos="SELECT * FROM detalhes_produtos WHERE cod_produto ='$codigo'";
$resultadoo=odbc_exec($ligaBD,$sqltamanhos);
while (odbc_fetch_row($resultadoo)){
$tamanho = odbc_result($resultadoo,2);?>
<option value ="<?php echo $tamanho; ?>"><?php echo $tamanho; ?> </option>
<?php }?>
</form>
and get the value via
$value=$_GET['tamanho'];
you can also check this get the value of <select> without submitting on the same page using php

Change the list of dropdown according to the selection of other dropdown

I have two dropdowns .i want second dropdown list shoul changed according to the value selected in first dropdown.
this is my first dropdown
Category :<select name="Category" id="a1_txtBox5" required="required">
<option value="select">select..</option>
<?php while($selectcategoryarray=mysql_fetch_array($selectcategory)) {
?>
<option value="<?php echo $selectcategoryarray[1];?>"><?php echo $selectcategoryarray[1];?></option>
<?php
}
?>
</select>
And here is my second dropdown:
<label style="margin-left:24px;">Subcategory :</label><select style="margin-right:35px;" name="subcategory" id="a1_txtBox3" required="required">
<option value="select"> select..</option>
<?php while($selectsubcategoryarray=mysql_fetch_array($selectsubcategory)) {
?>
<option value="<?php echo $selectsubcategoryarray[2];?>"><?php echo $selectsubcategoryarray[2];?></option>
<?php
}
?>
</select>
Please help.
Exactly you need to handle the Change Event for your first Select element and in the body of the event you need to send request to server for getting data of second Select element.
I recommend to use an ajax process to doing this.
And o this you should use jQuery for handling events and have ajax.

populating textbox from dropdown in php & mysql

I am new in this forum..Forgive me for any kind of mistake & help me.
I have a form with only two fields,first one textfield & next dropdown list.Now I want to show value in list from database based on textfield value from database.i.e If I type perfect username it will show me in dropdown the corresponding emailid(s) of that user.It will change after the username being changed.
Hope someone will help me in this matter.I was working a long time,but cant satisfied.Thanks in advance.This is the code what I have tried.But I want just the reverse.
`
function CBtoTB()
{document.getElementById("username").value=document.getElementById("usernameselect").value}
<?php
$result=mysql_query("select Username from users");
$options="";
while ($row=mysql_fetch_array($result)) {
$username=$row["Username"];
$options.="<OPTION VALUE=\"$username\">".$username.'</option>';
}
?>
<select name="usernameselect" id="usernameselect" onchange="CBtoTB()">
<option value="">< select user ><?php echo $options ?></option>
</select>
<input name="username" type="text" id="username" value="<?php echo $username ?>" size="25" readonly="readonly" />`
You option was writen wrongly please try this :
<select name="usernameselect" id="usernameselect" onchange="CBtoTB()">
<option value="">< select user ></option>
<?php echo $options ?>
</select>

Get the Selected value from the Drop down box in PHP

I am populating a Drop Down Box using the following code.
<select id="select_catalog">
<?php
$array_all_catalogs = $db->get_all_catalogs();
foreach($array_all_catalogs as $array_catalog){?>
<option value="<?= $array_catalog['catalog_key'] ?>"><?= array_catalog['catalog_name'] ?></option>
Now how can I get the selected option value using PHP (I have the code to get the selected item using Javascript and jQuery) because I want the selected value to perform a query in database.
Any help will be much appreciated. Thank you very much...
You need to set a name on the <select> tag like so:
<select name="select_catalog" id="select_catalog">
You can get it in php with this:
$_POST['select_catalog'];
Couldn't you just pass the a name attribute and wrap it in a form?
<form id="form" action="do_stuff.php" method="post">
<select id="select_catalog" name="select_catalog_query">
<?php <<<INSERT THE SELECT OPTION LOOP>>> ?>
</select>
</form>
And then look for $_POST['select_catalog_query'] ?
You have to give a name attribute on your <select /> element, and then use it from the $_POST or $_GET (depending on how you transmit data) arrays in PHP. Be sure to sanitize user input, though.
Posting it from my project.
<select name="parent" id="parent"><option value="0">None</option>
<?php
$select="select=selected";
$allparent=mysql_query("select * from tbl_page_content where parent='0'");
while($parent=mysql_fetch_array($allparent))
{?>
<option value="<?= $parent['id']; ?>" <?php if( $pageDetail['parent']==$parent['id'] ) { echo($select); }?>><?= $parent['name']; ?></option>
<?php
}
?></select>

Categories