500 (Internal Server Error) in my AJAX POST - javascript

I have a button to cancel a hiring event and included in that is a confirmation message using sweet alert but then confirming the action wont go pass my ajax post.
I tried checking the url's if it's correct but then again, all of the url's are correct.
Here is my function in the controller:
{
$hiring_id = $this->input->post('key');
$hiring_data = $this->hiring_model->get_hiring($hiring_id);
$return_a = $this->search_model->update_applicant($hiring_data[0]->applicant_id);
$return_b = $this->search_model->cancel_hiring($hiring_id);
if ($return_a > 0 && $return_b > 0) {
echo json_encode(array('ret'=>'1','new_url'=>base_url('partners/hiring')));
}
}
Here is my code in the HTML:
<button type="button" class="btn btn-danger btn-sm m-btn--custom" id="cancel_hiring" data-url="<?php echo base_url('partners/hiring/cancel_hiring/'); ?>" data-key="<?php echo $h->hiring_id;?>">
And here is my javascript:
<script>
$(function(){
$("#cancel_hiring").click(function(e){
Swal.fire({
title: 'Are you sure you want to cancel this hiring?',
text: "You won't be able to revert this!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Cancel Hiring'
}).then((result) => {
if (result.value) {
console.log($(this).data('url'));
console.log($(this).data('key'));
$.post($(this).data('url'), {key:$(this).data('key')}, function(data) {
console.log(data);
if (data.ret) {
console.log('cancelled');
Swal.fire(
'Success!',
'Hiring cancelled.',
'success'
)
var delay = 1000;
setTimeout(function(){ window.location = data.new_url; }, delay);
} else {
Swal.fire(
'Failed!',
'Error occured.',
'warning'
)
}
}, 'json');
}
})
});
});
</script>

Related

Sweet alert wait for reaction

Alert does not wait for user response and the form completes, Does anyone know how to solve this problem?
I need it in response to a button that is not on the form because I have several buttons on the form. Thank you
function Upravit(){
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
Swal.fire(
'Deleted!',
'Your file has been deleted.',
'success'
);
return true;
} else {
return false
}
});
}
<button {if $d->aktivni == 0}disabled{/if} onclick="return Upravit()"
n:name="upravitreklamaci" type="submit" class="btn btn-primary btn-lg btn-block">Upravit
reklamaci</button>

add sweetalert2 confirm delete in php without json data

How to use sweetalert2 with delete function in button href ?
i have button like this
<button type="button" id="btnDelete" href=" <?php echo site_url('administrator/master/delete/' . $a->idDept); ?>" class="btn btn-danger fa fa-trash but" data-toggle="tooltip" data-placement="top" title="Delete"></button>
$('.btnDelete').click(function() {
swal.fire({
title: 'Are you sure?',
text: "It will permanently deleted !",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then(function() {
swal.fire(
'Deleted!',
'Your file has been deleted.',
'success'
);
});
});
sweetalert2 working but not delete the data
You will need to pass the fetch/ajax method in Swal.fire call.
$('.btnDelete').click(function(e) {
e.preventDefault();
var url = $(this).attr('href');
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!',
preConfirm: () => {
return fetch(url)
.then(response => {
console.log(response);
if (!response.ok) {
throw new Error(response.statusText)
}
return response.json()
})
.catch(error => {
Swal.showValidationMessage(
`Request failed: ${error}`
)
})
}
}).then((result) => {
if (result.isConfirmed) {
Swal.fire(
'Deleted!',
'Your file has been deleted.',
'success'
)
}
});
});
Use this example codepan and modify it as you need.
https://codepen.io/ympervej/pen/wvJQyOr

Validate required field before swal?

