how to create the 'if-else' condition with ajax jquery? - php

how to create the 'if-else' condition with the following ajax jquery:
<script type="text/javascript">
$(document).ready(function () {
$('select[name="org_id"]').on('change', function () {
var stateID = $(this).val();
if (stateID){
$.ajax({
url: '<?php base_url();?>pasien_lama/dokter/'+stateID,
type: "GET",
dataType: "json",
success:function (data) {
$('select[name="person_id"]').empty();
$('select[name="person_id"]').append('<option >- Pilih Dokter -</option>');
$.each(data, function (key, value) {
$('select[name="person_id"]').append('<option value="'+ value.person_id +'">'+ value.person_nm +'</option>')
});
}
});
} else {
$('select[name="person_id"]').empty();
}
});
});
</script>
if not selected, the select option will return empty automatically
here is html code :
<label>Poli Tujuan</label>
<select class="form-control select2" name="org_id" style="width: 100%;">
<option selected="selected">- Pilih Poli -</option>
<?php foreach ($xocp_orgs as $nama_poli): ?>
<option value="<?php echo $nama_poli['org_id']; ?>"><?php echo $nama_poli['org_nm']; ?></option>
<?php endforeach; ?>
</select>
<label>Poli Dokter Spesialis</label>
<select id="imageSelector" class="form-control select2" name="person_id" style="width: 100%;" required>
<option>- Pilih -</option>
</select>

You can try something like jQuery.when() to get your task done, by making your method async. Resolve your actions based on your choice and then continue with necessary changes.
IF you want a learn better, this answer should help you.

