0

I'm trying to make a set of JavaScript functions that will allow me to load and submit forms dynamically. I have managed to create a working function that loads the form into the page, but I cannot get the submit function to work correctly. What happens is, when I click submit, the form that had loaded in already (using JavaScript load function) disappears and the original content reappears. Also the alert in the submit function does not appear.

I have looked all over this site and found similar issues, but none that load the form in using AJAX then submit using AJAX, all of the others already have the form loaded.

Any help would be much appreciated, I am very new to using AJAX.

Form

echo $Error;
echo '<form id="login" method="post">';
echo '<input type="test" name="Email" placeholder="[email protected]"';
if(isset($Email)) echo "value = ".$Email;
echo '      />'."<br />";
echo '<input type="password" name="Pass" placeholder="Password" />'."<br />";
echo '<input type="submit" value = "Login"/>'."<br />";
echo '</form>';

Load page Function

function Page(url,output)
{
    var Request = '';
    if (window.XMLHttpRequest)
    {
        Request=new XMLHttpRequest();
    }
    else
    {
        Request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    Request.onreadystatechange=function()
    {
        if (Request.readyState==4 && Request.status==200)
        {
            document.getElementById(output).innerHTML = Request.responseText;
        }
    }
    Request.open("GET",url,true);
    Request.send();
}

Submit function

$(function () {
$('form#login').on('submit', function(e) {
    alert('Found');
    $.ajax({
        type: 'post',
        url: 'php/login.php',
        data: $(this).serialize(),
        success: function (complete) {
            document.getElementById('output').innerHTML = complete;
        }
    });
    e.preventDefault();
  });
});  
5
  • Is the alert working.? Is there any error in the console.? Commented Dec 19, 2013 at 11:19
  • if you're using e.preventDefault, does this not stop the on submit from working? Commented Dec 19, 2013 at 11:21
  • No it does not alert anything, forgot to mention that. Commented Dec 19, 2013 at 11:22
  • @AndyHolmes Yes, it does, but that's what they want. You don't want to both submit via an AJAX request and a regular form submission. Commented Dec 19, 2013 at 11:23
  • @AnthonyGrist got it :) Commented Dec 19, 2013 at 11:24

1 Answer 1

2

If you're loading the form using AJAX you'll need a delegated event handler:

$(document).on('submit', 'form#login', function(event) {
    // your AJAX form submission code here
});

When your usage of .on() executes it's looking for that element to exist at that point, but it doesn't, which means you don't end up with any event handler being bound.

There's also a typo in your server-side code for the form: <input type="test" That should likely be type="text"

Full code:

$(function () {
    $(document). on('submit', 'form#login', function(e) {
        alert('Found');
        $.ajax({
            type: 'post',
            url: 'php/login.php',
            data: $(this).serialize(),
            success: function (complete) {
                document.getElementById('output').innerHTML = complete;
            }
        });
        e.preventDefault();
    });
});  
Sign up to request clarification or add additional context in comments.

2 Comments

Im not sure what you mean, where should i insert the above section.
@user2131323 You'd use it instead of the $('form#login').on('submit', function(e) {...}); you currently have; I've edited my answer to include the full code, just switch one out for the other.

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.