0

I've got the following code that I use on my links. This prevents the page from reloading and loads the content from the href tag in a div.

  $("a[rel='right']").click(function(e){
    e.preventDefault();
    pageurl = $(this).attr('href');
    $.ajax({url:pageurl.replace('index.php', 'rightcolumn.php')+'&rel=right',success: function(data){
      $('#WMS_NEW_right').fadeOut(500, function(){ $('#WMS_NEW_right').html(data).fadeIn(1000); }); 
    }
    });  
    if(pageurl!=window.location){
      window.history.pushState({path:pageurl},'',pageurl);
    }
    return false;
  });
});

My Question: I need the use the same concept behind this, except on form submit, it needs to not reload the page, but submit the form only inside a div #WMS_NEW_right. How can I do this? I don't need push state or anything, just need to be able to control that form with class="formrelright" to only reload a div and get the url from the form action. I will also need all data from the form method="POST" on the new page (inside div)

kdjernigan
  • 417
  • 2
  • 7
  • 22

3 Answers3

5

From my understanding, you want to use ajax to post a form without reloading the page during the form submission. So I would consider the following:

$('.formrelright').submit(function(event) {
    event.preventDefault();

    $.ajax({
        url: url,
        type: 'POST',
        data: $(this).serialize(),
        success: function(data) {
            // Whatever you want
        }
    });
});
jrpotter
  • 161
  • 1
0

Either use target and an IFrame, or JQuery to submit the form in the background. The latter is preferable if you want to use the contents of the response.

JQuery post()

Eran Boudjnah
  • 1,228
  • 20
  • 22
  • 1
    How can I setup a jquery post setup using my requirements. I've tried whats listed here http://jsfiddle.net/n73c7/1/ but I think that that is a different method than what you are referring to. I'm not experienced in jquery or javascript, so I'm kinda just using the bits of information I know to play with what I'm trying to accomplish – kdjernigan Dec 28 '13 at 22:37
  • 1
    post() is just a shorthand to the ajax() call you used. It just saves you the part that specifies the type of the request is POST. – Eran Boudjnah Dec 28 '13 at 22:42
  • oh, good to know. The only issue is the page is still completely reloading in my code. Any idea why? I'm trying to avoid the entire page from reloading and just reload the actual #WMS_NEW_right div – kdjernigan Dec 28 '13 at 22:46
  • 1
    Without your HTML it's hard to tell the cause for the form submission. Look here for clues: http://stackoverflow.com/questions/3569072/jquery-cancel-form-submit-using-return-false – Eran Boudjnah Dec 28 '13 at 22:49
  • 1
    yeah I forgot to copy it in. I found the issue. The issue was I am using an include for all content propagated inside #WMS_NEW_right. I was trying to include the jquery code to process the form inside that included page, which I thought would be the correct way of doing this. But, instead, it just wasn't working correctly. I moved that code up to the index.php page which is what is housing the entire page that doesn't change at all and it instantly worked. Thank you for all your help! – kdjernigan Dec 28 '13 at 23:08
0

Maybe try it like this:

HTML:

<!-- Notice there is no 'form' element -->
<div id="myform">
    <input type="text" name="firstName"><br>
    <input type="text" name="lastName"><br>
    <button type="button" id="submit_myform">Submit</button>
</div>

<div id="resultArea"></div>

<!-- the biggest critique about this method might be that
     yes, it is not very semantic.  If you want to use a form,
     just throw in an 'e.preventDefault()' in your jQuery -->

jQuery:

$('#submit_myform').on('click',function() {

    var firstName = $('input[name="firstName"]').val(),
        lastName = $('input[name="lastName"]').val();

    $.ajax({
        type: "POST",
        url: 'form.php',
        data: {
            firstname: firstName,
            lastname: lastName
        },
        //the success function is automatically passed the XHR response
        success: function(data) {
            $('#resultArea').html(data);
        },
    });

});
Josh Beam
  • 19,292
  • 3
  • 45
  • 68
  • I need to be able to do this via `method="POST"`...I don't want to insert the variables into the link itself. I don't mind having a separate div inside current div to update. That is fine. But it needs to be able to submit the form itself. Even if there is a div that gets updated similar to #resultArea – kdjernigan Dec 28 '13 at 22:33
  • Updated my answer. That may work. The parameters are instead passed as an object to the body of the POST request, and the response is returned, in this case, as the argument 'data' in the success method. Check out [jQuery.ajax()](http://api.jquery.com/jquery.ajax/) – Josh Beam Dec 28 '13 at 22:51
  • So basically the same thing I have here: http://jsfiddle.net/n73c7/1/ ...except its not preventing the page from reloading. Even with e.preventDefault – kdjernigan Dec 28 '13 at 22:59
  • Not sure... theoretically, e.preventDefault() should prevent the form from submitting. To troubleshoot: try taking out the form element and doing it the non-semantic way (the way I did the HTML above) and change the url property to whatever URL you need. Just move stuff around and see what happens. – Josh Beam Dec 28 '13 at 23:03
  • 1
    Also, in the jsfiddle, your missing an extra }); to close the click event. – Josh Beam Dec 28 '13 at 23:05
  • 1
    yeah I forgot to copy it in. I found the issue. The issue was I am using an include for all content propagated inside #WMS_NEW_right. I was trying to include the jquery code to process the form inside that included page, which I thought would be the correct way of doing this. But, instead, it just wasn't working correctly. I moved that code up to the index.php page which is what is housing the entire page that doesn't change at all and it instantly worked. Thank you for all your help! – kdjernigan Dec 28 '13 at 23:07