AJAX 500 Internal Server ERROR But The Proccess is Success - ajax

I am Facing Problem With Ajax It Keep Showing 500 internal Server Error
But The Ajax Proccess is Success I don't know Why I Have Been Trying To Fix This All Day But I Got Nothing But Weird Is The Process Is Success
Here is My Ajax Code
<script type="text/javascript" >
$(document).ready(function(){
$('#add_button').click(function(){
$('#user_form')[0].reset();
$('.modal-title').text("Add User");
$('#action').val("Add");
$('#user_uploaded_image').html('');
})
var dataTable = $('#user_data').DataTable({
});
$(document).on('click', '.delete_image', function(){
var user_id = $(this).attr("id");
var link = $(this).attr("style");
var mana = $(this).attr("name");
//console.log()
var dom_parent = $(this).context.parentElement.parentElement
if(confirm("Apakah Anda Yakin Ingin Menghapus Data Gambar Ini?"))
{
$.ajax({
url:"<?php echo base_url(); ?>produk/hapus_gambar",
method:"POST",
data:{mana:mana,link:link,user_id:user_id},
success:function(data)
{
alert(data);
}
});
}
else
{
return false;
}
});
});
</script>
And This Is My Button Code
<td align="center"> <button type="button" name="gambar" id="<?=$list_produk['id']?>" class="btn btn-danger delete_image" style="<?= $list_produk['gambar']; ?>"><img style="width:120px; height:120px;" src="http://localhost/proyek/gambar/thumb/<?=$list_produk['gambar']?> "></button></td>
Here Is My Script That Handle The Process
function hapus_gambar() {
$data['link'] = $_POST['link'];
$data['id'] = $_POST['user_id'];
$data['mana'] = $_POST['mana'];
$select =$this->db->query('SELECT `gambar` FROM `produk` WHERE `id` = "'.$data['id'].'"');
foreach($select->result_array() as $gambar) {
unlink('./gambar/'.$data['link']);
unlink('./gambar/thumb/'.$data['link']);
}
$query =$this->db->query("DELETE '".$data['mana']."' FROM `produk` WHERE id = '".$data['id']."' ");
echo 'Data Telah Dihapus';
}
I Am Using Codegniter
Thank You

SOLVED
Turns Out Some Of My Query Makes It Error
So I change My Query Little Bit And It Works
Thanks For The Help

Related

Like Post Button in wordpress

I tried to make a Like button for Posts so i found this tutorial with a lot of searching
tutorial : https://www.atomicsmash.co.uk/blog/create-like-post-button-in-wordpress/
but not worked to me !
my code in functions.php
add_action( 'rest_api_init', function () {
register_rest_route( 'example/v2', '/likes/(?P<id>\d+)', array(
'methods' => array('GET','POST'),
'callback' => 'example__like',
) );
});
function example__like( WP_REST_Request $request ) {
// Custom field slug
$field_name = 'postlike';
// Get the current like number for the post
$current_likes = get_field($field_name, $request['id']);
// Add 1 to the existing number
$updated_likes = $current_likes + 1;
// Update the field with a new value on this post
$likes = update_field($field_name, $updated_likes, $request['id']);
return $likes;
}
in ajax
<script type="text/javascript">
$('.like__btn').on('click', function(){
// AJAX call goes to our endpoint url
$.ajax({
url: 'http://192.168.1.8/p-test/wp-json/example/v2/likes/7',
type: 'post',
success: function() {
console.log('works!');
},
error: function() {
console.log('failed!');
}
});
// Change the like number in the HTML to add 1
var updated_likes = parseInt($('.like__number').html()) + 1;
$('.like__number').html(updated_likes);
// Make the button disabled
$(this).attr('disabled', true);
});
</script>
in HTML
<button class="like__btn">
<span class="like__number"><?php the_field('postlike'); ?></span>
</button>
Which part of the tutorial did I make a mistake?
Thanks for any help

Send Table cell value to Servlet Through ajax

