0

I am using the.load function and I am having some trouble:

<form id="register_frm" method="post" action = "/login/register/"> 
    <fieldset>
        <legend>New User:</legend> 
        <div>
        <label for="id_email">Email</label>
        <input id="id_email" type="text" name="email" maxlength="30" /> 
        </div>      
        <div> 
        <label for="id_conf_email">Confirm Email</label> 
        <input id="id_conf_email" type="text" name="conf_email" maxlength="30" /> 
        </div> 
    </fieldset> 
<input name = "register" type="submit" value="Register" /> 
</form>

I am trying to write a .load script that would display a success method if the 2 email address match.

function register_js() {
    $('#register_frm').load('/register/confirm');
    return false;
    }

$(document).ready(function () {
    ('#register_frm').submit(register_js);
    });
  1. How to change the code so the .load would submit a post request instead of the get?
  2. When I override the register button, how to make the .load submit the email and conf_email field so that I can verify them on the server side?

1 Answer 1

1

You don't want to use load you want to use post.

$(document).ready(function () {
    $('#register_frm').submit(function() {
        var $form = $(this);
        // if you want to get the url from the action use
        // var url = $form.attr('action');
        var url = '/register/confirm';
        $.post( url, $form.serialize(), function(html) {
            $form(html);
        });
        return false;
    });
});

Note if you don't reuse the $form variable as noted in the comments, you can simply use $(this).serialize() in the parameters to post.

Sign up to request clarification or add additional context in comments.

4 Comments

I am using a .load function because I am returning a "success message" that should be displayed instead of the register_frm section of the webpage. Is this possible withe $.post?
@Rami - yes, post can receive html, json, etc. You just need to specify the type if it's not html. If it is, just use the html method on the jQuery object for the form to replace it's contents or use replaceWith to replace the form entirely (probably better, but not shown in my updated example).
@Rami - FWIW, both load and post are simply convenience functions for the ajax method. load is essentially an ajax call using GET semantics where the success callback puts the content in the element to which load is applied. post, obviously, uses POST semantics and leaves it up to you to supply the callback, which can easily insert any new content delivered from the server.
thank you for the insights and the answer, This helped me put things into perspective and better understand how to use jquery tools.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.