0

So I am trying to submit just an email address using ajax to get people to register and I have no idea why I am getting a 500 internal server error. I am new to ajax calls.

I have tried to follow the following tutorial: http://www.youtube.com/watch?v=TZv5cgua5f0 However I have done as they have said and still if I do a post with values I do not get to the desired controller method. If I do add data to the post then I get an internal server error.

javascript:

$('#email_submit').click(function() 
    {

        var form_data = 
        {
           users_email: $('#users_email_address').val()
        };
        $.ajax
        ({
            url: 'http://localhost/myZone/NewsLetter/submit',
            type: 'POST',
            data: form_data,
            success: function(msg)
            {
                alert(msg);
            }

        });

        return false;
    });

HTML

<div id="email_newsletter_signup" class="ajax_email_block_signup" >
        <h3>Sign up to the newsletter:</h3>
        <?php echo form_error('signup_email','<div id="email_error" class="error">','</div>');?>
        <h3>email: <input id="users_email_address" type="email" name="signup_email" value="<?php echo set_value('signup_email'); ?>" placeholder="Your email"/> </h3>

        <input id="email_submit" type="submit" name="submit"/>
    </div>

contoller

    public function index()
        {
                Assets::add_module_js('newsletter','email.js');
                //If included will be added
                Template::set_block('email_block','email_block');
                Template::render();
        }

    public function submit($email)
            {
                $success = $this->newsletter_model->set_unverified_email($email);
//                if($success === FALSE)
//                {
//                    Template::set_block('newsletter_error','newsletter_error');
//                }
//                else
//                {
//                    Template::set_block('newsletter_success','newsletter_success');
//                }
//                Template::render();
                return;
            }

I have a breakpoint inside the submit and it just wont be hit when I do a post

Thanks

Vova Lando
  • 558
  • 3
  • 15
bubblebath
  • 939
  • 4
  • 18
  • 45
  • Take a step back and simplify your ajax task to narrow down possible errors/issues. For example, try making a simple ajax request on your server to a generic json or xml file to ensure that the request is being executed without a response error. As a next step, have your controller return a static piece of information to ensure that the controller is in fact tied to your associated view. Between those two trials, you should be able to narrow down the issue. Perhaps update your questions once you've done this and I'm sure you'll get a valuable answer pretty quickly. – Jbird Sep 02 '13 at 17:49
  • can you tell me the controller name? check your ajax url follows this format 'http://localhost/CONTROLLER_NAME/FUNCTION_NAME'.. – rAjA Sep 02 '13 at 18:13
  • You first of all have a lot of dead code inside submit, I suggest you to remove it when you create examples to be posted on this website. – hakre Sep 03 '13 at 06:07
  • I have found such a weird issue. Basically what happens is that when the codeigniter stuff is loaded (CodeIgniter.php) the input class fails to load $IN =& load_class('Input', 'core'); Its at this point that everything goes wrong?! – bubblebath Sep 03 '13 at 14:27

2 Answers2

1

Found my solution. Nothing to do with bonfire but with the codeigniter. It was the CSRF token.

Here is an excellent post about sorting the issue:

http://aymsystems.com/ajax-csrf-protection-codeigniter-20

Jubayer Arefin
  • 485
  • 7
  • 17
bubblebath
  • 939
  • 4
  • 18
  • 45
0

add csrf token to data before posting

$.ajax({
            type: "POST",
            url: url,
            data: {'<?php echo $this->security->get_csrf_token_name(); ?>':'<?php echo $this->security->get_csrf_hash(); ?>'}

        })

csrf token needs to be send along every request so it needs to be specified by the above echo statements

Jithu R Jacob
  • 368
  • 2
  • 17