NICON Posted January 12, 2017 Share Posted January 12, 2017 I have just begun dabbling with javascript as a hobby and have a very crude understanding of php so please bare with me, I am a noob! I am using a javascript onclick button to reference the function shown below. function deleteReply(fpid) { $.post("ajax_forumdelete.php", {'reply': fpid}, function (d) { }); location.reload(); } When the button is pressed and the function executes it does as it should. It deletes the forum reply post as its told and the page reloads and the post is gone! But without some sort of explanation, success message or something telling me that it did what its intended is very unsatisfying. I read around and found that an additional function is needed to give me a little message. success : function() { var x="Success"; alert(x); } Looks simple enough and one would think should be easy to incorporate but when I add it in every way shape or form I cannot get it to work. The original function wont even work after adding this. Below is basically what I am wanting to do but not quite sure how to go about it. function deleteReply(fpid) { $.post("ajax_forumdelete.php", {'reply': fpid}, function (d) { }); success : function() { var x="<?php $mtg->success("Reply has been deleted."); ?>"; alert(x); location.reload(); } } I want to use the php $mtg->success() function to echo the message in the pre defined output location and format and want to output the message after the reload... Any ideas on how to go about this? Quote Link to comment https://forums.phpfreaks.com/topic/302914-after-reload-use-php-function-to-show-success/ Share on other sites More sharing options...
benanamen Posted January 12, 2017 Share Posted January 12, 2017 Javascript question in the Php forum? Quote Link to comment https://forums.phpfreaks.com/topic/302914-after-reload-use-php-function-to-show-success/#findComment-1541303 Share on other sites More sharing options...
Jacques1 Posted January 12, 2017 Share Posted January 12, 2017 It doesn't make sense to use Ajax and then reload the page. All that gives you is a bloated reinvention of plain old forms. The point of Ajax is to not reload the page. Regarding the concrete issue, you should read the documentation. You can't just randomly add a “success:” to your code. The third parameter of the post() method is the success function. 1 Quote Link to comment https://forums.phpfreaks.com/topic/302914-after-reload-use-php-function-to-show-success/#findComment-1541304 Share on other sites More sharing options...
NICON Posted January 12, 2017 Author Share Posted January 12, 2017 Javascript question in the Php forum? Oh thats my fault I had the other forum open in another window and clicked on this tab by accident. Should I repost or can it be moved? Quote Link to comment https://forums.phpfreaks.com/topic/302914-after-reload-use-php-function-to-show-success/#findComment-1541306 Share on other sites More sharing options...
NICON Posted January 12, 2017 Author Share Posted January 12, 2017 It doesn't make sense to use Ajax and then reload the page. All that gives you is a bloated reinvention of plain old forms. The point of Ajax is to not reload the page. Regarding the concrete issue, you should read the documentation. You can't just randomly add a “success:” to your code. The third parameter of the post() method is the success function. I was looking for a bit of documentation breaking down the post() method for jQuery thanks so much. So I tried this trying to make sense of what exactly is happening. function deleteReply(fpid) { $.post("ajax_forumdelete.php", {'reply': fpid}, function (d) { alert(d); }); location.reload(); } while keeping the $mtg function in the ajax file and it does as expected. It returns the data from that function in the browsers alert window (not sure if thats exaclty what its called) telling me: domain name says: "$mtg function plain text blah blah" Then asking for OK. When OK is pressed the page refreshes and the data is gone. I will dig deeper on how to show that text being returned in html format in lieu of that alert window. Quote Link to comment https://forums.phpfreaks.com/topic/302914-after-reload-use-php-function-to-show-success/#findComment-1541309 Share on other sites More sharing options...
Jacques1 Posted January 12, 2017 Share Posted January 12, 2017 You cannot put the reload() call after the post() call, because the post() method returns immediately without waiting for the response. Depending on timing conditions, you may very well end up reloading the page before you've even received the data. That's why there's a success callback: Anything that should happen after the request has been successful must go into that function. Quote Link to comment https://forums.phpfreaks.com/topic/302914-after-reload-use-php-function-to-show-success/#findComment-1541310 Share on other sites More sharing options...
Destramic Posted January 13, 2017 Share Posted January 13, 2017 (edited) the script isn't fully functional but something like this...i guess you have a list of news or whatever it is your listing form the database, which you want to be able to delete but also keep the list in view with successful/error messages. jquery $(document).ready(function(){ $('span[data-delete-news-id]').click(function(event) { var data = { id: $(this).data('delete-news-id') csfr: 'token here' }; $.ajax({ url: 'http://domain/delete', type: 'POST', data: data, dataType: 'text', success: function(result) { //check result and remove for list }, error: function(jqXHR, satus, error){ console.log(status, error); } }); }); }); html news article - php is the best - <span data-delete-news-id="5">delete</span> no need for a form or to reload page...hope this helps...also you going to want to look at stopping cross site forgery attacks hence the csfr: 'token here' parameter Edited January 13, 2017 by Destramic Quote Link to comment https://forums.phpfreaks.com/topic/302914-after-reload-use-php-function-to-show-success/#findComment-1541345 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.