0

here is the scenario working in CodeIgniter: ( I am sorry if the Question format is not the right one, this is my fist post here.)

  1. I have large form to be submited and it is submiting OK with ajax, after the validation.
  2. In my controller i am passing the username in the view if the user is logged in, and it is placed in a hidden input field in the form. If the user is not logged in the result is

<input type="hidden" id="loggedin" name="username" value="" />

3.in my javascript i check if the form is valid() and if the username!=="" and then i am posting the form with ajax , else i am popping a dialog with the login page. The form submition is working fine when a user is logged in.

The problem is that i cannot figure out how to post the form after the user is logged in in the login dialog.

here comes the code :

    $('#MyForm').submit(function(event){
     event.preventDefault();


     var username=document.getElementById('loggedin').value;
     if($('#MyForm').valid() && username!=="" ){
     var serializeddata=$("#MyForm").serialize();



      $.ajax({
    type: "POST",
    url: "formsubmition", // The urls are in CI format
    contentType: "application/x-www-form-urlencoded;charset=UTF-8",

    data: serializeddata,
    dataType: "html",
    success: function(data) {
        $('#message_ajax_anaz').html(data);
                    }
                })




     }else if($('#MyForm').valid() && username==""){

      $("#loginpop").load("login").dialog();


     } 
     });

the login page ajax code:

$('#login').submit(function(event){
     event.preventDefault();

     var dataser=$('#login').serialize();
      $.ajax({
    type: "POST",
    url: "login",
    contentType: "application/x-www-form-urlencoded;charset=UTF-8",

    data: dataser,
    dataType: "html",
    success: function(data) {
        $('#message_ajax').html(data);
                    }
                })

And the above is working fine in the dialog, i can login normally.

Now How can i return in the $('#MyForm').submit(function with the user now as logged in ,also without having to fill the form again?

Just figured out a way I hope it helps more people.

I initialized dialog outside the submit function :

    $("#loginpop").load("login").dialog({
        autoOpen:false
    })

then in changed my else statement :

    else if($('#MyForm').valid() && username==""){

  $("#loginpop").dialog('open');

And in my login ajax success function i added :

    $("#loggedin").val(data); // Changed the value of the hidden input with the username value on the fly. data contains the username

        $('#MyForm').submit();
        $('#loginpop').dialog('close');

Now everything is working and my form is submiting after the loggin :)

1 Answers1

0

https://stackoverflow.com/a/2276477/753676 would be much easier.

http://api.jquery.com/serialize/

You can store it also with cookie.js (and delete the cookie after submission) and can submit it then directly

Community
  • 1
  • 1