I have a table which consists of 2 columns, ContactName and Usertype. I have a dropdown list which consists of data from ContactName. When I select any value in the dropdown, its Usertype should be shown according to what was selected. Please help me. I have done this much so far. When I select any value in the dropdown, the same value is shown but I want the user type.
<?php
if(!isset($_SESSION)) {
session_start();
}
$dingo=$_SESSION['dingo'];
$query11="Select ISO3,Notify,Dingoid from rahul_tbl_users where Dingoid=$dingo";
$query123=mysql_query($query11);
$query1234=mysql_fetch_array($query123);
$fetch=mysql_query("SELECT tdd.Dingoid,tc.Dingoid,tc.A_End,tbidd.OpportunityNumber, tbidd.Status,tbidd.Country,tbidd.OpportunityName,tbidd.Allocatedto,tbidd.Email,tbidd.Customer,tbidd.Country,tbidd.ContactName,tc.Usertype,tbidd.G1_OPPID
FROM scott123.rahul_tbl_users tdd inner join scott123.rahul_user_opps tc on
tdd.Dingoid=tc.Dingoid Inner Join scott123.rahul_tbl_opportunities tbidd
on tc.A_End=tbidd.OpportunityNumber
WHERE tc.Dingoid =$dingo");
$fetch_result=mysql_fetch_array($fetch);
?>
<form method="post" action="">
<?php
$SQLString="SELECT distinct(G1_OPPID),ContactName from rahul_tbl_opportunities where G1_OPPID IS NOT NULL and ContactName!='' ";
$result1 = mysql_query($SQLString);
$select_box='<select name="select1" id="select1" onchange="javascript:load_value(this.value);">';
$input="";
while($rows1 = mysql_fetch_array($result1)) {
$select_box .='<option id="user_name" value="'.$rows1["ContactName"].'">'.$rows1['ContactName'].'</option>';
}
$input ='<input type="text" name="test" id="test" value="" />';
echo $select_box."</select>";
echo $input;
?>
<input type="submit" name="submit_name11" value="Add Permission"/>
<input type="submit" name="submit_name12" value="Edit Permission"/>
</form>
What I gather is you just want the textbox to display what is currently selected in the select box?
Change your onchange event in the select box to be:
onchange="transferValue();"
And add this script tag:
<script type="text/javascript">
function transferValue(){
var selectBox = document.getElementById("select1");
var selectedOption = selectBox.options[selectBox.selectedIndex].value;
var textBox = document.getElementById("test");
textBox.value = selectedOption;
}
window.onload = function(){
transferValue();
};
</script>
Related
I have a form wherein I have a drop down which is being populated from the database also I have an input box right beneath it. I want to fetch the value of both the fields via Ajax using jQuery and insert them into the database.
Problem: Value of the text field is getting inserted successfully but the value of drop down is not getting inserted.
I know I will have to fetch the value of the drop down separately and then add it to the data in ajax but I am not able to figure how to do the same.
NOTE: COULD THE DOWN VOTERS BE KIND ENOUGH TO TELL ME WHY I WAS DOWNVOTED SO THAT I COULD IMPROVE UPON THINGS.
sub_category_add.php
<form method="post">
<select id="cat_sub_cat">
<?php
$data=mysqli_query($con,"SELECT * FROM category");
while($row=mysqli_fetch_array($data))
{
echo "<option value=".$row['cid'].">".$row['category']."</option>";
}
?>
</select><br><br>
<h3>Add Sub Categories</h3><br>
<input type="text" name="sub_cat"><br><br>
<input type="submit" name="submit" value="Insert" id="sub_cat_btn">
</form>
Ajax File:
$(document).on('click','#sub_cat_btn', function(event){
event.preventDefault();
$.ajax({
url:"sub_cat_add_back.php",
method:"post",
data:$('form').serialize(),
dataType:"html",
success:function(strMsg){
$("#cat_sub_msg").html(strMsg);
}
})
sub_cat_add_back.php
<?php
include "../includes/config.php";
$name=$_POST['sub_cat'];
$cid=$_POST['cat_sub_cat'];
$data=mysqli_query($con,"INSERT INTO subcategory (name,cid) VALUES ('$name','$cid')");
if($data=="true")
{
echo "Successfully Inserted";
}
else
{
echo "Error";
}
?>
Your problem is that the select tag does not have the Name attribute
<select id="cat_sub_cat" name="cat_sub_cat">
If you want to get values on click instead of form serialize. add id in input
<input type="text" name="sub_cat" id="custom">
$("#cat_sub_cat option:selected");
$("#custom").val();
and if you want to do that with form serialize then add name in your select
<select id="cat_sub_cat" name="your_select_name">
i have this html for that displays 2 dropdown. one is for category and second is for subcategory
<form action="a_insert_product.php" method="post">
<div class="module_content">
<fieldset>
<label>Category</label>
<select name="catname">
<?php
$sql = "SELECT * FROM category";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result))
{
$catname=$row["catname"];
$catid=$row["id"];
?>
<option value="<? echo $catname.'-'.$catid;?>"><? echo $catname;?></option>
<?}
}?>
</select>
</fieldset>
<fieldset>
<label>Subcategory</label>
<select name="subcatname">
<?php
$sql = "SELECT * FROM subcategory";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result))
{
$subcatname=$row["subcatname"];
$subcatid=$row["id"];
?>
<option value="<? echo $subcatname.'-'.$subcatid;?>"><? echo $subcatname;?></option>
<?}
}?>
</select>
</fieldset>
<fieldset>
<label>Product Name</label>
<textarea rows="2" name="prodname"></textarea>
</fieldset>
</div>
<footer>
<div class="submit_link">
<input type="submit" name="submit" value="Submit" class="alt_btn">
</div>
</footer>
</form>
Although it is working fine but i wish to display the subcategory according to the category that is selected in the category dropdown, i don't have much knowledge regarding javascript. can anyone plz tel how it can be done
You can do it using AJAX, for example, assign an "id" attribute to both your catname and subcatname dropdowns:
<select name="catname" id="category-dropdown">
<select name="subcatname" id="subcategory-dropdown">
And then use some AJAX magic, for example:
<script type="text/javascript">
$("#category-dropdown").change(function() {
var selection_value = $(this).val();
$.post("subcategories.php", { category: selection_value },
function(result){
$.each(result, function(index, subcat) {
var option_html = "<option>" + subcat.name + "</option>";
$("#subcategory-dropdown").append(option_html);
}
},
"json");
});
</script>
Please note that this example uses jQuery, so you will have to add it to your page before using. Also, you will need to create a PHP page that will provide you with the response for subcategories, in this case I have used JSON, which is fairly easy to use.
I would do the following:
On the first dropdown, you'd have an on change handler which checks for what is selected in the dropdown. I would link that to an ajax call which then brings in the correct data to allow you to populate the second dropdown.
Google or search on here "JQuery on change" and "JQuery ajax" - there's tons of resources out there.
This is a different approach to same ends
I'm trying to create an "editable" drop down list. I have a drop down list populated from a table "city" in my db, it works perfect but now I want user to be able to add a new value to the list if it doesn't exist there (and I want to save this value, so next time it appears in the drop down list).
Any ideas?
Thank you
<select name="city">
<?php
// connecting to DB
$con= mysqli_connect('localhost','root','1234','e-sage');
// enable hebrew input
mysqli_set_charset($con,'utf8');
// read the city table from DB
$sql = mysqli_query($con,"SELECT CityName FROM city");
while ($row = mysqli_fetch_array($sql)){
// creating temporary variable for each row
$city1=$row["CityName"];
//assigning a value to each option in dropdown list
echo "<option value=\"$city1\"> $city1 </option>";
}
?>
</select>
You can provide a 'other' selection in the dropdown. If user selects that then display a new textbox from which you can insert in the database.
Then try this:
Lets agree that you already got all the cities in array OK?
<select name="city">
<?PHP
foreach($cities as $city)
{
echo '<option value="'.$city.'">'.$city.'</option>';
}
?>
</select>
<form method="post">
<label for="new_city">New city</label>
<input id="new_city" name="new_city" placeholder="Enter new city name" />
<input type="button" id="addCity" value="Insert city" />
</form>
<script>
$(document).on('click','#addCity',function()
{
var new_city=$("#new_city").val();
var e=0;
$("[name='city'] option").each(function(i,item)
{
var check_city=$(item).val();
if(check_city==new_city)
{
e++;
}
else
{
}
});
if(e>0)
{
alert('The same');
}
else
{
/*Submit the form*/
alert('This city is fresh and new');
}
});
</script>
So the script is checking if the value is already assigned to option. If not - allows to submit the form.
Check this one: http://jsfiddle.net/n47N2/1/
use datalist tag
link to w3School tutorial
I'm working right now on how I can pass the value of <input id="sub" name="sub"> that has a value of first 3 letters of dropdown option text. I need to pass or post the value of <input id="sub" name="sub"> to process my query below.
When I choose for example the ITEquipment in dropdown, the <input name="sub"> get the first 3 letters of dropdown option text. So now it has the value of ITE, that ITE what I need in my query. In query for example, $sub=$POST["sub"] is equal to ITE so I used LIKE function, LIKE '$sub-__' . Hope you understand what I'm trying to do.
How I can do that without using submit form?
Here's my example process of what I'm working http://jsfiddle.net/xqGLS/1/
Help please?
<?php
$mysqli = new mysqli("localhost", "root", "", "2015");
$resultcode = $mysqli->query("SELECT category, id, maincode FROM category GROUP BY id ORDER BY maincode");
$code = '';
while($row = $resultcode->fetch_assoc())
{
$code .= '<option value = "'.$row['maincode'].'">'.$row['category'].'</option>';
}
?>
<form action="<?php $_SERVER['PHP_SELF']?>" method="POST">
<br/>
Category
<select name="maincode" style="text-transform:uppercase;" onchange = "GetChangedValue(this);">
<option value=""></option>
<?php echo $code; ?>
</select>
</br>
Sub Code
<input type="" name="sub1code" id="sub1code" value="" readonly style="width:45px;text-transform:uppercase;">
<script>
$('[name="maincode"]').change(function() {
$('[name="sub1code"]').val($(this).val());
var input = $('[name="sub"],[name="sub1"]'),
input1 = $('[name="sub"]'),
input2 = $('[name="sub1"]'),
input3 = $('[name="equal"]');
input.change(function () {
input3.val(input1.val() + input2.val());
});
});
</script>
<script>
function GetChangedValue(e) {
var value = e.options[e.selectedIndex].text;
var elem = document.getElementById("sub"); elem.value = value.substring(0,3);
}
</script>
<input name="sub" id="sub" value="" style="width:35px;text-transform:uppercase;" readonly>
--My Query--
<input id="sub1" name="sub1" style="width:35px;text-transform:uppercase;" value='
<?php
$result2 = $mysqli->query("SELECT * FROM code
WHERE sub1code LIKE 'SUP-___' ORDER BY sub1code");
while($row = $result2->fetch_assoc())
{
$value = $row['sub1code'];
}
$first = substr($value, 0, 4);
echo $first;
$last = substr($value, -3);
$i="0";
while($i<=$last)
{
$i++;
}
$value2=strlen($i);
echo $first;
if($value2==1)
{
echo "00".$i;
}
elseif($value2==2)
{
echo "0".$i;
}
else
{
echo $i;
}
?>'>
<input id="equal" name="equal" value="" style="width:60px;text-transform:uppercase;" type="hidden">
<input type="submit" name="">
</form>
HEAVILY EDITED
$(document).ready(function(){
$("#dd").change(function(){
var v1 = $(this).val().substring(0,3);
$.ajax({
type : 'get',
url : 'call.php',
data : {varReq:v1}
}).done(function(returnResult){
alert(returnResult);
});
});
});
call.php would contain your php/mysql script. To get the value passed through there should be via $_GET or $_POST. But you have to include the proper type in your jquery's ajax too.
So in my example, What I have on my php, just a dummy one.
<?php
if(isset($_GET['varReq']))
{
if($_GET['varReq'] == 'ABC') echo "AAAA";
else echo "BBBB";
}
?>
It pass this html element...
<h1>Example</h1><p>BBBB</p>
Then pop up message (alert) will be called showing the return value.
I have create one form which contains Test code,Test name,Test parameter are Text box & Instrument code is drop down list & also contain add another button on click of add another button text box for new Test parameter & drop down list for new Instrument code will be generated i want to save data of that newly generated Test parameter text box data & newly generated Instrument code drop down list data for single Test code..
I have create but it can not save dynamically generated new multiple text box data & generated Instrument code drop down list data please guide me.
Here is my PHP code:
<?php
global $Hostname;
global $Username;
global $Password;
global $Database_name;
function getConnection()
{
$Hostname = "localhost";
$Username ="root";
$Password ="";
$Database_name="labdata";
$oMysqli = new mysqli($Hostname,$Username,$Password,$Database_name);
return($oMysqli);
}
include_once "dTestCreation.php";
if(isset($_POST['submit']))
{
$Testcode = $_POST['testcode'];
$TestName = $_POST['testname'];
$Testparameter = $_POST['testparameters'];
$Instrumentcode = $_POST['instrument_code'];
$InsertQuery = "INSERT INTO demo_test(testcode,testname) VALUES('$Testcode','$TestName')"; //query for insert data into table
$oMysqli=getConnection(); //establish connection
$oMysqli->query($InsertQuery);
$InsertQuery1 = "INSERT INTO test_table(testcode,testparameters,instrument_code) VALUES('$Testcode','$Testparameter','$Instrumentcode')";
$oMysqli=getConnection(); //establish connection
$oMysqli->query($InsertQuery1);
}
?>
<html>
<head>
<title>TestData</title>
<script type="text/javascript">
function create_row() //create function create_row
{
var newtr=document.createElement("tr"); //variable for tr
var newtd=document.createElement("td"); //variable for td
var newtd1=document.createElement("td"); //variable f
var output="<input type=\"text\" name=\"testparameters[]\">";
/*var output1="<select name=\"instrument_name\"><option value='$tResult[$kkk]'>tResult[$kkk]</option></select>";*/
var output1="<select id=\"instrument_code\"><?php include_once "dTestCreation.php"; //include file dTestCreation.php
$tResult = getIname();
for($kkk=0;$kkk<count($tResult);$kkk++)
{
echo "<option value=".$tResult[$kkk].">".$tResult[$kkk]."</option>";
}//display values of instrument into list.
?></select>";
newtd.innerHTML=output; //display first td
newtd1.innerHTML=output1; //display second td
newtr.appendChild(newtd); //increment no of rows.
newtr.appendChild(newtd1);
document.getElementById("table1body").appendChild(newtr);
}
</script>
</head>
<body>
<form name="testdetails" method="post" target="_self" action="<?php $_PHP_SELF ?>">
<label for='Testcode'>Testcode</label>
<input type="text" name="testcode"></input>
<label for='TestName'>TestName</label>
<input type="text" name="testname"></input><br></br>
<label for='Testparameter'>Testparameter</label>
<input type="text" name="testparameters"></input>
<label for='Instrumentcode'>Instrumentcode</label>
<select name="instrument_code">
<?php
$tResult = getIname(); for($kkk=0;$kkk<count($tResult);$kkk++) //loop for incremenet code hereted value display in listbox.
{
echo "<option value=".$tResult[$kkk].">".$tResult[$kkk]."</option>";
}`enter code here`
?>
</select>
<table>
<tbody id="table1body">
<tr>
<!--<td><input type="textfield" name="testparameters"></td>-->
<td> <input type="button" name="button" value="Add Test Parameter" onclick="create_row()"> <!--call function create_row() for onclik add new row-->
</tr>
</tbody>
</table>
<input type="submit" name="submit" value="submit"></input>
</form>
</body>
</html>
1) You missed name attribute into your automatically generated dropdown list so without name attribute you will not get data into php script.
2) Your newly generated text box name = testparameters[] which array so you can't get value just using $_POST['testparameters']. TO get value, you have to loop through array.
foreach($_POST['testparameters'] as $value) {
// here , you get dynamically added texbox values.
}
Again try loop also but still value cant be stored into cant not count drop down list value &
& testparameters textbox save Array keyword
what i Try Here is My code:
<?php
include_once "dTestCreation.php";
if(isset($_POST['submit'])) //when clikc on submit button
{
$Testcode = $_POST['testcode'];
$TestName = $_POST['testname'];
$Testparameter = $_POST['testparameters'];
$Instrumentcode = $_POST['instrument_code'];
$InsertQuery = "INSERT INTO demo_test(testcode,testname) VALUES('$Testcode','$TestName')"; //query for insert data into table
$oMysqli=getConnection(); //establish connection
$oMysqli->query($InsertQuery);
//print_r($InsertQuery);exit();
$length = count($_POST['testparameters']);
for($i=0;$i<$length;$i++)
{
//echo $_POST['testparameters'][$i];
$a = $_POST['testparameters'][$i];
$na = array('testparameters' => $a['testparameters']);
foreach($na as $k => $v)
{
$na[$k] = mysql_real_escape_string($v);
}
$TestParameters = $na['testparameters'];
//$TestParameters = $_POST['testparameters'][$i];
$InsertQuery1 = "INSERT INTO test_table(testcode,testparameters,instrument_code) VALUES('$Testcode','$Testparameter','$Instrumentcode')"; //query for insert data into table
$oMysqli=getConnection(); //establish connection
$oMysqli->query($InsertQuery1);
}
}
?>
<html>
<head>
<title>TestData</title>
<script type="text/javascript">
function create_row() //create function create_row
{
var newtr=document.createElement("tr"); //variable for tr
var newtd=document.createElement("td"); //variable for td
var newtd1=document.createElement("td"); //variable f
var output="<input type=\"text\" name=\"testparameters[]\">";
/*var output1="<select name=\"instrument_name\"><option value='$tResult[$kkk]'>tResult[$kkk]</option></select>";*/
var output1="<select id=\"instrument_code\" name=\"instrument_code\"><?php include_once "dTestCreation.php"; //include file dTestCreation.php
$tResult = getIname();
for($kkk=0;$kkk<count($tResult);$kkk++)
{
echo "<option value=".$tResult[$kkk].">".$tResult[$kkk]."</option>";
}//display values of instrument into list.
?></select>";
newtd.innerHTML=output; //display first td
newtd1.innerHTML=output1; //display second td
newtr.appendChild(newtd); //increment no of rows.
newtr.appendChild(newtd1);
document.getElementById("table1body").appendChild(newtr);
}
</script>
</head>
<body>
<form name="testdetails" method="post" target="_self" action="<?php $_PHP_SELF ?>">
<label for='Testcode'>Testcode</label>
<input type="text" name="testcode"></input>
<label for='TestName'>TestName</label>
<input type="text" name="testname"></input><br></br>
<label for='Testparameter'>Testparameter</label>
<input type="text" name="testparameters"></input>
<label for='Instrumentcode'>Instrumentcode</label>
<select name="instrument_code">
<?php
$tResult = getIname(); //call function getIname() included in file dTestCreation.php
for($kkk=0;$kkk<count($tResult);$kkk++) //loop for incremented value display in listbox.
{
echo "<option value=".$tResult[$kkk].">".$tResult[$kkk]."</option>"; //Instrument values display in listbox
}
?>
</select>
<table>
<tbody id="table1body">
<tr>
<!--<td><input type="textfield" name="testparameters"></td>-->
<td> <input type="button" name="button" value="Add Test Parameter" onclick="create_row()"> <!--call function create_row() for onclik add new row-->
</tr>
</tbody>
</table>
<input type="submit" name="submit" value="submit"></input>
</form>
</body>
</html>