Welcome Guest, Not a member yet? Register   Sign In
Ajax msg display help!
#1

[eluser]debow[/eluser]
Hello, I have the following ajax setup for a registration form and its works for the most part but what I'm trying to clean up is the message on any alert or success that keeps showing the alert plus all code in the same popup. Is there anyway around this? How do I get only the error or success message.

AJAX
Code:
$(document).ready(function() {
     $('#registration_form').submit(function(){
            var userEmail =$('#userEmail').val();
            var userPassword =$('#userPassword').val();            
            if(!userEmail || userEmail == '')
            {
              alert('Please enter a valid email address');
              
              return false;
            }
            if(!userPassword || userPassword == '')
              {
                alert('You must choose a password');
                return false;
              }              
            var form_data = {
                  userEmail: $('#userEmail').val(),
                  userPassword: $('#userPassword').val(),
                  //ajax: '1'  
                };
            $.ajax({
                url: "<?php echo site_url('users/add'); ?>",
                type: 'POST',
                data: form_data,
                success: function(msg) {
                    alert(msg);
                }
              });
      return false;
       });    
});

For the email/password empty filed check it works but when my post is ran my controller does the following

Code:
if ($_POST)
       {
                if ($this->form_validation->run() === TRUE)
                {  
                      $userId = $this->user_model->AddUser($_POST);
                    echo "Registration Successful";
                }
                else
                {
                    echo validation_errors(' ',' ');
                }
        }

My issue is with this message. I only want to see "Registration Successful" not all the html code as well. Attached is a screen shot of what I'm seeing.
#2

[eluser]Dennis Rasmussen[/eluser]
Do you use any kind of template engine/library?
Anyway it seems like you're loading a view AFTER the user is added in your controller, either by a library or in your controller.
#3

[eluser]debow[/eluser]
That was easy enough.

I commented out the following lines and get what I want.
Code:
if ($_POST)
       {
                if ($this->form_validation->run() === TRUE)
                {  
                      $userId = $this->user_model->AddUser($_POST);
                    echo "Registration Successful";
                    
                }
                else
                {
                    echo validation_errors(' ',' ');
                }
        }
        else
        {
          $data['main_content'] = 'home/main_index';
          $this->load->view('includes/template', $data);
        }
        

        //$data['main_content'] = 'home/main_index';
            //$this->load->view('includes/template', $data);
    }


Just have to figure out now how to close the form once I click ok to the "Registration Successfull" popup is displayed.

I'm sure its something simple like adding a close call in the following ajax.
Code:
$.ajax({
                url: "<?php echo site_url('users/add'); ?>",
                type: 'POST',
                data: form_data,
                success: function(msg) {
                    alert(msg);
                }
              });
      return false;
#4

[eluser]Dennis Rasmussen[/eluser]
You're welcome Smile

And for the callback you requested, place it here:
Code:
$.ajax({
                url: "<?php echo site_url('users/add'); ?>",
                type: 'POST',
                data: form_data,
                success: function(msg) {
                    alert(msg);
                    // Anything goes here
                    // Could be something like $('#form').hide();
                    // However I don't know what you mean by "closing the form" :) It oculd be anything
                }
              });
      return false;
#5

[eluser]debow[/eluser]
Thanks again. This is what I needed. It both fun and frustrating leaing php/mysql/ajax and CI all at the same time Smile

Code:
$.ajax({
                url: "<?php echo site_url('users/add'); ?>",
                type: 'POST',
                data: form_data,
                success: function(msg) {
                    alert(msg);
                    $('#simplemodal-container').hide(); //works
                    [removed] = '<?php echo site_url('main'); ?>'; // Members Area
                }
              });
#6

[eluser]Dennis Rasmussen[/eluser]
It is indeed fun.
And the more time you spend on it, the easier it gets Smile




Theme © iAndrew 2016 - Forum software by © MyBB