What I want is every time I have change the value from combobox (value from combobox is from database ) the input type will change too
here
<div class="col-md-12" >
<label>Charge</label>
<select id="name" name="name" class="form-control">
<?php
while ($reserve=mysqli_fetch_array($charge)) { ?>
<option value=" <?php echo $reserve['name']?>">
<?php echo $reserve['name']; ?>
</option><?php } ?>
</select>
</div>
<div class="col-md-12">
<br>
<label>Price</label>
<input type="number" class="form-control" id="Price" name="Price" disabled>
</div>
on database the name has a corresonding price for example chair has 200 price
what I want is every time I changed the combobox the value at inputtype will change too
I am not sure why you use ajax if you want to show option value . please check this code .
$('#name').change(function () {
var option_value = $(this).val();
$('#Price').val(option_value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<select id="name" name="name">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<div class="col-md-12">
<br>
<label>Price</label>
<input type="number" class="form-control" id="Price" name="Price" disabled>
</div>
</form>
so update select code using your this code
<select id="name" name="name" class="form-control">
<?php
while ($reserve=mysqli_fetch_array($charge)) { ?>
<option value=" <?php echo $reserve['name']?>">
<?php echo $reserve['name']; ?>
</option><?php } ?>
</select>
You need to add Jquery code to accomplish this task.
Your Html :
<div class="col-md-12" >
<label>Charge</label>
<select id="name" name="name" id="name" class="form-control">
<?php while ($reserve=mysqli_fetch_array($charge)) { ?>
<option value=" <?php echo $reserve['name']?>">
<?php echo $reserve['name']; ?>
</option><?php } ?>
</select>
</div>
<div class="col-md-12"><br>
<label>Price</label>
<input type="number" class="form-control" id="Price" name="Price" disabled>
</div>
Jquery Code :
$( document ).ready(function() {
$("#name").change(function () {
var value = this.value;
$('#Price').val(value);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<select id="name" name="name" class="form-control" onchange="callAjax(this)">
<?php
while ($reserve=mysqli_fetch_array($charge)) { ?>
<option value=" <?php echo $reserve['name']?>">
<?php echo $reserve['name']; ?>
</option><?php } ?>
</select>
<script>
function callAjax(val){
var selectedVal = val.value;
alert(selectedVal+" "+val)
$.ajax({
url: "<some_Url>", // Use PHP url from where you can get Data from Database
data: selectedVal,
success: function(result){
$("#Price").val(result);
}});
}
</script>
Related
I have a select field and i am using my option value as a variable for some further work but i would like to create a second variable for the name of my option to use elsewhere.
This is my form
<form action="second.php" method="post" class="form">
Client:<select class="default" id="client" name="client" required>
<option value="" selected>Select Client</option>
<option value="clienta" name"Client A">Client A</option>
<option value="clientb" name "Client B">Client B</option>
</select>
</form>
In my second.php file i am getting the value for client like this:
$client = $_POST["client"];
Im not sure how to get the name for client instead of value.
i have tried:
$client_name = $_POST["client.name"];
$client_name = $_POST["client[name]"];
$client_name = $_POST["client(name)"];
But none of these work, they all come back blank when i want to return the name of the option e.g.
"Client A" instead of "clienta"
Does anyone know how i could get to the name field instead of the value from my select?
If you Need Both value and Name. the best way to archive is by combining them in value
<?php
if (isset($_POST['client']))
{
$pair = explode("/", $_POST["client"]);
$name = $pair[0];
$value = $pair[1];
print "Key: $name<br />";
print "Value: $value<br />";
}
?>
<form method="post" action="" name="form">
<select name="client">
<option value="clienta/Client A">Client A</option>
<option value="clientb/Client B">Client B</option>
</select>
<input name="submit" type="submit">
</form>
You don't need add attribute name <option> element:
<form action="second.php" method="post" class="form">
Client:<select multiple="multiple" class="default" id="client" name="client[]" required>
<option value="" selected>Select Client</option>
<option value="ClientA">Client A</option>
<option value="ClientB">Client B</option>
</select>
</form>
In php:
$clients = $_POST['client']; // will be array with selected options
Use Like Below Code
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<form class="form-horizontal" action="#">
<div class="form-group">
<label class="control-label col-sm-2" for="client">Client:</label>
<div class="col-sm-6">
<select class="form-control" id="client" name="client" required>
<option value="" selected>Select Client</option>
<option value="clienta" name="Client A">Client A</option>
<option value="clientb" name="Client B">Client B</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="button" class="btn btn-default" id="submit_btn" name="submit_btn" value="Submit">
</div>
</div>
</form>
</body>
</html>
<script>
$('#submit_btn').on('click', function(){
var option_value = $('#client').val();
var option_name = $('#client').find('option:selected').attr('name');
$.ajax({
type:"post",
url:"second.php",
data:"option_value="+option_value+"&option_name="+option_name,
success: function(data){
console.log(data);
}
});
});
</script>
second.php
<?php
echo $option_value = $_POST['option_value'];
echo $option_name = $_POST['option_name'];
?>
<form action="second.php" method="post" class="form">
Client:<select class="default" id="client" data-name="client" required>
<option value="" selected>Select Client</option>
<option value="clienta" data-name="Client A">Client A</option>
<option value="clientb" data-name="Client B">Client B</option>
<input type="button" id ="button" value="send">
$(document).ready(function(){
$('#button').click(function(){
$.ajax({
type: 'POST',
url: 'second.php',
data: {'clinet_val':$('#client').val(),'clinet_name':$('#client').find(':selected').data('name')},
success:function(response)
{
console.log(response);
}
});
});
});
I am trying to make a dropdown list and take three parameters from the user which will be stored in a php file for later use. My current code is :
<body>
<header>
<form action="parameter.php" method="post">
<label class="heading">First</label>
<select name="First">
<option value="First-1">First-1</option>
<option value="First-2">First-2</option>
</select>
<input type="submit">
</form>
<form action="parameter.php" method="post">
<label class="heading">Second</label>
<select name="Second">
<option value="Second-1">Second-1</option>
<option value="Second-2">Second-2</option>
</select>
<input type="submit">
</form>
<form action="parameter.php" method="post">
<label class="heading">Third</label>
<select name="Third">
<option value="Third-1">Third-1</option>
<option value="Third-2">Third-2</option>
<input type="submit">
</select>
</form>
</header>
</body>
Here, the problem is, I don't want submit button for each parameter, instead there should be only one button for all the parameters. I have tried several things but none is working.
My php file
<?php
echo $_POST['First'];
echo $_POST['Second'];
echo $_POST['Third'];
?>
Where am I going wrong?
EDIT : Corrected the php code
Just wrap all your selects under one form instead of 3 different forms.
Make sure that the input submit is not inside the select tag.
Also, note that your php code is trying to echo parameters that don't exist in the html you shared. The name of your select will be the parameter name.
<form action="parameter.php" method="post">
<label class="heading">First</label>
<select name="First" >
<option value="First-1">First-1</option>
<option value="First-2">First-2</option>
</select>
<label class="heading">Second</label>
<select name="Second" >
<option value="Second-1">Second-1</option>
<option value="Second-2">Second-2</option>
</select>
<label class="heading">Third</label>
<select name="Third" >
<option value="Third-1">Third-1</option>
<option value="Third-2">Third-2</option>
</select>
<input type="submit">
</form>
PHP
<?php
echo $_POST['First'];
echo $_POST['Second'];
echo $_POST['Third'];
?>
you can add all there in one form
<form action="parameter.php" method="post">
<label class="heading">First</label>
<select name="First" >
<option value="First-1">First-1</option>
<option value="First-2">First-2</option>
</select>
<label class="heading">Second</label>
<select name="Second" >
<option value="Second-1">Second-1</option>
<option value="Second-2">Second-2</option>
</select>
<label class="heading">Third</label>
<select name="Third" >
<option value="Third-1">Third-1</option>
<option value="Third-2">Third-2</option>
<input type="submit">
</select>
<input type="submit" name="submit" value="submit">
</form>
in your code you have missed button "name".
parameter.php code should be this.
if(isset($_POST['submit'])){
echo $First = $_POST['First'];
echo $Second = $_POST['Second'];
echo $Third = $_POST['Third'];
}
<form action="parameter.php" method="post" id="frm">
<label class="heading">First</label>
<select name="First" >
<option value="First-1">First-1</option>
<option value="First-2">First-2</option>
</select>
<label class="heading">Second</label>
<select name="Second" >
<option value="Second-1">Second-1</option>
<option value="Second-2">Second-2</option>
</select>
<label class="heading">Third</label>
<select name="Third" >
<option value="Third-1">Third-1</option>
<option value="Third-2">Third-2</option>
</select>
<input type="submit">
</form>
JQuery AJAX
<script>
$('#frm').on('submit', function(e){
e.preventDefault();
$.ajax({
url : $(this).attr('action'),
type: "POST",
dataType: "json",
data : $(this).serialize(),
success: function(data)
{
console.log(data)
}
});
});
</script>
PHP
<?php
echo $_POST['First'];
echo $_POST['Second'];
echo $_POST['Third'];
?>
Here is my code
<div class="form-group">
<label for="salary" class="control-label">Department:*
</label>
<select class="form-control" id="selDepartment"
name="selDepartment" autofocus="autofocus">
<option value="-1" onmouseout="" ="">Select
Department</option>
<?php
$query="SELECT * FROM department_master";
$results=mysqli_query($con,$query);
foreach ($results as $dm_department) {
?>
<option value="<?php echo $dm_department["dm_department"];?>";?>
<?php echo $a=$dm_department["dm_department"];?></option>
<?php
}
?>
</select>
</div>
<div class="form-group">
<label for="salary" class="control-label">Designation:*
</label>
<select class="form-control" id="selDesignation"
name="selDesignation" autofocus="autofocus">
<option value="-1" onmouseout="" ="">Select
Designation</option>
<?php
$query="SELECT * FROM designation_master";
$results=mysqli_query($con,$query);
foreach ($results as $dm_designation) {
?>
<option value="<?php echo $dm_designation["dm_designation"];?>";?>
<?php echo $dm_designation["dm_designation"];?></option>
<?php
}
?>
</select>
</div>
How to select only particular designations from the department and here is my code.
Please include JS library before script
<select class="form-control" id="selDepartment" name="selDepartment" autofocus="autofocus" onchange='xyz(this.value)'>
<script>
function xyz(temp){
$.ajax({
type:'post',
url: "abc.php",
data: "department="+temp
success: function(result){
$("#selDesignation").html(result);
}});
</script>
In abc.php
$department=$_POST['department'];
$html="<option value="-1" onmouseout="" ="">Select
Designation</option>";
$query="SELECT * FROM designation_master where department=$department";
$results=mysqli_query($con,$query);
foreach ($results as $dm_designation) {
$html=$html+" <option value=$dm_designation['dm_designation']> $dm_designation['dm_designation']</option>';
}
echo $html;
i have a problem in dependent dropdown and i am not professional in this issue
so the problem is when i choose the first one the another one doesnt get any value from database and i didnt know the solution
<?php
include 'header.php';
require 'connection.php';
?>
<div class="container">
<form id="contact" action="add_line.php" method="post">
<center> <h3>Add Line</h3></center>
<fieldset>
<?php
require 'connection.php';
$query = "select * from agents";
$result = mysqli_query($conn,$query);
?>
<div class="select">
<select class="agents-name " name="agents-name" autofocus tabindex="1">
<option selected="selected">--Select agent--</option>
<?php while ($row = mysqli_fetch_assoc($result)): ?>
<option value="<?php $row['id'];?>"><?php echo $row['name'];?></option>
<?php endwhile;?>
</select>
</div>
<div class="select" >
<select tabindex="1" name="sp_choosen" class="sp_choosen"
onChange="getState( this.value );" tabindex="2">
<option selected="selected">--Select service provider--</option>
<option value="CELLCOM">CELLCOM</option>
<option value="HoTMobile">HoTMobile</option>
<option value="Orange">Orange</option>
<option value="Pelphone">Pelphone</option>
<option value="Golan">Golan</option>
<option value="019">019</option>
</select>
</div>
<div class="select">
<select id="packet_select" name="packet_chossen" tabindex="3">
<option selected="selected">--Select packet--</option>
</select>
</div>
</fieldset>
<fieldset>
<input placeholder="customer name" type="text" tabindex="4"
name="customer_name" required >
</fieldset>
<fieldset>
<input placeholder="SIM_SERIAL" type="tel" tabindex="5" name="sim_serial"
required >
</fieldset>
<fieldset>
<input placeholder="phone_number" type="tel" tabindex="6" name="number"
required >
</fieldset>
<fieldset>
<label></label>
</fieldset>
<fieldset>
<button name="submit" type="submit" id="contact-submit" tabindex="7" >Add
Available line</button>
</fieldset>
</form>
</div>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
function getState(val) {
$.ajax({
type: "POST",
url: "dropdown_add_line.php",
data:'sp='+val,
success: function(data){
$("#packet_select").html(data);
}
});
}
</script>
<?php
include 'footer.php';
?>
and this is the dropdown_add_line.php :
<?php
require_once("connectione.php");
$db_handle = new DBController();
if(!empty($_POST["sp"])) {
$sp=$_POST['sp'];
$query ="SELECT * FROM packets p WHERE sp LIKE '%$sp%'";
$results = $db_handle->runQuery($query);
?>
<option value="">--Select service provider--</option>
<?php
foreach($results as $packets) {
?>
<option value= "<?php echo $packets["id"]; ?>" ><?php echo $packets["packet"]; ?></option>
<?php
}
}
?>
and the table name is "packets" and the columns is "id","sp","packet"
Here is my code,
This is my form part, this is basically divided into
two main actions using jquery,
1) First action :, Based on Selection
from :'Academic Year' selection, 'Course' selection & 'Class'
selection , i used jquery ajax post method to load data from DB to All
5 set of IDs. .... Hey this part work Fine..
2) [my probs comes here
-->], Second Action :, when i select anyone of IDs, it shud show or
display new value of text box instead of default value, based on data
from DB by using similar ajax post method
========================================================================
<form name="aone" id="aone" action="mattendance.php" method="POST" enctype="multipart/form-data">
<div> <div style='padding-top:20px;'>
Academic Year :
<select name="academic" id="academic">
<option value="0"> --- Select ---- </option>
<option value="1990-91">1990-91 </option>
<option value="1991-92">1991-92 </option>
<option value="1992-93">1992-93 </option>
<option value="1993-94">1993-94 </option>
<option value="1994-95">1994-95 </option>
<option value="1995-96">1995-96 </option>
<option value="1996-97">1996-97 </option>
<option value="1997-98">1997-98 </option>
<option value="1998-99">1998-99 </option>
<option value="1999-00">1999-00 </option>
<option value="2000-01">2000-01 </option>
<option value="2001-02">2001-02 </option>
<option value="2002-03">2002-03 </option>
<option value="2003-04">2003-04 </option>
<option value="2004-05">2004-05 </option>
<option value="2005-06">2005-06 </option>
<option value="2006-07">2006-07 </option>
<option value="2007-08">2007-08 </option>
<option value="2008-09">2008-09 </option>
<option value="2009-10">2009-10 </option>
<option value="2010-11">2010-11 </option>
<option value="2011-12">2011-12 </option>
<option value="2012-13">2012-13 </option>
<option value="2013-14">2013-14 </option>
<option value="2014-15">2014-15 </option>
<option value="2015-16">2015-16 </option>
<option value="2016-17">2016-17 </option>
<option value="2017-18">2017-18 </option>
<option value="2018-19">2018-19 </option>
<option value="2019-20">2019-20 </option>
</select>
</div>
<div style='padding-top:15px;'>
<label>Select Course : </label>
<input type="radio" name="course" class="course" value="SDC">SDC
<input type="radio" name="course" class="course" value="GDC" checked="defaultChecked" />GDC
</div>
<div style='padding-top:15px;'>
<label>Select Class : </label>
<input type="checkbox" name="first" id="first" value="I Year" >I Year
<input type="checkbox" name="second" id="second" value="II Year">II Year
<input type="checkbox" name="third" id="third" value="III Year">III Year
</div>
<div style='padding-top:25px;'>
<label> Date : </label>
<span class="demo">
<input type="text" name="date" class="datepicker">
</span>
<span> FN : <input type='checkbox' name='actfnone' value='0'> </span>
<span > AN : <input type='checkbox' name='actanone' value='0'> </span>
</div>
<div style='padding-top:10px;'>
<div style='padding-top:5px;'>
ID No: 1 <span style="padding-left:10px;"> <select name="sId1" class="sId" onchange="Javascript: callone(this.options[this.selectedIndex].value);"> </select> </span><span> <input type="text" id="nameone" value=""> </span>
</div>
<div style='padding-top:5px;'>
ID No: 2 <span style="padding-left:10px;"> <select name="sId2" class="sId" onchange="Javascript: calltwo(this.options[this.selectedIndex].value);"> </select> </span><span > <input type="text" id="nametwo" value=""> </span>
</div>
<div style='padding-top:5px;'>
ID No: 3 <span style="padding-left:10px;"> <select name="sId3" class="sId" onchange="Javascript: callthree(this.options[this.selectedIndex].value);"> </select></span><span > <input type="text" id="namethree" value=""> </span>
</div>
<div style='padding-top:5px;'>
ID No: 4 <span style="padding-left:10px;"> <select name="sId4" class="sId" onchange="Javascript: callfour(this.options[this.selectedIndex].value);"> </select> </span><span > <input type="text" id="namefour" value=""> </span>
</div>
<div style='padding-top:5px;'>
ID No: 5 <span style="padding-left:10px;"> <select name="sId5" class="sId" onchange="Javascript: callfive(this.options[this.selectedIndex].value);"> </select> </span> <span > <input type="text" id="namefive" value=""> </span>
</div>
</div>
</div>
<p align="center">
<input type="submit" value="SUBMIT" onclick="javascript: return ATcheck()" >
<input type="hidden" name="add" value="add" />
</p>
</form>
Jquery Area [Works fine] :
Action Part one :
<script>
function popfield(acyr,crs,cls) {
$.ajax({
type : "POST",
url : "mattendance.php",
cache: false,
data : "academic="+ acyr +"&course="+ crs +"&class="+ cls, success: function(data) {
$(".sId").html(data).show();
}
});
}
$("#aone").ready(function() {
$("#academic").change(function () {
var acval = $(this).find("option:selected").text();
});
$("input:radio[name=course]").click(function() {
var csval = $('input[name=course]:checked', '#aone').val();
});
$("#first").click(function () {
if ($("#first").is(":checked")) {
var sclass = $('input[name=first]:checked').val();
$(":checkbox").click(popfield($("#academic").find("option:selected").text(),$('input[name=course]:checked', '#aone').val(),$('input[name=first]:checked').val()));
}
});
$("#second").click(function () {
if ($("#second").is(":checked")) {
var sclass = $('input[name=second]:checked').val();
$(":checkbox").click(popfield($("#academic").find("option:selected").text(),$('input[name=course]:checked', '#aone').val(),$('input[name=second]:checked').val()));
}
});
$("#third").click(function () {
if ($("#third").is(":checked")) {
var sclass = $('input[name=third]:checked').val();
$(":checkbox").click(popfield($("#academic").find("option:selected").text(),$('input[name=course]:checked', '#aone').val(),$('input[name=third]:checked').val()));
}
});
});
Action Part two [Problem is here] :
function callone(sid) {
alert('alert one :'+ sid);
var dataString = "Id="+ sid ;
$.ajax({
type : "POST",
url : "mattendance.php",
data : dataString,
success: function(data) {
$("#nameone").html(data);
}
});
} }
[PHP part for Action Two :]
<?
// Call to Names from Ajax
if($_POST["Id"]!="")
{
$dbselect=mysql_query("SELECT * FROM `tblNewStudent` WHERE `IdNo`='".$_POST["Id"]."' ORDER BY `SNo` ASC ");
$row = mysql_fetch_object($dbselect);
print $row->Name;
}
?>
I tried alot by seeing examples but cant able to display new value in
text field n got wierd , seriously looking forward to ur best
answers..
Thanks in Advance , {Raymond Herbert}
Try using following
$('#nameone').val(data);
instaed of
$("#nameone").html(data);