jquery is not sending date in query string - php

I have this html code.
<form class="" role="form">
<div class="col-lg-2">
<div class="form-group">
<label for="streams">Select Stream</label>
<select name="streams" id="streams" class="form-control">
<option value="">-- Select --</option>
<?php
while($streamRow = mysql_fetch_array($streamResult)) {
echo
"<option value=".$streamRow[0].">".$streamRow[1]."</option>";
}
?>
</select>
</div>
</div>
<div class="col-lg-2">
<div class="form-group">
<label for="branches">Select Branch</label>
<select name="branches" id="branches" class="form-control">
<option value="">-- Select --</option>
</select>
</div>
</div>
<div class="col-lg-2">
<div class="form-group">
<label for="batches">Select Batch</label>
<select name="batches" id="batches" class="form-control">
<option value="">-- Select --</option>
</select>
</div>
</div>
<div class="col-lg-2">
<div class="form-group divBefore">
<label for="divisionBefore">Div</label>
<select name="divisionBefore" id="divisionBefore" class="form-control">
<option value="">Sel</option>
</select>
</div>
<div class="form-group divAfter hide">
<label for="division">Div</label>
<select name="division" id="division" class="form-control">
<option value="">Sel</option>
<?php
$divisionResult = mysql_query("SELECT * FROM division");
while($divisionRow = mysql_fetch_array($divisionResult)) {
echo
"<option value=".$divisionRow[0].">".$divisionRow[1]."</option>";
}
?>
</select>
</div>
</div>
<div class="col-lg-2">
<div class="form-group semesterBefore">
<label for="semBefore">Sem</label>
<select name="semBefore" id="semBefore" class="form-control">
<option value="">Sel</option>
</select>
</div>
<div class="form-group semesterAfter hide">
<label for="sem">Sem</label>
<select name="sem" id="sem" class="form-control">
<option value="">Sel</option>
</select>
</div>
</div>
<div class="col-lg-2">
<div class="form-group subject">
<label for="subject">Select Subject</label>
<select name="subject" id="subject" class="form-control">
<option value="">-- Select --</option>
</select>
</div>
</div>
<!--<div class="col-lg-2" id="lecBefore">
<div class="form-group">
<label for="subject">Select Lecture</label>
<select name="lect" id="lect" class="form-control">
<option value="">-- Select --</option>
</select>
</div>
</div>
<div class="col-lg-2 hide" id="lecAfter">
<div class="form-group">
<label for="subject">Select Lecture</label>
<select name="lecture" id="lecture" class="form-control">
<option value="">-- Select --</option>
<option value="1">Lecture 1</option>
<option value="2">Lecture 2</option>
<option value="3">Lecture 3</option>
<option value="4">Lecture 4</option>
<option value="5">Lecture 5</option>
<option value="6">Lecture 6</option>
<option value="7">Lecture 7</option>
<option value="8">Lecture 8</option>
</select>
</div>
</div>-->
<div class="col-lg-2 hide" id="date">
<div class="form-group">
<label for="dateNow">Start Date</label> <br>
<div class="input-group date" id="avsDate">
<input type="text" class="form-control" name="avsDateTxt" id="avsDateTxt" readonly value="<?php echo date('d-m-Y');?>"><span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
</div>
</div>
</div>
<div class="col-lg-2 hide" id="date1">
<div class="form-group">
<label for="dateNow">End Date</label> <br>
<div class="input-group date" id="endDate">
<input type="text" class="form-control" name="endDateTxt" id="endDateTxt" readonly value="<?php echo date('d-m-Y');?>"><span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
</div>
</div>
</div>
</form>
and my jquery is.
$(document).ready(function() {
$('.sidebar-menu .attnd').addClass('active');
$('.sidebar-menu .attnd .genExcel').addClass('active');
$(".sidebar-menu .attnd").tree();
$('#streams').change(function() {
$('#branches').html("<option value=''>-- Select --</option>");
$('#batches').html("<option value=''>-- Select --</option>");
$('.divAfter').hide();
$('.divBefore').show();
$('.semesterAfter').hide();
$('.semesterBefore').show();
$('#date').hide();
$('#date1').hide();
$('#lecAfter').hide();
$('#lecBefore').show();
$('#studManip').html('');
if($(this).val() != '')
$.streamSelection($(this).val());
});
$('#branches').change(function() {
$('#batches').html("<option value=''>-- Select --</option>");
$('.divAfter').hide();
$('.divBefore').show();
$('.semesterAfter').hide();
$('.semesterBefore').show();
$('#date').hide();
$('#date1').hide();
$('#lecAfter').hide();
$('#lecBefore').show();
$('#studManip').html('');
if($(this).val() != '')
$.branchSelection($(this).val());
});
$('#batches').change(function() {
$('.divAfter').hide();
$('.divBefore').show();
$('.semesterAfter').hide();
$('.semesterBefore').show();
$('#date').hide();
$('#date1').hide();
$('#lecAfter').hide();
$('#lecBefore').show();
$('#studManip').html('');
if($(this).val() != '') {
$.listSemester($(this).val());
$('.divBefore').hide();
$('#division').val('');
$('.divAfter').show();
}
});
$('#division').change(function() {
$('.studList').hide();
$('.semesterAfter').hide();
$('.semesterBefore').show();
$('#date').hide();
$('#date1').hide();
$('#lecAfter').hide();
$('#lecBefore').show();
$('#studManip').html('');
if($(this).val() != '') {
$('.semesterBefore').hide();
$('#sem').val('');
$('.semesterAfter').show();
}
});
$('#sem').change(function() {
$('#date').hide();
$('#date1').hide();
$('#lecAfter').hide();
$('#lecBefore').show();
$('#studManip').html('');
if($(this).val() != '')
$.searchSubject();
});
$('#subject').change(function() {
if($(this).val() != '') {
$.when($('#date').show())
.then($('#date1').show())
.then($('.btnExcel').show());
}
});
/*$('#lecture').change(function() {
$.when($('#date').hide())
.then($('#studManip').html(''));
if($(this).val() != '') {
$('#date').show();
$.when($.searchStudent())
.then($('#studManip').show());
}
});*/
$('#avsDate').datepicker({
format: "dd-mm-yyyy",
startDate: "01-01-2012",
endDate: '<?php echo date('d-m-Y')?>',
todayBtn: "linked",
autoclose: true,
todayHighlight: true,
});
$('#endDate').datepicker({
format: "dd-mm-yyyy",
startDate: "01-01-2012",
endDate: '<?php echo date('d-m-Y')?>',
todayBtn: "linked",
autoclose: true,
todayHighlight: true,
});
$('#avsDate').datepicker().on('changeDate', function(e) {
$.when($('.btnExcel').attr('href', 'studExcel.php?streamId='+$('#streams').val()+'&branchId='+$('#branches').val()+'&batchId='+$('#batches').val()+'&divisionId='+$('#division').val()+'&semId='+$('#sem').val()+'&sDate='+$('#advDatetxt').val()+'&eDate='+$('#endDatetxt').val()))
.then($('.btnExcel').show());
});
$('#endDate').change(function() {
$.when($('.btnExcel').attr('href', 'studExcel.php?streamId='+$('#streams').val()+'&branchId='+$('#branches').val()+'&batchId='+$('#batches').val()+'&divisionId='+$('#division').val()+'&semId='+$('#sem').val()+'&sDate='+$('#advDatetxt').val()+'&eDate='+$('#endDatetxt').val()))
.then($('.btnExcel').show());
});
/*var date = new Date();
$('#avsDate').datepicker().on('changeDate', function(e) {
$('#studManip').html('');
if(weekday[$(this).datepicker('getDate').getUTCDay()] != 'Sunday'){
$.when($.searchStudent())
.then($('#studManip').show());
} else
alert('Today is Sunday');
});*/
});
$.streamSelection = function(selStreamId) {
$.ajax({
url:"../student/searchBranch.php",
data:{
streamId:selStreamId,
},
success: function(data) {
$('#branches').html(data);
},
error: function(error) {
alert(error);
}
});
}
$.branchSelection = function(selBranchId) {
$.ajax({
url:"../student/searchBatch.php",
data:{
branchId:selBranchId,
},
success: function(data) {
$('#batches').html(data);
},
error: function(error) {
alert(error);
}
});
}
$.listSemester = function(selStreamId) {
$.ajax({
url:"../student/searchStream.php",
data:{
streamId:selStreamId,
},
success: function(data) {
$('#sem').html(data);
},
error: function(error) {
alert(error);
}
});
}
$.searchSubject = function() {
$.ajax({
url:"searchSubject.php",
data:{
streamId : $('#streams').val(),
branchId : $('#branches').val(),
semId : $('#sem').val(),
},
success: function(data) {
$('#subject').html(data);
},
error: function(error) {
alert(error);
}
});
}
every thing is working fine but when I change advDatetxt or endDatetxt jquery won't sending value with query string and describe as indefined. When I put cursor on excel button it will display left bottom corner as.