I need to post values from a table cell to a servet through Ajax but the response is returning a null. My Javascript is not that strong and I am certainly doing something wrong and need some direction. Below are my JSP and Ajax script:
**My JSP code**
<td><button type="button" class="btn btn-default btn-md" class="itemData" tableData="${item}" onclick="saveData()"><span class="glyphicon glyphicon-save"></span></button> </td>
**My Ajax code**
function saveData(){
$(document).ready(function(){
var dataID = $(this).attr('tableData');
$.ajax({
url : 'DataCtrlServlet',
type: 'Post',
data : dataID,
success : function(responseText) {
$('#submissionSuccessContainer').text(responseText);
}
});
return false;
});
}
Romve return false; and $(document).ready ,also you need to change the data format to pass the parameter correctly
function saveData(){
var dataID = $(this).attr('tableData');
$.ajax({
url : 'DataCtrlServlet',
type: 'Post',
data : {
dataID:dataID
},
success : function(responseText) {
$('#submissionSuccessContainer').text(responseText);
}
});
}

Ajax is not getting results

I just got into ajax and I cannot understand what is wrong about the code?
index.html
<html>
<head>
<title>Ajax call</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
<head>
<body>
<script>
var UserID = 'myname';
var EmailAddress = 'myname#mail.com';
$.ajax({
url: "ajax.php",
type: "get", //send it through get method
data:
{
ajaxid: 4,
UserID: UserID,
EmailAddress: EmailAddress
},
success: function(response)
{
//Do Something
Alert("Sucess");
},
error: function(xhr)
{
//Do Something to handle error
Alert("Failure");
}
});
</script>
</body>
</html>
ajax.php
<?php
foreach ($_GET as $key => $value) {
echo $key . ' => ' . $value . '<br />';
}
?>
After resolving this issue, my main goal is to replace the code in the php file with a function that connects to a SQL Server and retrieve some data using the data sent by the ajax call. I will try this later.
Regards,
Elio Fernandes
Change Alert("smth") to alert("smth")
Then in success function you need to "send" server response to html
success:function(response){
alert('success')
$('body').append(response)
}

Ajax alert after controller's method success

I have a problem that I was stacked on for a while. I am rather new to this stuff so please be patient. I think it is like very simple to solve but I am running circles now. My intention is to make a simple alert that informs that method was completed. As you can see below my first attempt was made with simple string and if statement. I dont want to reload the page any more but now I have a problem that method ends later than alert script starts.
So I have a controller:
[HttpPost]
public ActionResult Executor(LMCMainViewModel model)
{
var curlsExecutor = _curls;
var applicationPurgeCurl = curlsExecutor.GetApplicationCurl(model);
var temporary = model.SuccessExecutionStatus = curlsExecutor.CurlCaller(model.ApplicationList.ApplicationCurl);
var tempListOfApplications = PopulateModel(model);
model.ApplicationList.ListOfApplications = tempListOfApplications.ApplicationList.ListOfApplications;
model.SuccessExecutionStatus = temporary;
return View("ListOfApplications", model);
}
And I have my view:
#model LMC.Models.LMCMainViewModel
#{
ViewData["Title"] = "Liberation";
}
#using (Html.BeginForm("HeaderString", "LMC"))
{
}
#using (Html.BeginForm("Executor", "LMC", FormMethod.Post))
{
<div class="col-sm-2" asp-action="ListOfApplications">
#Html.DropDownListFor(x => x.ApplicationList.ChosenApplication, Model.ApplicationList.ApplicationListItem, new { #id = "DropdownID" })
</div>
<div class="col-sm-2 col-sm-push-5">
#Html.HiddenFor(x => x.ApplicationList.ApplicationListItem)
<input class="btn ctn-success" id="submit" type="submit" value="Submit" />
<button id="submitButtonAjax" type="button" class="btn btn-success">Ajax button</button>
<div class="col-sm-12">
#Html.Label(null, (Model.SuccessExecutionStatus ? "Success" : " "),
new { Style = Model.SuccessExecutionStatus ? "color: green;" : "color: red;" })
</div>
</div>
}
Than I tried to implement many variations of Ajax script but I got so twisted that I can't even post you the best one... One thing I know is that when I remove: #using (Html.BeginForm("Executor", "LMC", FormMethod.Post)) and try to place it into Ajax it simply doesn't work. My intention is to have something that would work like this:
<script type="text/javascript">
$("#submitButtonAjax").on("click", function () {
$.ajax({
type: 'POST',
url: "/LMC/Executor",
success: function () {
alert("Went well");
}
});
</script>
I tried to convert controller method return into Json but it didnt work as well. I would appreciate any advices, where I can read about anything similar(I am aware that there are probably many similar topics, but I wan't able to implement anything that was working into my code, because I find my question one step backwards in comparison to others), or maybe it is that easy and I am missing something and you can just post a solution. Anyway, many thanks for any help.
The ajax call should look like bellow
$( document ).ready(function() {
$("#submitButtonAjax").on("click", function () {
var postData = {
FirstPropertyOfTheModel: ValueForTheFirstProperty,
SecondPropertyOfTheModel: ValueForTheSecondProperty,
};
$.ajax({
type: 'POST',
url: "/LMC/Executor",
data:postData,
success: function () {
alert("Went well");
},
error: function () {
alert("Opssss not working");
}
});
});
});
data is the value for the model of ActionResult method of the controller. The FirstPropertyOfTheModel and SecondPropertyOfTheModel will be replaced by the name of property and assign the corresponding value respectably. At url you can use
'#Url.Action("Executor", "LMC")'
so the ajax will look like
$( document ).ready(function() {
$("#submitButtonAjax").on("click", function () {
var postData = {
FirstPropertyOfTheModel: ValueForTheFirstProperty,
SecondPropertyOfTheModel: ValueForTheSecondProperty,
};
$.ajax({
type: 'POST',
url: '#Url.Action("Executor", "LMC")',
data:postData,
success: function () {
alert("Went well");
},
error: function () {
alert("Opssss not working");
}
});
});
});

Jquery and ajax

I am quite new to JS and JQ so I am ask this basic question. I have code like this:
$(".delete").click(function() {
var commentContainer = $(this).parent();
var id = $(this).attr("id");
var string = 'id='+ id ;
$.ajax({
url: "<?php echo site_url('messages/delete') ?>",
type: "POST",
data: string,
cache: false,
success: function(){
commentContainer.slideUp('1500', function() {$(this).remove();
$('#messages').fadeOut('1000');
});
}
});
return false;
});
For now it is working like it should. But I want to add something more. Div with id #messages contains all messages, and I wish that upon deleting message it fadeout(it is working) and then load new data and present all messages again without the deleted one.
Also, there is a counter that counts unread messages, and I created separate page for ajax purpose, but I don't know how to go on from fadeOut.
This is the code from the seperate page:
<?php if(isset ($messages)) { ?>
<?php foreach ($messages as $msg){ ?>
<div class="col_3">
<?php
if($msg['read'] == 0){ echo 'Status of message: Unreaded';}
elseif($msg['read'] == 1){echo 'Status of message: Readed';}
echo "<p>Message from: $msg[name]</p>";
echo "<p>Sender email: $msg[email]</p>";
echo "<p>Message: <br />$msg[message]</p>"; ?>
Delete message
</div>
I edited code a bit, but still nothing.
$(".delete").click(function() {
var commentContainer = $(this).parent();
var id = $(this).attr("id");
var string = 'id='+ id ;
$.ajax({
url: "<?php echo site_url('messages/delete') ?>",
type: "POST",
data: string,
cache: false,
success: function(){
commentContainer.slideUp('600', function() {$(this).remove();
$('.messages').fadeOut('2000', function(){$(this).remove();
$('#messages').load("<?php echo site_url('messages/show') ?>").show().fadeIn('1000');
});
});
}
});
return false;
});
When I look page source I can see that the content has been changed, but it is not displayed.
I didn't quite understand what you are trying to do but if this is what you mean by saying but I don't know how to go on from fadeOut you can put a callback function to fadeOut
$('#messages').fadeOut('1000', function () {
// Do something here
);

Resources