How do I refresh a page following JQuery dialogue form post and display response.

Posted on 2016-10-18
Medium Priority
Last Modified: 2016-10-18
I have the following JQuerty dialogue which posts form data to the processing page "save_applicantnote.php" and retrieves  
 the response which is then assigned to the <p> tag with id errors.

This works fine but I can't get the page to refresh and display the response. It either displays the response or refreshes but not both.  Using location.reload(true); reloads the page but then I loose my response from the post.

Is there anyway to reload the page and display any error response returned from the post?

Thank you

      $( "#dialog-window" ).dialog({

        autoOpen: true,
        maxHeight: 900,
        width: 550,
        height: 900,
        draggable: true,
        modal: true,
        buttons: {
            "Save": function () { 
             $.post("save_applicantnote.php", $("#mainForm").serialize(), function(response)


            "Cancel": function () {

Open in new window

Question by:EICT
  • 4
  • 2
LVL 62

Expert Comment

by:Julian Hansen
ID: 41848105
You can't refresh and display the response. As soon as you refresh any response from the server is lost.

Why do you want to refresh? Either you are doing AJAX or you are doing a complete Post back - why do you want to mix them?

Theoretically you could get the response - append it to the page URL and refresh the page and have either the server render out the page or use javascript on the page to pull the value from the URL - this is possible but not really practical.

The principle with AJAX is you submit data to the server while keeping the same page - you then update the page (or parts of it) with data the server sends back as part of the AJAX call.

Can you explain why you want to mix the two approaches?

Author Comment

ID: 41848157
Hi Julian,

I have a page which displays journal notes retrieved from a MYSQL database. At the top of the page is a "Add Note" button which when clicked displayed a modal dialogue box form for users to add a new note.  After clicking save the new note is added to the database.  The dialogue form is part of the same page code and the displayed notes but hidden until the dialogue box is opened.

Basically, after the user clicks "Save" I wanted the page to refresh so the user can see the new note added and for a message to pop up saving "new note added successfully"

The "New note added successfully" is a response from the processing page. In the event of an error an error response is returned.        

Can I not show the new note and also have a message? perhaps I could display the message directly on the page rather than collecting is a response?

LVL 62

Expert Comment

by:Julian Hansen
ID: 41848193
The standard ways of accomplishing what you want to do is

You return the list of notes from the AJAX request and re-populate the list
You append the new note with JavaScript

You can return multiple values from your Server request using JSON

Option 1 above
Send your data to the server with AJAX
Server script processes and adds to the databse
Server script requests list of notes from the server as an array
Then either
On the server render out the list elements with the data retrieved above and send back the already rendered HTML which you can then replace into page
Send back a JSON encoded array and use JavaScript to create each of the list entries for each note

Which you choose is up to you

Option 2
Send the note to the server
Server adds note to database and sends back a status (true / false)
Based on the return the AJAX call back you add the new item to the list leaving the other items intact.

I will try and put some code together to show how it is done.
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

LVL 62

Accepted Solution

Julian Hansen earned 2000 total points
ID: 41848259
Sample code
    <h2>List items</h2>
    <ul id="items"></ul>
    <button class="add-item btn btn-primary" id="option1-server" data-type="serverRender" data-toggle="modal" data-target="#NewItem">Option 1 (Server Render)</button>
    <button class="add-item btn btn-success" id="option1-browser" data-type="serverJSON" data-toggle="modal" data-target="#NewItem">Option 1 (Browser Render)</button>
    <button class="add-item btn btn-default" id="option2-local" data-type="localInsert" data-toggle="modal" data-target="#NewItem">Option 2 (Local Insert)</button>

Open in new window

<div class="modal fade" id="NewItem" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">New Item</h4>
      <div class="modal-body">
        <form id="NewItemForm">
      <label for="note" class="control-label">Note</label>
      <textarea class="form-control" id="newitemdata" name="newitemdata"></textarea>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary" id="SaveItem">Save changes</button>

Open in new window

$(function() {
  // Stores the button that was clicked
  var target;
  // initialise the list
    url: 't1729.php',
    data: {action: 'serverRender'},
    dataType: 'JSON',
    type: 'POST'
  }).done(function(resp) {
  // Set which button was clicked so we know
  // method to use
  $('#NewItem').on('show.bs.modal', function(e) {
    target = e.relatedTarget;
  // Called when save button is clicked
  $('#SaveItem').click(function() {
  // Hide the modal
    // Serialize the data from the form
    var data = $('#NewItemForm').serialize();
    // Add the action based on the value
    // we saved on form show
    var action = $(target).data('type');
    // Save the item we are adding
    var newitemdata = $('#newitemdata').val();
    // Clear the textarea for the next time
    // Add the action to the query
    data += '&action=' + action;
    // AJAX the data to the script
      url: 't1729.php',
      data: data,
      dataType: 'JSON',
      type: 'POST'
    }).done(function(resp) {
      if (resp) {
        // Server Render so just replace the 
        // html in the list with the server
        // rendered html
        if (resp.action == 'serverRender') {
        // JSON return - run through
        // the items in the JSON, dynamically
        // create an <li> and append it to the
        // list
        else if (resp.action == 'serverJSON') {
          for(var i in resp.items) {
        // None of the above so add the saved value to the list
        else {

Open in new window

Working sample here

Author Closing Comment

ID: 41848477
Thank you Julian this is brilliant. Thanks for your time on this, excellent solution.
LVL 62

Expert Comment

by:Julian Hansen
ID: 41848506
You are most welcome.

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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 title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
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)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

607 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