简体   繁体   中英

load form with ajax and submit with jQuery

I have struggled for quite some time now with this but can figure out whats wrong.

I have an index file where I'm loading the contend with ajax (in this case a form stored in add_admin.php). I have a form which loads just perfectly in a div after clicking a menu item(calling the ajax function - this part works). But if I want to submit that form with jQuery afterwords using the

$(".loginform").submit(function(e) {});

it doesn't get called. I'm suspecting the reason is that the form was not present on the page at the time it was loaded. If I move the form directly to the index page, the function works perfectly.

$(document).ready(function () {
$(".loginform").submit(function(e) {
  data = $("#loginform").serialize();
  password = document.getElementById("user_pass").value;
  passtosubmit=hex_sha512(password);

  data += "&pass=" + encodeURIComponent(passtosubmit);
  password="";

//$.post("modules/process/process_add_admin.php", data);
//alert(data);return false;
$.ajax({
  type: "POST",
      url: "/modules/process/process_add_admin.php",
  data: data,
  success: function() {
    $('#main_panel_container').html("<div id='message'></div>");
    $('#message').html("<h2>Contact Form Submitted!</h2>")
    .append("<p>We will be in touch soon.</p>")
    .hide()
    .fadeIn(1500, function() {
    $('#message').append("<img id='checkmark' src='images/check.png'/>");
  });
 }
});
return false; 
  });
});

The form to submit

add_admin.php

<div id="contact_form">
<form name="loginform" class="loginform" id="loginform" action="#" method="post">
            <label><strong>Username</strong></label><input type="text" name="username" id="user_login"  size="28" class="input" />
            <br />
            <label><strong>Password</strong></label><input type="password" name="p" id="user_pass"  size="28" class="input"/>
            <br />
            <label><strong>E-mail</strong></label><input type="text" name="email" id="user_email"  size="28" class="email" />
            <br />
            <label><strong>First Name</strong></label><input type="text" name="fname" id="user_fname"  size="28" class="input" />
            <br />  
            <label><strong>Last Name</strong></label><input type="text" name="lname" id="user_lname"  size="28" class="input" />
            <br />  
            <input id="save" class="addbutton" type="submit" value="Add" onclick=""/>
</form>
</div>

can anyone please advise?

Thanks

Here's what I normally do, add a onSubmit event in the form tag

<form name="loginform" class="loginform" id="loginform" action="#" method="post" onSubmit="return addContent('loginform');">

and then with javascript

 function addContent(frm) {
     //anything you wanna do before you post

     $.post(
            url,
            $('#' + frm).serialize(),
            function (data) {
                result = data;
            }
          )
          .success(function() {
            //add your success proccesses
          })
          .complete(function() { 

          })
          .error(function() {
               alert('An error has occurred.');
          });      

     return false;// this stops the form from actually posting
 }

Use this code:

<div id="contact_form">
<form name="loginform" class="loginform" id="loginform" action="javascript:add_admin();" method="post">
            <label><strong>Username</strong></label><input type="text" name="username" id="user_login"  size="28" class="input" />
            <br />
            <label><strong>Password</strong></label><input type="password" name="p" id="user_pass"  size="28" class="input"/>
            <br />
            <label><strong>E-mail</strong></label><input type="text" name="email" id="user_email"  size="28" class="email" />
            <br />
            <label><strong>First Name</strong></label><input type="text" name="fname" id="user_fname"  size="28" class="input" />
            <br />  
            <label><strong>Last Name</strong></label><input type="text" name="lname" id="user_lname"  size="28" class="input" />
            <br />  
            <input id="save" class="addbutton" type="submit" value="Add"/>
</form>
</div>

You have to define a function on javascript use this javascript code

function add_admin(){
  data = $("#loginform").serialize();
  password = document.getElementById("user_pass").value;
  passtosubmit=hex_sha512(password);

  data += "&pass=" + encodeURIComponent(passtosubmit);
  password="";

    //$.post("modules/process/process_add_admin.php", data);
     //alert(data);return false;
      $.ajax({
        type: "POST",
        url: "/modules/process/process_add_admin.php",
        data: data,
        success: function() {
          $('#main_panel_container').html("<div id='message'></div>");
          $('#message').html("<h2>Contact Form Submitted!</h2>")
          .append("<p>We will be in touch soon.</p>")
          .hide()
          .fadeIn(1500, function() {
            $('#message').append("<img id='checkmark' src='images/check.png' />");
          });
        }
      });
      return false;
}

Thanks!

I was having the same issue when I ran across this thread. This solution did not quite work for what I needed to do, but what I wound up doing is just calling my page initiate function again after I loaded the AJAX form. This added the new AJAX form to the event calls and it worked perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM