Jump to content
NICON

after reload use php function to show success

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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.

  • Like 1

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 by Destramic

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.