0

I'm trying to make an ajax request with jquery/codeigniter. Well, there seems to be a problem. i do get it to work, but it seems like the POST is not sent, for some reason...

Javascript

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

        $.ajax({ // Starter Ajax Call

            method: "POST", 
            url: baseurl + 'profile/statusUpdate', 
            data: $('#statusUpdateForm').serialize(),
            success: function(data) { 
                alert(data);
            }

        });

        return false;

    });

From the codeigniter controller

    if($this->input->is_ajax_request()) {
        echo $this->input->post('profileUpdate');
    }

If i replace the "echo $this->" etc.. with just echo "Hello" i do get an alertbox with "Hello", why don't i get the text from the box?

html

    <div id="statusUpdate">
        <?php echo form_open(base_url() . 'profile/statusUpdate', array('name' => 'statusUpdateForm')); ?>
            <input type="text" value="Hva tenker du på?" name="profileUpdate" id="profileUpdate" onfocus="if(this.value == 'Hva tenker du på?')this.value=''" onblur="if(this.value == '')this.value='Hva tenker du på?'" />
            <input type="submit" value="" name="profileUpdateButton" id="profileUpdateButton" />
        <?php echo form_close(); ?>
    </div>
3
  • have you tried to alert $('#statusUpdateForm').serialize() into the click event handler? Commented Jul 21, 2011 at 20:10
  • What happened when you ran var_dump($_POST)? Should be one of your your default debugging tools: php.net/manual/en/function.var-dump.php Commented Jul 21, 2011 at 21:06
  • if i var_dump i get array(0){} and alert $('#statusUpdateForm').serialize() give me prntscr.com/2ecwh Commented Jul 23, 2011 at 14:49

1 Answer 1

2

Change your html markup to this...

<div id="statusUpdate">                   <!-- Use ID not NAME here |v| -->
    <?php echo form_open(base_url() . 'profile/statusUpdate', array('id' => 'statusUpdateForm')); ?>
        <input type="text" value="Hva tenker du på?" name="profileUpdate" id="profileUpdate" onfocus="if(this.value == 'Hva tenker du på?')this.value=''" onblur="if(this.value == '')this.value='Hva tenker du på?'" />
        <input type="submit" value="" name="profileUpdateButton" id="profileUpdateButton" />
    <?php echo form_close(); ?>
</div>

you were setting the form's name and you were selecting $('#statusUpdateForm') which means the id of statusUpdateForm, not the name attribute...

Sign up to request clarification or add additional context in comments.

Comments

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.