I think problem is only spell mistakes
<input type="text" class="form-control" name="avsDateTxt" id="avsDateTxt" readonly value="<?php echo date('d-m-Y');?>">
sDate='+$('#advDatetxt').val()
Here spell mistake in id value, change it as
sDate='+$('#avsDatetxt').val()
next one :
<input type="text" class="form-control" name="endDateTxt" id="endDateTxt" readonly value="<?php echo date('d-m-Y');?>">
eDate='+$('#endDatetxt').val()
Here also spell mistake, change it as
eDate='+$('#endDateTxt').val()

Some of your IDs don't match with the selectors.
See this
id="avsDateTxt"
against
$('#advDatetxt')

Related

PHP AJAX - data update/edit inserts as new data instead of updating

Data insert & Delete works fine. However, the edit doesn't update the data. Instead it inserts a new one..
I have INSERT & UPDATE in a single file, made with if conditions:
<?php
include ("./backend/config/connection.php");
include ("./backend/config/function.php");
if (isset($_POST["traffic_operation"])) {
if (isset($_POST["traffic_operation"]) == "Add") {
$traffic_doc = "";
if ($_FILES["traffic_doc"]["name"] != "") {
$traffic_doc = upload_image();
}
$statement = $connection->prepare('INSERT INTO traffic_violations (
plateNumber,
carModel,
carColor,
violationType,
ownerGender,
violationDateTime,
violationLocation,
workingShift,
violationAction,
violationStatement,
cccEmployee
) VALUES (
:plate_number,
:car_model,
:car_color,
:violation_type,
:owner_gender,
:violation_date,
:violation_location,
:working_shift,
:violation_action,
:traffic_doc,
:ccc_employee
)
');
$result = $statement->execute([
":plate_number" => $_POST["plate_number"],
":car_model" => $_POST["car_model"],
":car_color" => $_POST["car_color"],
":violation_type" => $_POST["violation_type"],
":owner_gender" => $_POST["owner_gender"],
":violation_date" => $_POST["violation_date"],
":violation_location" => $_POST["violation_location"],
":working_shift" => $_POST["working_shift"],
":violation_action" => $_POST["violation_action"],
":traffic_doc" => $traffic_doc,
":ccc_employee" => $_POST["ccc_employee"],
]);
if (!empty($result)) {
echo 'Traffic Violation Added';
}
}
if ($_POST["traffic_operation"] == "Edit") {
$traffic_doc = "";
if ($_FILES["traffic_doc"]["name"] != "") {
$traffic_doc = upload_image();
}
$statement = $connection->prepare('UPDATE traffic_violations SET
plateNumber = :plate_number,
carModel = car_model,
carColor = car_color,
violationType = violation_type,
ownerGender = owner_gender,
violationDateTime = violation_date,
violationLocation = violation_location,
workingShift = working_shift,
violationAction = violation_action,
violationStatement = traffic_doc,
cccEmployee = ccc_employee,
WHERE id = :id');
$result = $statement->execute([
":id" => $_POST["violation_id"],
":plate_number" => $_POST["plate_number"],
":car_model" => $_POST["car_model"],
":car_color" => $_POST["car_color"],
":violation_type" => $_POST["violation_type"],
":owner_gender" => $_POST["owner_gender"],
":violation_date" => $_POST["violation_date"],
":violation_location" => $_POST["violation_location"],
":working_shift" => $_POST["working_shift"],
":violation_action" => $_POST["violation_action"],
":traffic_doc" => $traffic_doc,
":ccc_employee" => $_POST["ccc_employee"],
]);
if (!empty($result)) {
echo "Traffic Violation Updated";
}
}
}
?>
I also use Ajax:
$(document).ready(function() {
$("#add_btn").on('click', function() {
$("#traffic_violation_form")[0].reset();
$(".violation-title").text("Add New Violation");
$("#traffic_action").val("Add");
$("#traffic_operation").val("Add");
$("#traffic_doc").html('');
});
var dataTable = $('.violation_data').DataTable({
"processing": true,
"serverSide": true,
"order": [],
"ajax": {
url: "/traffic-fetch",
type: "POST"
},
"columnDefs": [
{
"target": [0, 3, 4],
"orderable": false
}
]
});
// For Inserting New Violation
$(document).on('submit', '#traffic_violation_form', function(e){
e.preventDefault();
var plateNumber = $("#plate_number").val();
var carModel = $("#car_model").val();
var carColor = $("#car_color").val();
var ownerGender = $("#owner_gender").val();
var violationType = $("#violation_type").val();
var violationLocation = $("#violation_location").val();
var workingShift = $("#working_shift").val();
var violationAction = $("#violation_action").val();
var violationStatement = $("#traffic_doc").val().split('.').pop().toLowerCase();
var cccEmployee = $("#ccc_employee").val();
if(violationStatement != '') {
if( $.inArray(violationStatement, ['jpg', 'jpeg', 'JPG', 'JPEG', 'png', 'PNG', 'webp', 'WEBP']) == -1 ) {
alert('Invalid file type.');
$("#traffic_doc").val();
return false;
}
}
if( plateNumber !='' && carModel !='' && carColor !='' && ownerGender !='' && violationType !='' && violationLocation !='' && workingShift !='' && violationAction !='' && cccEmployee !='') {
$.ajax({
url: "/traffic-insert",
method: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
success: function(data) {
$("#traffic_violation_form")[0].reset();
$("#trafficModal").modal('hide');
dataTable.ajax.reload();
}
});
}
else {
alert('Nothing should left empty!');
}
});
// For Updating Violation
$(document).on('click', '.update', function(){
var violation_id = $(this).attr("id");
$.ajax({
url:"/traffic-edit",
method:"POST",
data:{violation_id:violation_id},
dataType:"json",
success:function(data)
{
$("#trafficModal").modal('show');
$(".violation-title").text("Edit Violation");
$("#violation_id").val(violation_id);
$("#plate_number").val(data.plate_number);
$("#car_model").val(data.car_model);
$("#car_color").val(data.car_color);
$("#owner_gender").val(data.owner_gender);
$("#violation_type").val(data.violation_type);
$("#violation_location").val(data.violation_location);
$("#working_shift").val(data.working_shift);
$("#violation_action").val(data.violation_action);
$("#ccc_employee").val(data.ccc_employee);
$("#user_uploaded_image").html(data.violationStatement);
$("#traffic_action").val("Edit");
$("#traffic_operation").val("Edit");
}
})
});
$(document).on('click', '.delete', function(){
var violation_id = $(this).attr("id");
if(confirm("Are you sure you want to delete this?"))
{
$.ajax({
url:"/traffic-delete",
method:"POST",
data:{violation_id:violation_id},
success:function(data)
{
alert(data);
dataTable.ajax.reload();
}
});
}
else
{
return false;
}
});
});
And here is the HTML form:
<form name="traffic_violation_form" id="traffic_violation_form" action="/traffic-process" method="POST" enctype="multipart/form-data">
<div class="modal-body" id="violation-data">
<div class="form-row">
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="plate_number">Plate Number</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">Plate #</span>
</div>
<input type="text" class="form-control" id="plate_number" name="plate_number" >
</div>
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="car_model">Vehicle Model</label>
<div class="input-group">
<input type="text" class="form-control" id="car_model" name="car_model" >
</div>
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="car_color">Vehicle Color</label>
<div class="input-group">
<input type="text" class="form-control" id="car_color" name="car_color" >
</div>
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="owner_gender">Owner Gender</label>
<div class="input-group">
<select class="form-control" name="owner_gender" id="owner_gender" data-toggle="select" >
<option value="" disabled selected>Select Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="No driver">No Driver</option>
</select>
</div>
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="violation_type">Violation Type</label>
<select data-toggle="select" class="form-control" name="violation_type" id="violation_type" >
<option value="" disabled selected>Select violation type</option>
<option value="Speeding">Speeding</option>
<option value="Drifting">Drifting</option>
<option value="Pass traffic light">Pass traffic light</option>
</select>
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="violation_location">Violation Location</label>
<select class="form-control" name="violation_location" id="violation_location" data-toggle="select" >
<option value="" disabled selected>Select Location</option>
<option value="Baylasun">Baylasun</option>
<option value="Baylasun Hotel">Baylasun Hotel</option>
<option value="AL-Waha">AL-Waha</option>
<option value="Al-Morooj">Al-Morooj</option>
<option value="Royal Green">Royal Green</option>
<option value="Beach Towers">Beach Towers</option>
<option value="Emmar Building">Emmar Building</option>
<option value="Industrial Area (East)">Industrial Area (East)</option>
<option value="Industrial Area (West)">Industrial Area (West)</option>
<option value="Hejaz Gate">Hejaz Gate</option>
<option value="Gate 3">Gate 3</option>
<option value="Marina 1">Marina 1</option>
<option value="Marina 2">Marina 2</option>
<option value="Marina 3">Marina 3</option>
<option value="Tala Garden">Tala Garden</option>
<option value="AL-Shorooq">AL-Shorooq</option>
<option value="Yam Beach">Yam Beach</option>
<option value="Sales Center">Sales Center</option>
</select>
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="violation_date">Violation Created At</label>
<input class="form-control" type="datetime-local" name="violation_date" id="violation_date" >
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="working_shift">Working Shift</label>
<select class="form-control" name="working_shift" id="working_shift" data-toggle="select" >
<option value="" disabled selected>Select Shift</option>
<option value="Morning">Morning (A)</option>
<option value="Evening">Evening (B)</option>
<option value="Night">Night (C)</option>
</select>
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="traffic_doc">Violation Statement</label>
<input type="file" class="form-control" id="traffic_doc" name="traffic_doc" >
<span id="user_uploaded_image"></span>
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="ccc_employee">CCC Employee</label>
<input type="text" class="form-control" id="ccc_employee" name="ccc_employee" >
</div>
</div>
<div class="col-md-12 mb-3">
<div class="form-group">
<label class="form-control-label" for="violation_action">Violation Action</label>
<textarea class="form-control" name="violation_action" id="violation_action" ></textarea>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<input type="hidden" name="violation_id" id="violation_id" />
<input type="hidden" name="traffic_operation" id="traffic_operation" />
<input type="submit" name="traffic_action" id="traffic_action" class="btn btn-primary" value="Add" />
</div>
</form>
The issue is in your condition that you are trying to compare with "Add"
The output of $_POST["traffic_operation"]) will be TRUE OR FALSE.
So replace the below line
if (isset($_POST["traffic_operation"]) == "Add")
with
if ( (isset($_POST["traffic_operation"]) AND ($_POST["traffic_operation"] == "Add") )

how to get the value from select2 and use it for the next select2

<div class="form-group col-sm-6">
<label for="" class="control-label">Skill</label> <br>
<select name="skill[]" class="select-skill-only form-control" id="req-skill" multiple="multiple" required>
#foreach ($skill2 as $skill)
<option value="{{$skill->skill_id}}">{{$skill->skill_name}}</option>
#endforeach
</select>
</div>
<div class="form-group col-sm-6">
<label for="" class="control-label">Skill Priority</label> <br>
<select name="priority[]" class="select-skill-only form-control" id="req-priority" multiple="multiple" required>
#foreach ($skill2 as $priorityskill)
<option value="{{$priorityskill->skill_id}}">{{$priorityskill->skill_name}}</option>
#endforeach
</select>
</div>
set a listener for change any select and get value it after any change.
for this you give id to selects.
for example :
let skills = [];
$('#skill-select').on('change', function(){
skills = $(this).val();
$('#priority-select > option').each(function() {
if(!skills.includes($(this).val())){
$(this).prop('disabled', true)
}
});
});
and use skills selected array everywhere

Select2: Disabled selected option value not working

I have module that you will transfer the qty from "warehouse from" to "warehouse to"(Warehouse From & Warehouse To these are my dropdown)
I want put some validation that you cannot longer choose the first selected warehouse coming from "warehouse from"
View
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 input-padding-left">
<div class="form-group">
<label>Document Reference</label>
<input type="text" name="document_reference" class="form-control">
</div>
<div class="form-group">
<label>From:</label>
<select class="form-control warehouse" name="warehouse_from" required="required" id="warehouse_from" style="width: 100%;">
<option></option>
<option value="0">Unassigned Warehouse</option>
#if($warehouses)
#foreach($warehouses as $v => $warehouse)
<option value="{{ $warehouse->id }}">{{ $warehouse->name }}</option>
#endforeach
#endif
</select>
</div>
</div>
<hr>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 input-padding-right">
<div class="form-group">
<label>Posting Date</label>
<input type="text" name="posting_date" class="form-control dates">
</div>
<div class="form-group">
<label>To:</label>
<select class="form-control warehouse" name="warehouse_to" id="warehouse_to" required="required" style="width: 100%;">
<option></option>
#if($warehouses)
#foreach($warehouses as $v => $warehouse)
<option value="{{ $warehouse->id }}">{{ $warehouse->name }}</option>
#endforeach
#endif
</select>
</div>
</div>
Jquery
$('.warehouse').select2({
placeholder: "Select a Warehouse",
});
$(document).on('select2:select', '#warehouse_from', function(e){
// $('#warehouse_from ').on('change',function(){
//send ajax request
var id = $(this).val();
var val = $('#warehouse_from option:selected').text();
alert(val);
$(this).find('option').prop('disabled',false);
$(this).find('option[value='+val+']').prop('disabled',true);
getItemsByWarehouse(id);
e.preventDefault();
return false;
});
Question: Why my disabled didnt work?
You getting the text, not the value of the selected option element:
var val = $('#warehouse_from option:selected').text();
// ...
$(this).find('option[value='+val+']').prop('disabled',true);
It should be:
var val = $('#warehouse_from option:selected').val();
// ...
$(this).find('option[value='+val+']').prop('disabled',true);
My mistake is im disabling the value coming from warehouse_from not in the warehouse_to.
**Jquery**
$(document).on('select2:select', '#warehouse_from', function(e){
//send ajax request
var id = $(this).val();
var val = $('#warehouse_from option:selected').text();
alert(id);
$('select#warehouse_to>option[value="'+id+'"]').attr('disabled','disabled');
getItemsByWarehouse(id);
e.preventDefault();
return false;
});

Fetch Database Value in multiple textbox while selecting dropdown in php

I have two pages add_admin.php and ajax_admin.php when i select name in dropdown it display FIRSTNAME, MIDDLENAME and LASTNAME in TEXTBOXES.
HERE is My Code:
add_admin.php
<!-- Department -->
<div class="col-xs-12 col-sm-12 col-md-12" >
<div class="form-group">
<select id="faculty_name" name="faculty_name" class="form-control" onchange='fetch_select(this.value)' required>
<option selected="selected" disabled="disabled">Please Select Faculty</option>
<?php
$query = mysql_query("select * from faculty_details");
while($row = mysql_fetch_array($query))
{
?>
<option value="<?php echo $row['FACULTY_ID'];?>"><?php echo $row['FIRSTNAME']." ".$row['MIDDLENAME']." ".$row['LASTNAME'];?></option>
<?php
}
?>
</select>
</div>
</div>
<!-- First Name -->
<div class="col-xs-12 col-sm-4 col-md-4">
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-fw fa-user"></i></div>
<input type="text" id="fname" name="fname" class="form-control input-md" placeholder="First Name" value="">
</div>
</div>
</div>
other text box are there contaisn middlename, lastname
Ajax Code
function fetch_select(val)
{
$.ajax
({
type: 'post',
url: 'ajax_admin.php',
data:
{
get_option:val
},
success: function (response)
{
$('#fname').val(response);
}
});
ajax_admin.php
<?php
if(isset($_POST['get_option']))
{
$state = $_POST['get_option'];
$find=mysql_query("select * from faculty_details where FACULTY_ID=$state");
while($row=mysql_fetch_array($find))
{
echo "$row[FIRSTNAME]";
}
}
?>
Get the result as json from the server so that you can easily use it in jQuery:
ajax-admin.php
if(isset($_POST['get_option']))
{
$state = $_POST['get_option'];
$row1=array();
$find=mysql_query("select firstname,middlename,lastname from faculty_details where FACULTY_ID=$state");
while($row=mysql_fetch_array($find))
{
$row1[]=$row;
}
die(json_encode($row1));
}
Ajax Code
function fetch_val(val) {
$.ajax({
url:"ajax-admin.php",
type:"POST",
data:{"get_option":val},
dataType:"JSON",
success:function(data){
$('#fname').val((data[0].firstname));
$('#mname').val((data[0].middlename));
$('#lname').val((data[0].lastname));
}
});
}

Ajax Multiple Submission of the form and get email about the form information

I have issues with submitting a form through ajax. I am submitting the same form two times and I want to email for the same form. On the first submission, I want the name, email, contact information and on the second submission of the form I want the city related information about the user . The form is working fine but I get the mail only when the user submits the form on the second attempt. When a user submits the forms through ajax I did not get any email information about the user name and email etc. Also, i am using a google captcha in that form. the code is working fine just I am not receiving any email on the first submission of the form . Need help..!! Any help would be appreciated Thanks..!!
Here is my code :-
if(isset($_POST['contraform']) && $_POST['contraform']=='submited'){
$secret = "6Lf0MAoUAAAAAMWzxj-kbdy-u32onIYhZ0TdzpDc";
$response = $_POST['g-recaptcha-response'];
$remoteip = $_SERVER['REMOTE_ADDR'];
$url = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response&remoteip=$remoteip");
$result = json_decode($url, TRUE);
if ($result['success'] == 1) {
if(isset($_POST["source"])){
$source = $_POST["source"];
}
if(isset($_POST["uname"])){
$name = $_POST["uname"];
}
if(isset($_POST["uemail"])){
$email = $_POST["uemail"];
}
if(isset($_POST["uphonefull"])){
$phone = $_POST["uphonefull"];
}
if(isset($_POST["utreatments"])){
$treatment = $_POST["utreatments"];
}
if(isset($_POST["form_name"])){
$form_name = $_POST["form_name"];
}
if(isset($_POST["ucity"])){
$city = $_POST["ucity"];
}
if(isset($_POST["uhospital"])){
$hospital = $_POST["uhospital"];
}
if(isset($_POST["thanks"])){
$thanks = $_POST["thanks"];
}
header_contact_forms($source,$name,$email,$phone,$treatment,$form_name, $city,$hospital);
}
}
Here is my form code :-
<form name="contraform" id="contraform" method="post" action="<?php bloginfo("template_directory"); ?>/forms/process.php">
<input type="hidden" name="form_name" id="form_name" value="Header Contact Form" class="basicformname" >
<input type="hidden" value="<?php echo $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" name="source" id="source" />
<input type="hidden" value="submited" name="basicform"/>
<div id="sf1" class="frm">
<fieldset>
<!--<legend>Take a step for a Good health.</legend>-->
<div class="wor-alert"><p class="text-center fill-fields">Please Fill All This Fields*</p></div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon" id="basic-addon"><i class="abc-icon abc-user"></i></span>
<input type="text" placeholder="Name" id="uname" name="uname" class="form-control" autocomplete="off">
<span class="nameerr" id="unameerr"><?php echo #$_SESSION['unameerr'] ? $_SESSION['unameerr']:''; unset($_SESSION['unameerr']); ?></span>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon" id="basic-addon"><i class="abc-icon abc-envelope"></i></span>
<input type="email" placeholder="Email" id="uemail" name="uemail" class="form-control" autocomplete="on">
<span class="emailerr" id="uemailerr"><?php echo #$_SESSION['uemailerr'] ? $_SESSION['uemailerr']:''; unset($_SESSION['uemailer']); ?></span>
</div>
</div>
<div class="form-group">
<input type="text" placeholder="Phone Number" id="uphone" name="uphone" class="uphone form-control" autocomplete="off">
<span class="phoneerr" id="uphoneerr"><?php echo #$_SESSION['uphoneerr'] ? $_SESSION['uphoneerr']:''; unset($_SESSION['uphoneerr']); ?></span>
<input type="hidden" class="uphonefull" name="uphonefull">
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon" id="basic-addon"><i class="abc-icon abc-user-md"></i></span>
<span class="plain-select">
<select name="utreatments" data-style="btn-primary" id="utreatments" class="form-control selectpicker">
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
</select>
</span>
</div>
</div>
<div class="col-md-12 text-center">
<div class="head-captcha">
<div id="recaptcha3"></div>
<input type="hidden" class="hiddenRecaptcha required" name="bhiddenRecaptcha" id="bhiddenRecaptcha">
</div>
</div>
<div class="clearfix" style="height: 10px;clear: both;"></div>
<div class=" text-center">
<button class="btn open1" type="button" id="head-frm-btn">Next <span class="fa fa-arrow-right"></span></button>
</div>
</fieldset>
</div>
<div id="sf2" class="frm" style="display: none;">
<fieldset>
<!--<legend>What treatment are you looking for?</legend>-->
<div class="form-group">
<span class="plain-select">
<select name="ucity" id="ucity" class="form-control">
<option value="none">Select City</option>
<option value="Mumbai">Mumbai</option>
<option value="Pune">Pune</option>
<option value="Banglore">Banglore</option>
<option value="Nagpur">Nagpur</option>
<option value="Goa">Goa</option>
</select>
</span>
</div>
<div class="form-group">
<span class="plain-select">
<select name="uhospital" id="uhospital" class="form-control">
<option value="">Select Hospital</option>
<option value="Hospital One">Hospital One</option>
<option value="Hospital Two">Hospital Two</option>
<option value="Hospital Three">Hospital Three</option>
<option value="Hospital Four">Hospital Four</option>
<option value="Hospital Five">Hospital Five</option>
</select>
</span>
</div>
<div class="clearfix" style="height: 10px;clear: both;"></div>
<div class="clearfix" style="height: 10px;clear: both;"></div>
<div class=" text-center">
<button class="btn back2" type="button"><span class="fa fa-arrow-left"></span> Back</button>
<button class="btn open2" type="button" id="head-frm-btn-two">Next <span class="fa fa-arrow-right"></span></button>
</div>
</fieldset>
</div>
<div id="sf3" class="frm" style="display: none;">
<fieldset>
<!--<legend>Almost done</legend>-->
<div class="waittext">
<p>Our customer care executive will get in contact with you soon. Please bear with us.</p>
</div>
<div class="clearfix" style="height: 10px;clear: both;"></div>
<div class=" text-center">
<button class="btn back3" type="button"><span class="fa fa-arrow-left"></span> Back</button>
<input class="btn open3" value="Submit" type="submit" name="head_form_submit">
</div>
</fieldset>
</div>
</form>
Here is the Ajax Form Submission Code :-
var eml = /^([A-Za-z0-9_\-\.])+\#([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
$("#head-frm-btn, #head_form_submit").click(function () {
$(".wor-alert").hide();
var source = $("#source").val();
var uname = $("#uname").val();
var uemail = $("#uemail").val();
var uphone = $("#uphone").val();
// var ucity = $("#ucity").val();
//var uhospital = $("#uhospital").val();
var utreatments = $("#utreatments").val();
var uphonefull = $(".uphonefull").val();
var form_name = $("#form_name").val();
var basicform = "submited";
// Returns successful data submission message when the entered information is stored in database.
var dataString = 'uname=' + uname + '&uemail=' + uemail + '&uphonefull=' + uphonefull +'&utreatments='+utreatments+'&basicform='+basicform+'&form_name ='+form_name + '&source=' + source;
if (uname == '' || uemail == '' || uphone == '' || eml.test($.trim(uemail)) == false) {
$(".wor-alert").stop().fadeIn('slow');
return false;
}else {
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "wp-content/themes/theme-name/forms/process.php",
data: dataString,
cache: false,
success: function (response) {
$('#response').html(response);
}
});
}
return false;
});
header_contact_froms function :-
function header_contact_forms($source,$name,$email,$phone,$treatment,$form_name, $city,$hospital){
include('mail.php');
if($city){include('thankyou.php');}
}

Categories