How to validate required input field before swal?I have added required in input field but its not working?swal alerts are working fine,Anyone please help me to solve this issue.Thanks in advance.
<input type="text" class="form-control" name="marks" id="marks" required>
<button type="button" id="btn_upload<?php echo $s;?>" class="btn btn-primary" onClick="FunctionAdd(<?php echo $s;?>)" >Submit</button>
<script>
function FunctionAdd($s)
{
swal({
title: 'Are you sure?',
text: "The data will input",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Add',
cancelButtonText: 'Cancel'
}).then((result) => {
if (result.value) {alert('submitting');
var myForm = $("#myForm"+$s)[0];
$.ajax({
url:"<?php echo base_url(); ?>creates",
type:"post",
data: new FormData(myForm),
processData:false,
contentType:false,
cache:false,
async:false,
success: function(data){
window.location = '';
},
failure:function(d){
alert("Error")
alert(d)
}
});
}
else if (result.dismiss === 'cancel') {
swal(
'Cancelled',
'You stay here :)',
'error'
)
}
})
You can try triggering HTML5 form validation first using HTMLFormElement.reportValidity
function FunctionAdd($s) {
var myForm = $("#myForm"+$s)[0];
if (myForm.reportValidity()) {
swal({
...
}).then((result) => {
...
});
}
}

Sweet alert not triggering my php file

I am using sweet alerts 2 for my webpage and i am trying to trigger my php file when the user presses delete on the sweet alert, if they do not do nothing. For some odd reason it is not triggering the php file, the pop does not come up either, i ran a simple sweet alert in the console just to see if i have the correct scripts in and it works fine.
I have this code so far :
<form method="post" id="deleteF" name="delete">
<button type="submit" name="delete" class="btn btn-xl btn-mrg" id="delete" >Delete</button>
</form>
$(document).on('submit', 'deleteF', function (e) {
e.preventDefault();
swal({
title: "Are you sure?",
text: "You can not recover your account if you delete!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Delete",
cancelButtonText: "Cancel",
closeOnConfirm: false,
closeOnCancel: false
},
function (isConfirm) {
if (isConfirm) {
$.ajax({
type: 'POST',
url: 'lib/delete.php',
success: function (data) {
},
error: function (data) {
swal("NOT Deleted!", "Something happened!", "error");
}
});
} else {
swal("Cancelled", "Your account was not deleted", "error");
}
});
return false;
});
My php file only has a simple echo in it just to for testing, any help would be greatly appreciated, Note i only included the relevant parts, the sweet alert section is enclosed in script tags
<a data-id='" . $rows['id'] . "' id='delete_id' class='btn btn-danger btn-sm'<span class='glyphicon glyphicon-trash' href='javascript:void(0)'></span>delete</a>
<script>
$(document).ready(function() {
$(document).on('click', '#delete_id', function(e) {
var id = $(this).data('id');
SwalDelete(id);
e.preventDefault();
});
});
function SwalDelete(id) {
Swal.fire({
title: 'Are you sure?',
text: "It will be deleted permanently!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!',
showLoaderOnConfirm: true,
preConfirm: function() {
return new Promise(function(resolve) {
$.ajax({
url: 'office_info_delete.php',
type: 'POST',
data: 'id=' + id,
dataType: 'json'
})
.done(function(response) {
Swal.fire('Deleted!', 'Your file has been deleted.', 'success')
})
.fail(function() {
Swal.fire('Oops...', 'Something went wrong with ajax !', 'error')
});
});
},
});
}
</script>

my ajax dont work at all?

I have an AJAX function (below). I am trying to pass an input from one page to another.
It redirects me to the other page but does not pass the required value.
I do not understand why it doesn't work as I expected.
I am looking for another way to do this. Any help will be appreciated.
//second page
<?php
$cuit = $_POST['cuit'];
?>
<input id="act" type="text" class="form-control" value="<?php echo $cuit ?>">
function mod(id, origen) {
swal({
title: 'Are you sure?',
text: "You won't be able to revert this!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!',
cancelButtonText: 'No, cancel!',
confirmButtonClass: 'btn btn-success',
cancelButtonClass: 'btn btn-danger',
buttonsStyling: false
}).then(function() {
if (origen == "perfiles") {
$.post("api/eliminar_perfil.php", {
id: id
}, function(mensaje) {
$("#tr_" + id).remove();
});
} else if (origen == "index") {
$.ajax({
type: "post",
url: "perfiles.php",
data: {
cuit: $("#cuit").val()
},
success: function(result) {
location.href = 'http://localhost/miramonteapp/perfiles.php';
}
});
}
swal('Deleted!', 'Your file has been deleted.', 'success')
}, function(dismiss) {
// dismiss can be 'cancel', 'overlay',
// 'close', and 'timer'
if (dismiss === 'cancel') {
swal('Cancelled', 'Your imaginary file is safe :)', 'error')
}
})
}
<input id="cuit" type="text" class="form-control" onkeyup="searchCliente();">
<button id="btnBuscar" onclick="mod($('#cuit'), 'index');" type="button" class="btn btn-default" name="button">Buscar</button>
Boy, I think your solution is simpler, you don't need the ajax call if you want to redirect with a param.
so replace this
$.ajax({
type : "post",
url : "perfiles.php",
data : {cuit: $("#cuit").val()},
success : function(result) {
location.href = 'http://localhost/miramonteapp/perfiles.php';
}
});
by this
location.href = 'http://localhost/miramonteapp/perfiles.php?cuit=' +
$("#cuit").val();
and in your destination page perfiles.php
just read the param and use it, you can read params in JS using this as a guide How to get the value from the GET parameters?

Categories

Resources