Dependent dropdown - php

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"

Related

Show DIV based on form value

I have a database driven table where the last column in the table contains a form that repeats for each row. I want to have a day_inperson field show if the value in the spring_2021_status field is equal to "Both" Since the form is repeated on the page, it doesn't work.
Here is my code for the repeating form. The form itself works fine and inserts into the database. Just need the one field to show if it meets the value of "Both":
<form id="update_location" name="update_location" method="POST">
<div class="form-group">
<br>
<strong>Select a New Location:</strong>
<select class="form-control" id="spring_2021_status" name="spring_2021_status">
<option></option>
<option value="In Person" <?php if (!(strcmp('In Person', ($survey->getColumnVal("spring_2021_status"))))) { echo "selected=\"selected\"";} ?>>In Person</option>
<option value="Remote" <?php if (!(strcmp('Remote', ($survey->getColumnVal("spring_2021_status"))))) {echo "selected=\"selected\"";} ?>>Remote</option>
<option value="Both" <?php if (!(strcmp('Both', ($survey->getColumnVal("spring_2021_status"))))) {echo "selected=\"selected\"";} ?>>Both</option>
<option value="N/A" <?php if (!(strcmp('N/A', ($survey->getColumnVal("spring_2021_status"))))) {echo "selected=\"selected\"";} ?>>N/A</option>
</select>
<br>
<script>
$('#spring_2021_status').on('change',function(){
if( $(this).val()==="Both"){
$("#update_status2").show()
}
else{
$("#update_status2").hide()
}
});
</script>
<div class="row" id="update_status2" style="display:none;">
<div class="col mb-2"><label for="days_inperson"><strong>Which days?</strong> (Hold down CTRL to select multiple ones)</label>
<select multiple class="form-control" id="days_inperson" name="days_inperson">
<option value="Mon">Mon</option>
<option value="Tues">Tues</option>
<option value="Wed">Wed</option>
<option value="Thurs">Thurs</option>
<option value="Fri">Fri</option>
</select>
</div>
</div>
<strong>Add Note:</strong>
<textarea id="notes" name="notes" rows="4" cols="50">
<?php echo($survey->getColumnVal("notes")); ?></textarea>
</div>
<input type="hidden" value="<?php echo($survey->getColumnVal("theein")); ?>" name="ein" id="ein">
<button type="submit" name="submit3" id="submit3" class="btn btn-info">Update Location Status</button>
</form>
You can use class selector inside your div update_status2 then whenever your select-box gets change use $(this).closest('form').find().. this line will get closest form from select-box and then find your div which you need to show/hide.
Demo Code :
//onchange of sleect
$('[name=spring_2021_status]').on('change', function() {
if ($(this).val() === "Both") {
//get closest form tag and then find your div
$(this).closest('form').find(".update_status2").show()
} else {
$(this).closest('form').find(".update_status2").hide()
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="update_location" name="update_location" method="POST">
<div class="form-group">
<br>
<strong>Select a New Location:</strong>
<select class="form-control" name="spring_2021_status">
<option></option>
<option value="In Person" selected>In Person</option>
<option value="Remote">Remote</option>
<option value="Both">Both</option>
<option value="N/A">N/A</option>
</select>
<br>
<!--added class-->
<div class="row update_status2" style="display:none;">
<div class="col mb-2"><label for="days_inperson"><strong>Which days?</strong> (Hold down CTRL to select multiple ones)</label>
<select multiple class="form-control" id="days_inperson" name="days_inperson">
<option value="Mon">Mon</option>
<option value="Tues">Tues</option>
<option value="Wed">Wed</option>
<option value="Thurs">Thurs</option>
<option value="Fri">Fri</option>
</select>
</div>
</div>
<strong>Add Note:</strong>
<textarea id="notes" name="notes" rows="4" cols="50">
something...</textarea>
</div>
<input type="hidden" value="abc" name="ein" id="ein">
<button type="submit" name="submit3" id="submit3" class="btn btn-info">Update Location Status</button>
</form>
<form id="update_location" name="update_location" method="POST">
<div class="form-group">
<br>
<strong>Select a New Location:</strong>
<select class="form-control" name="spring_2021_status">
<option></option>
<option value="In Person" selected>In Person</option>
<option value="Remote">Remote</option>
<option value="Both">Both</option>
<option value="N/A">N/A</option>
</select>
<br>
<div class="row update_status2" style="display:none;">
<div class="col mb-2"><label for="days_inperson"><strong>Which days?</strong> (Hold down CTRL to select multiple ones)</label>
<select multiple class="form-control" id="days_inperson" name="days_inperson">
<option value="Mon">Mon</option>
<option value="Tues">Tues</option>
<option value="Wed">Wed</option>
<option value="Thurs">Thurs</option>
<option value="Fri">Fri</option>
</select>
</div>
</div>
<strong>Add Note:</strong>
<textarea id="notes" name="notes" rows="4" cols="50">
something...</textarea>
</div>
<input type="hidden" value="abc" name="ein" id="ein">
<button type="submit" name="submit3" id="submit3" class="btn btn-info">Update Location Status</button>
</form>

create variable of select option name instead of value in php

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);
}
});
});
});

