selectbox and checkbox not working in ajax - javascript

<select id="multi-select-demo" multiple="multiple">
</select>
in below code we want to assign dynamic options values through ajax but it's not working
$.ajax({
type: 'POST',
url: 'get_checkparishdata',
data: { "campids": optionValue },
success: function (data2) {
new_data2 = data2;
if (new_data2 == '<option>No Record Found</option>') {
$(".bootparish").html(new_data2);
$("#multi-select-demo").html(new_data2);
$('#multi-select-demo').multiselect();
} else {
$(".bootparish").html(new_data2);
$("#multi-select-demo").html(new_data2);
}
}
});
//want to assign dynamic option value
$option_parish = '';
foreach($datapar as $Jurisdiction_parish)
{
$option_parish.='<option value="'.$Jurisdiction_parish.'"
selected>'.$Jurisdiction_parish.'</option>';
}
return $option_parish;

Related

Get value checkbox and push in array

Can you help me on how to get the checkbox values to be a data array? I code like this, and don't get any output. Thanks for helping me.
In my html :
<input class="form-check-input delete-checkbox" type="checkbox" name="checkbox[]" id="checkbox" value="{{ $item->id }}"data-id="{{ $item->id }}">
In my JS:
function multiple_delete(id) {
const selected = [];
$(".form-check input[type=checkbox]:checked").each(function () {
selected.push(this.value);
});
if (selected.length > 0) {
dataid = selected.join(",");
$.ajax({
url: "multiple-delete",
type: "POST",
data: +dataid,
success: function (data) {
if (data["success"]) {
alert(data["success"]);
} else {
alert(data["error"]);
console.log(data);
}
},
error: function (data) {
alert(data.responseText);
},
});
}
}
My output :
First, you are using the wrong selector. You should change .form-check in the javascript to .form-check-input or .delete-checkbox. Also you can easily get values of the selected checkboxes using jquery's .map() method.
Change your javascript code to something like this
function multiple_delete(id) {
const selected = $(".delete-checkbox:checked").map((i, el) => el.value).get();
if (selected.length > 0) {
$.ajax({
url: "multiple-delete",
type: "POST",
data: selected.join(","),
success: function (data) {
if (data["success"]) {
alert(data["success"]);
} else {
alert(data["error"]);
console.log(data);
}
},
error: function (data) {
alert(data.responseText);
},
});
}
}

How to update select option datatable laravel for current row or id

