Solved

Javascript callback

Posted on 2013-11-20
8
288 Views
Last Modified: 2013-12-10
Hi

I know this is quite a basic question for anyone who has a good understanding of JavaScript.. I am playing catch up !

so basically.. call backs..

I have a form. The user clicks to edit or add a record. When he clicks update, I am displaying a message confirming the update. I then put them back to the main record list.

My idea was to show the message and then a call back to return to the list. I have got it a little backwards and it's basically showing at the same time..

Here's the code.. Thanks in advance

Main Form
  $.post($("#updateClientform").attr("action"),$("#updateClientform").serialize(),
    function() {
      fnUpdateSuccess('Client',function() {window.location.href = 'clients.php'});
    }
  );

Open in new window

Functions
function fnUpdateSuccess(fItemName, callback){
  confirmationModal('Information','The '+fItemName+ ' has been succesfully Updated !');
  if (callback) {
    callback();
  }
}
function confirmationModal(fTitle,fBody){
  $('#confirmationModal').modal('show')
  $('#confirmationModal .modal-title').html(fTitle)
  $('#confirmationModal .modal-body').html(fBody)
}

Open in new window

0
Comment
Question by:maddisoncr
  • 4
  • 4
8 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39665303
I took the liberty to format your question. It was near unreadable.

The answer is dependent on how you call the post.

If in a submit button, you need to prevent the form from being submitted.
$("#form").on("submit",function(e) {
  e.preventdefault();
  $.post($("#updateClientform").attr("action"),$("#updateClientform").serialize(),
      function() {
        fnUpdateSuccess('Client',function() {window.location.href = 'clients.php'});
      }
    );
})

Open in new window


However I wonder why you would ever want to change the location if you use ajax to update the server.

If you need to reload the page, simply post the form and load the result
0
 

Author Comment

by:maddisoncr
ID: 39665411
Sorry about the formatting and thank you for your comments, i will have a look now
0
 

Author Comment

by:maddisoncr
ID: 39668577
At present. I show a list of clients. The user then selects a client and a new php file is shown. This isn't through ajax.

When the user edits the client and updates it. I send the user back to the clients form. What i also want to do is show a modal box telling them that the update has been carried out. the user then clicks ok to dismiss.

At the moment, everything i've tried seems to load them both at the same time.

Hope that helps
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39668755
$.post($("#updateClientform IS Ajax!
0
 

Author Comment

by:maddisoncr
ID: 39669095
I meant that I am not loading the new page through the ajax load.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39669444
But you are updating something using Ajax, then you load a page using window.location

If you instead just posted the form to the server and returned the new page, you would not have any issues
0
 

Author Comment

by:maddisoncr
ID: 39670047
i understand what you are saying..

when i update the client.. i use a php file called updateclient.php. so is it a case of just using a header to load clients.php ?
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 39670224
Yes, a location header is enough
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
function parameter and using that as array 15 23
JQuery Search Filter 2 39
How to control cache of some js files ? 7 41
How to make google charts responsive 4 7
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question