How select only particular designations from the department

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;

PHP AJAX Change value based on combobox

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>

How do I get value of cID on form submission

<img src="img/commentBelowIcon.png" width="26" height="26" class="left" /><h3>Add Comment</h3>
<?php if(!empty($_GET['pID'])) $the_pID = mysql_real_escape_string($_GET['pID']); $thedt = date("Y-m-d");?>
<form action="inc/q/prof.php?pID=<?php echo $the_pID; ?>" method="post" id="addition">
<div class="field required">
<select id="courseInfoDD" class="verifyText" name="courseInfoDD" tabindex="1">
<option disabled="disabled">Course...</option>
<?php while($row3 = $sth3->fetch(PDO::FETCH_ASSOC))
{echo "<option value=". $row3['cID'] . ">" .$row3['prefix']." ".$row3['code']."</option>";}
?>
</select>
</div>
<div class="field required">
<select id="commQuarter" class="verifyText" name="commQuarter" tabindex="2" >
<option disabled="disabled">Quarter...</option>
<option value="Fall">Fall</option>
<option value="Winter">Winter</option>
<option value="Spring">Spring</option>
<option value="Summer">Summer</option>
</select>
</div>
<div class="field required">
<select id="commYr" name="commYr" class="verifyText" tabindex="3">
<option disabled="disabled">Year...</option>
<?php $startdate = 2000;$enddate = date("Y");$years = range ($startdate,$enddate);foreach($years as $year){echo "<option value='$year'>$year</option>";}?>
</select>
</div>
<div class="field required">
<select id="commExp" class="verifyText" name="commExp" tabindex="4" >
<option disabled="disabled">Overall Experience</option>
<option value="1">Positive</option>
<option value="2">Neutral</option>
<option value="3">Negative</option>
</select>
</div>
<div class="field required">
<textarea type="text" id="addComment" class="verifyText" name="addComment" tabindex="5" value="Enter comment"></textarea></div>
<input type="hidden" name="dt" value="<?php echo $thedt; ?>" />
<input type="hidden" name="pID" value="<?php echo $the_pID; ?>" />
<div class="field required">
Accept Terms?
Yes: <input type="radio" name="terms" value="Yes" id="accepting" tabindex="6" />
No: <input type="radio" name="terms" value="No" id="accepting" tabindex="7" />
</div>
<p class="iferror">Please correct the above.</p>
<input type="submit" name="submit" id="submit" tabindex="8" />
</form>
</div>
</div>
<?php // Get select box options
$pID3 = filter_input(INPUT_GET, 'pID', FILTER_SANITIZE_NUMBER_INT);
$pdo3 = new PDO('mysql:host=###;dbname=###', $u, $p);
$pdo3->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth3 = $pdo3->prepare('
SELECT pID, C.cID, C.prefix, C.code
FROM Department D, Course C, Professor P
WHERE pID = ?
AND D.dID = C.dID
AND D.dID = P.dID;
');
$sth3->execute(array(
$pID3
));
?>
I get submitted data that looks like this:
How do I get a value for cID to submit as well? Can someone show me what I need to put in my php?
Thanks
execute
$cid = mysql_insert_id();
after
$sth3->execute(array(
$pID3
));
link http://php.net/manual/en/function.mysql-insert-id.php
hope helps
:)

Categories