i have problem to update selected option .The database can be update ,but it update for all data .i just want to update current row only
javascript :
$(document).ready(function () {
$('#table_refund').on('change' ,'select', function() {
var data= $(this).closest('tr');
var status = $('option:selected',this).val();
alert(status);
$.ajax({
type: "get",
url: "/financial/approve/refund",
data: {status: status },
dataType: "JSON",
success: function (response) {
$('.alert').show();
}
});
});
controller:
public function studentrefund(Request $req)
{
//update table
data = Refund::find($req->userid);
$data->status = $req->status;
$data->save();
return response()->json(['success'=>'Saved successfully.']);
}
You Need to check particular Id and that userID so you get current row.
Controller::
public function studentrefund(Request $req)
{
//update table
$data = Refund::where('id',$req->id)->where('userid',$req->userid)->first();
$data->status = $req->status;
$data->save();
return response()->json(['success'=>'Saved successfully.']);
}
You can Use fundOrFail the findOrFail and firstOrFail methods will retrieve the first result of the query. However, if no result is found, a Illuminate\Database\Eloquent\ModelNotFoundException will be thrown laravel.com/docs/5.1/eloquent#retrieving-single-models
public function studentrefund(Request $req)
{
//validate user id
request()->validate([
'userid' => 'required',
]);
//update table
$data = Refund::findOrFail($req->userid);
$data->status = $req->status;
$data->save();
return response()->json(['success'=>'Saved successfully.']);
}
option :
<select id="table_refund">
<option data-id="{{ $row->id }}" value="Aktive">Test Data</option>
</select>
Jquery :
$(document).ready(function() {
$('#table_refund').on('change', 'select', function() {
var id = $(this).data('id');
var status = $('option:selected', this).val();
$.ajax({
type: "POST",
url: "{{ url('financial/approve/refund') }}",
data: {
"id": id,
"status": status,
},
dataType: "JSON",
success: function(response) {
$('.alert').show();
}
});
});
Controller :
public function studentrefund(Request $request)
{
$data = Refund::findOrFail($request->id);
$data->status = $request->status;
$data->save();
return response()->json(['success'=>'Saved successfully.']);
}

How do I update datetime field using php?

I have this function wherein I can update my input fields, I have a fetch.php that fetches data from my sql.
So this is my input fields --> [Sample Pic - Input Fields][1]
Then when I click update it returns this --> [Sample Pic - Updating Input Fields][2]
My problem is whenever I update it returns all of the data in the fields except the date and time? How can I fix this?
Below are my codes. TYVM.
<div class="col-sm-2">
<input type="datetime-local" name="date_submitted" id="date_submitted" class="form-control" placeholder="Date Submitted" style="width: 120%;" />
</div>
<script>
$(document).ready(function() {
addDocu();
function addDocu() {
var action = "select";
$.ajax({
url: success: function(data) {
alert(data);
addDocu();
}
});
} else {
alert("All Fields are Required");
}
});
$(document).on('click', '.update', function() {
var id = $(this).attr("id");
$.ajax({
url: "fetch.php",
method: "POST",
data: {
id: id
},
dataType: "json",
success: function(data) {
$('#action').text("Save");
$('#docu_id').val(id);
$('#code').val(data.code);
$('#doc_kind').val(data.doc_kind);
$('#date_submitted').val(data.date_submitted);
$('#remarks').val(data.remarks);
}
})
"select.php",
method: "POST",
data: {
action: action
},
success: function(data) {
$('#code').val('');
$('#doc_kind').val('');
$('#date_submitted').value('');
$('#remarks').val('');
$('#action').text("Add");
$('#result').html(data);
}
});
}
$('#action').click(function() {
var docCode = $('#code').val();
var docKind = $('#doc_kind').val();
var dateSubmitted = $('#date_submitted').val();
var docRemarks = $('#remarks').val();
var id = $('#docu_id').val();
var action = $('#action').text();
if (docCode != '' && docKind != '' && dateSubmitted != '') {
$.ajax({
url: "action.php",
method: "POST",
data: {
docCode: docCode,
docKind: docKind,
dateSubmitted: dateSubmitted,
docRemarks: docRemarks,
id: id,
action: action
},
success: function(data) {
alert(data);
addDocu();
}
});
} else {
alert("All Fields are Required");
}
});
$(document).on('click', '.update', function() {
var id = $(this).attr("id");
$.ajax({
url: "fetch.php",
method: "POST",
data: {
id: id
},
dataType: "json",
success: function(data) {
$('#action').text("Save");
$('#docu_id').val(id);
$('#code').val(data.code);
$('#doc_kind').val(data.doc_kind);
$('#date_submitted').val(data.date_submitted);
$('#remarks').val(data.remarks);
}
})
});
</script>
if(isset($_POST["id"]))
{
$output = array();
$procedure = "
CREATE PROCEDURE whereDocu(IN docu_id int(11))
BEGIN
SELECT * FROM officesectb WHERE id = docu_id;
END;
";
function convert ($rem) {
$rem = trim($rem);
return preg_replace('/<br(\s+)?\/?>/i', "\n", $rem);
}
if(mysqli_query($connect, "DROP PROCEDURE IF EXISTS whereDocu"))
{
if(mysqli_query($connect, $procedure))
{
$query = "CALL whereDocu(".$_POST["id"].")";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result))
{
$rem = $row["remarks"];
$output['code'] = $row["code"];
$output['doc_kind'] = $row["doc_kind"];
$output['date_submitted'] = $row['date_submitted'];
$output['remarks'] = convert($rem);
}
echo json_encode($output);
}
}
}

Load dropdown list based on another dropdown selected item

Load dropdown list based on another dropdown selected item
Here my js :
$("#Ownerstate").change(function () {
var procemessage = "<option value=`0`> Please wait...</option>";
$("#OwnerAgency").html(procemessage).show();
var url = "/Index/GetAgencyState/";
// Get state code
var Ownerstate = $("#Ownerstate :selected").val();
$.ajax({
url: url,
data: { statecode: Ownerstate },
cache: false,
type: "POST",
success: function (carData) {
if (carData.length > 1) {
$("#OwnerAgency").prop("disabled", false);
}
var select = $("#OwnerAgency");
select.html('');
$.each(carData, function (index, itemData) {
select.append($('<option/>', {
value: itemData.Value,
text: itemData.Text
}));
});
},
error: function (reponse) {
alert("error : " + reponse);
}
});
});
Not getting load dropdownlist

Ajax call not succeeding after function call

I am attempting to load dynamic data based on the specific URI segment thats on the page.
Heres my Javascript:
$(document).ready(function() {
$(function() {
load_custom_topics();
});
$('#topics_form').submit(function() {
var topic = document.getElementById('topics_filter').value
$.ajax({
type: "POST",
url: 'ajax/update_session_topic',
dataType: 'json',
data: { topic: topic },
success: function(){
load_custom_topics()
}
});
return false;
});
function load_custom_topics(){
$.ajax({
type: "POST",
url: 'ajax/load_custom_topics',
dataType: 'json',
data: {},
success: function (html) {
$('div.topics_filter').html(html['content']);
get_threads();
}
});
}
function get_threads(){
var page = document.getElementById('page').value
$.ajax({
type: "POST",
url: 'ajax/get_threads',
dataType: 'json',
data: {page: page},
success: function (html) {
$('div#thread_list').html(html['content']);
}
});
}
});
So, as you can see, on page load, it kicks off load_custom_topics which runs just fine. Its then supposed to call get_threads(). This is where the thing stops, and I get no data.
Get_threads()
public function get_threads()
{
$session = $this->session->userdata('active_topic');
if ($this->input->post('page') == '1')
{
$data['list'] = $this->right_model->thread_list_all();
$data['test'] = "all";
} else {
$data['list'] = $this->right_model->thread_list_other($session);
$data['test'] = "not all";
}
if ($data['list'] == FALSE)
{
$content = "no hits";
} else {
$content = $this->load->view('right/thread_list', $data, TRUE);
}
$data['content'] = $content;
$this->output->set_content_type('application/json')->set_output(json_encode($data));
}
While I create the 'page' dynamically, the HTML outputs to this:
<div name="page" value="1"></div>
Any reason why get_threads() is not running?
This does not have an ID. It has a name.
<div name="page" value="1"></div>
This means that your request for getElementById is failing. So this line of code should be showing a TypeError in your console.
var page = document.getElementById('page').value

Categories