Set the value for the default option to a empty string
<option selected="selected" value="">- Pilih Poli -</option>
change your if statement to
if (stateID.length) {//
demo:
$('select[name="org_id"]').on('change', function () {
var stateID = $(this).val();
if (stateID.length){
console.log('has values');
} else {
console.log('empty');
$('select[name="person_id"]').empty();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label>Poli Tujuan</label>
<select class="form-control select2" name="org_id" style="width: 100%;">
<option selected="selected" value="">- Pilih Poli -</option>
<?php foreach ($xocp_orgs as $nama_poli): ?>
<option value="<?php echo $nama_poli['org_id']; ?>">
<?php echo $nama_poli['org_nm']; ?>
</option>
<?php endforeach; ?>
</select>
<label>Poli Dokter Spesialis</label>
<select id="imageSelector" class="form-control select2" name="person_id" style="width: 100%;" required>
<option>- Pilih -</option>
</select>

Related

issue on two multiple dropdown

https://i.stack.imgur.com/2O2Ug.png
when i change course name first row change only that row parent topic
how to do this please give advice
first course name select box code
<select class="form-control select2" onChange="getval(this.value)"; id="course_id1" name="course_id[]" data-live-search="true" >
<option value="" selected="selected"> Select Course Name</option>
<?php
$sql = "SELECT * from course_master";
$result = $connect->query($sql);
while($row_pt = $result->fetch_array())
{
?>
<option value="<?php echo $row_pt['course_id']; ?>" ><?php echo $row_pt['course_name']; ?></option>
<?php
}
?>
</select >
second select box when i select course name from drop down change that row parent id
<select class="form-control select2" id="parent_id" name="parent_id[]" data-live-search="true" ></select>
call function on change in course name
function getval(val)
{
$.ajax({
type: "POST",
url: "get.php",
data: {parent_id:val,syllabus_id:syllabus_id},
success: function(data)
{
$("#parent_id").html(data);
}
});
}
get.php
<?php
include('connection/core.php');
if(isset($_SESSION['userId']))
{
if (!empty($_POST["parent_id"]))
{
$query_sql = "SELECT syllabus_id,topic_name FROM syllabus WHERE syllabus_id!='". $_POST["syllabus_id"]."' AND course_id ='" .$_POST["parent_id"] ."'";
$results = $connect->query($query_sql);
echo $query_sql;
?>
<option value="0" selected> Parent </option>
<?php
foreach ($results as $p)
{
?>
<option value="<?php echo $p["syllabus_id"]; ?>"><?php echo $p["topic_name"]; ?></option>
<?php
}
}
}
?>
Add below code for your first row.
For example.
<select class="form-control select2 selectCourse" data-id="1" id="course_id_1" name="course_id_1[]" data-live-search="true" >
And for your parent topic name add as below.
<select class="form-control select2" id="parent_id_1" name="parent_id_1[]" data-live-search="true"></select>
Now when you are adding new dynamic row in your HTML then add selectCourse as class name in your select box of course name AND data-id="1" for each dynamically added row.
For example.
For course name add as below.
<select class="form-control select2 selectCourse" data-id="2" id="course_id_2" name="course_id_2[]" data-live-search="true" >
For parent topic name add as below.
<select class="form-control select2" id="parent_id_2" name="parent_id_2[]" data-live-search="true"></select>
In your script add code as below.
<script type="text/javascript">
$(document).on('change', '.selectCourse', function(){
var course_id = $(this).attr('data-id');
var val = $(this).find(":selected").val();
var syllabus_id = 1;
$.ajax({
type: "POST",
url: "get.php",
data: {parent_id:val,syllabus_id:syllabus_id},
success: function(data)
{
$("#parent_id_" + course_id).html(data);
}
});
});
</script>

Can't get the select option value into jQuery post

HTML:
<select id="s2_basic" class="form-control" id="userid" name="userid">
<option value="" selected>-- Choose option --</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
Javascript:
function submitForm() {
var userid = $('#userid').attr('selected', 'selected');
$.ajax({
type: "POST",
url: "submit.php",
data: "userid=" + userid,
success: function(text) {
if (text == "success") {
formSuccess();
} else {
formError();
submitMSG(false,text);
}
}
});
}
Submit PHP returns error message:
Value: ([object Object])
I have also tried
var userid = $('#userid').val();
var userid = $('#userid option').val();
Still, the option value never gets passed correctly.
What am I doing wrong?
I want just the value="x" number to be posted as userid=x.
remove id="s2_basic" from your select.
change :
<select id="s2_basic" class="form-control" id="userid" name="userid">
to
<select class="form-control" id="userid" name="userid">
$('#userid').on('change',function(){
console.log('value '+$('#userid option:selected').val())
console.log('text '+$('#userid option:selected').text())
console.log('index '+$('#userid option:selected').index())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<select class="form-control" id="userid" name="userid">
<option value="" selected>-- Choose option --</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
try this

jQuery ReferenceError: function is not defined

I have a simple jquery function that reflects the selected value of a dropdown to textbox but the problem is that the console says that my function was undefined. I put the function inside the onchange event of the dropdown. Please note that all of my jquery functions was on the bottom of the page..
and here is the dropdown:
<div class="form-group">
<label class="control-label">Operation</label>
<select class="form-control" id="operationName" name="operationName">
<option value="">-SELECT-</option>
<?php
foreach($operation as $val)
{
?>
<option value="<?php echo $val['OperationName'];?>"><?php echo $val['OperationName'];?></option>
<?php
}
?>
</select>
<span class="text-danger"><?php echo form_error('operation');?></span>
<input type="text" class="form-control" id="deldays" name="deldays"
value="" />
this is the operation array model
function getDeliveryDays($str)
{
$this->db->select('DeliveryDays');
$this->db->from('operation');
$this->db->where('OperationName',$str);
return $this->db->get()->result();
}
this is the controller:
public function getDays()
{
$id = $this->input->post('q');
$data['days'] = $this->wip_model->getDeliveryDays($id);
echo json_encode($data);
}
I took the working in my other project and use it in my current project but still the same result and when I inspect it in the dev tools, the said script is missing in view source but it's present in IDE:
this is the code:
function showDeliveryDay(operation)
{
$.ajax({
type: "POST",
url: "<?php echo site_url('wip/getDays/');?>",
data: {q:operation},
success: function(data){
console.log(data);
},
});
}
Thanks in advance....
Does you put the function inside domready? If so, remove that and placed it outside like so :
<script>
function showDays() {
var x = document.getElementById("operationName").value;
document.getElementById("deldays").innerHTML = x;
}
// domready here
$(function(){...}); // or $(document).ready(function(){...});
</script>
Make sure you are including your jquery in your code. try this:
jQuery( document ).ready(function() {
jQuery( "#operationName" ).change(function() {
var pVal = jQuery("#operationName").val();
jQuery("#deldays").val(pVal);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<label class="control-label">Operation</label>
<select class="form-control" id="operationName" name="operationName">
<option value="">-SELECT-</option>
<option value="one">1</option>
<option value="two">2</option>
<option value="three">3</option>
</select>
<span class="text-danger"></span>
<input type="text" class="form-control" id="deldays" name="deldays"
value="" />
</div>
You can refer this code
Html
<div class="form-group">
<label class="control-label">Operation</label>
<select class="form-control" id="operationName" name="operationName">
<option value="">-SELECT-</option>
<option value="one">1</option>
<option value="two">2</option>
<option value="three">3</option>
</select>
<span class="text-danger"></span>
<input type="text" class="form-control" id="deldays" name="deldays"
value="" />
Js code
jQuery( document ).ready(function() {
jQuery('#operationName').on('change', function(){
var p=jQuery(this).val();
jQuery("#deldays").val(p);
});
});
You can view demo here

jquery ajax submit only submitting first value

I am submitting a form using jquery and ajax. I have my first select menu and depending on the value of this select menu a div that contains a different menu will become visible.
<table>
<tr>
<td>
<select required="" class="input-medium" id="foulTypes" name="foul" data-original-title="" title="">
<option value="">Select Foul</option>
<option value="85">AID - Aiding Runner</option>
<option value="1">BAT - Illegal Bat</option>
<option value="2">BBW - Block Below Waist</option>
<option value="3">BOB - Blocking Out of Bounds</option>
<option value="4">CHB - Chop Block</option>
</select>
</td>
</tr>
</table
<script>
var foulTypes = $('#foulTypes');
var select = this.value;
foulTypes.change(function () {
//We first disable all, so we dont submit data for more than 1 category
$("#catDPI, #catILF, #catOPI, #catOH, #catDH, #catILD, #catUNS, #catUNR, #catTGT").hide().find(".category").attr("disabled", true);
var $divSelectedCategory;
if ($(this).val() == '1') {
$divSelectedCategory = $("#catDPI");
$('#catDPI').show();
} else {
$('#catDPI').hide();
}
if ($(this).val() == '85') {
$divSelectedCategory = $("#catILF");
$('#catILF').show();
} else {
$('#catILF').hide();
}
if ($(this).val() == '2') {
$('#catOPI').show();
$divSelectedCategory = $("#catOPI");
} else {
$('#catOPI').hide();
}
//We now enable only for the selected category
$divSelectedCategory.show().find(".category").attr("disabled", false).val('');
});
</script>
<div id="catDPI" style="display:none;">
<select name="category" id="dpiCategory" class='category' style="width:210px;">
<option value="Playing Through Receiver">Playing Through Receiver</option>
<option value="arm bar">arm bar</option>
<option value="Grabbing">Grabbing</option>
</select>
</div>
<div id="catILF" style="display:none;">
<select name="category" id="ilfCategory" class='category' style="width:210px;">
<option value="">Select</option>
<option value="moving">Moving</option>
<option value="illegal">Illegal</option>
<option value="standing">Standing</option>
</select>
</div>
<div id="catOPI" style="display:none;">
<select name="category" id="opiCategory" class='category' style="width:210px;">
<option value="">Select</option>
<option value="restrict">Restrict</option>
<option value="holding">Holding</option>
</select>
</div>
The foulTypes .change function is working and the correct div is being displayed. The problem I am having is when I submit the form. If the #catDPI is visible the category menu selected value is submitted fine. If #catILF or #catOPI is visible the value for category is blank. I am not using the Ids of the category menus.
$("#submit-foul").submit(function(e){
e.preventDefault();
var category = $(".category").val();
var dataString = 'category=' + category;
if($("#submit-foul").valid()){
$.ajax({
url: 'myUrl',
type: 'POST',
success: function(data){
if(data == 1){
alert('Success');
};
}
});
}
return false;
});
I am not sure what I am doing wrong. Time to send it to the experts!! I appreciate all your help.
Try to change function "foulTypes" and add class in visible select.
foulTypes.change(function () {
//We first disable all, so we dont submit data for more than 1 category
$("#catDPI, #catILF, #catOPI, #catOH, #catDH, #catILD, #catUNS, #catUNR, #catTGT").hide().find(".category").attr("disabled", true);
var $divSelectedCategory;
if ($(this).val() == '1') {
$divSelectedCategory = $("#catDPI");
$('#catDPI').show();
$('#dpiCategory').addClass("visible");
} else {
$('#catDPI').hide();
$('#dpiCategory').removeClass("visible")
}
.... CONTINUE
In your ajax submit..
var category = $(".category.visible").val();

show the selected drop down values to the another table

I am Have html table inside each td i have dropdown see below image how it looks like
if i select 1201 in dropdown of second td also i want to change the value of to other dropdown with the same id,if the id is not matched it will not change
<td style="width:141px" id="CPH_GridView1_route'.$rows['net_id'].'" >
<select name="mySelect" id="mySelect" class="edit1 route '.$rows["net_id"].'" >
<option value="-1">Choose..</option>';
<option value="-1">1201</option>';
<option value="-1">1101</option>';
</select>
</td>
ajax
<script>
$(document).ready(function(){
$('.edit1').on('change', function(){
$.ajax({ type: "POST",
url:"xxxxx/routestatusupdate.php",
data:"value="+$(this).val()+"&rowid="+arr[2]+"&field="+arr[1]+"&clientid="+clientid+"&account_id="+account_id,
success: function(res){
data = jQuery.parseJSON(res); //added line
alert('Saved Successfully!');
$('#CPH_GridView1_route').empty();
$('#CPH_GridView1_route').append(data.routeupdate);
$('.ajax').html($(this).val());
$('.ajax').removeClass('ajax');
}
});
});
});
</script>
<select name="mySelect" id="mySelect" class="edit1 route '.$rows["net_id"].'" onChange="foo()">
<option value="-1">Choose..</option>';
<option value="-1">1201</option>';
<option value="-1">1101</option>';
</select>
<select name="second" id="mySelect" >
<option value="-1">Choose..</option>';
<option value="-1">1</option>';
<option value="-1">2</option>';
</select>
<script>
function foo(){
alert("cahnge");
document.getElementById("div1').innerHTML="<select name="second" id="mySelect" >
<option value="-1">Choose..</option>';
<option value="-1">1</option>';
<option value="-1">2</option>';
</select>";
}
</script>

Categories