Undermine2k
Undermine2k

Reputation: 1491

Passing Form data to Controller

So in my Javascript I am using form serialize the output is correct

(issubmit=1&username=tester&password=test&cpassword=test&firstname=sdfds&lastname=fsdfdsf&gender=&email=pie%40aol.com&phone=&address=&phone=&address=)

When I try to pass it to my controller, I get nothing, result = "". What am I doing wrong?

Jquery:

var dataString = $("form").serialize();
    $.ajax({
        url: '<?php echo base_url();?>index.php/welcome/submit_form/',
        async: false,
        type: 'POST',
        data: dataString,
        dataType: 'html',
        success: function(data) {
        $('#ajax-content-container').html(data);
    }
});

Controller:

function submit_form() {
    $data = $this->input->post('dataString');
    echo "<p>result= ".$data."</p>";
}

Upvotes: 0

Views: 1801

Answers (1)

antony
antony

Reputation: 2893

Your controller seems to be checking for a post parameter named 'dataString'. But this parameter doesn't seem to be being sent in your ajax request. When you serialize your form, it's combining all form fields into one url-encoded string. There's no reference to a 'dataString' parameter in here.

What you might want to do in your form is wrap every input field's name attribute like this:

<input type="text" name="dataString[username]" value="" />
<input type="text" name="dataString[password]" value="" />

After doing this, I believe you should be able to check for a parameter called "dataString" in your controller. It should be an array containing all the fields in key/value format. E.g.

function submit_form() {
    $data = $this->input->post('dataString');
    echo var_dump($data);
}

Upvotes: 2

Related Questions