Welcome Guest, Not a member yet? Register   Sign In
jquery.serialize() codeigniter does not get value
#1

[eluser]audunfr[/eluser]
Hello.

I have tried so many things now that i cant remember all.
I found the following php code that is in the controller on google for normal php.

My question is how can i get the info from a form into codeigniter with jquery serialize?

code:

Code:
$(document).ready(function(){


$(".error").hide();
$("#submit").click(function(){
    $(".error").hide();
    var firstname = $("input#firstname").val();
    if (firstname == "")
    {
        $("label#firstname_error").show("fast");
        $("input#firstname").focus();
        return false;
    }
    var lastname = $("input#lastname").val();
    if(lastname == "")
    {
        $("label#lastname_error").show("fast");
        $("input#lastname").focus();
        return false;
    }
    
    var dataString = $("form").serialize();
    
    $.ajax({
        type: "POST",
        url: "/mob/addNewUserAction",
        
        data: dataString,
        success: function()
        {
            var org = $("#formContent").html()
            $("#formContent").html("<div id='message'></div>");
            $("#message").html("<h2>User was createt with success!</h2>")
            .append("<img id='checkmark' src='/images/open.png' />")
            .append("<p class='links'><a href='/mob/newUser'>Create a new user</a> | <a href='/mob'>Frontpage</a>")
            .hide()
            .fadeIn(1500);
            
        }
    });
    return false;    
});    
});

The View:
Code:
<div id="formContent">
&lt;form action="" name="form" id="form" class="niceform"&gt;
    <fieldset>
        <legend>Personal Info</legend>
        <dl>
            <dt><label for="firstname">Firstname:</label></dt>
            <dd>&lt;input type="text" name="firstname" id="firstname" size="32" maxlength="128" /&gt;&lt;/dd>
            <dt><label for="firstname" class="error" id="firstname_error" style="color:red; width: 200px;">This field is required!</label></dt>
            </dl>
        <dl>
            <dt><label for="lastname">Lastname:</label></dt>
            <dd>&lt;input type="text" name="lastname" id="lastname" size="32" maxlength="32" /&gt;&lt;/dd>
        <dt><label for="lastname" class="error" id="lastname_error" style="color:red;">This field is required!</label></dt>
        </dl>
        <dl>
            <dt><label for="location">Location:</label></dt>
            <dd>
                <select size="5" name="location" id="location">
                    <option value="xx">xxx</option>
                    <option value="xx">xx</option>
                    <option value="xx">xx</option>
                    <option value="xx">xx</option>
                    <option value="xxxxx">xx</option>
                    
                </select>
            </dd>
        </dl>
        <dl>
    <dd>
    &lt;input type="submit" name="submit" id="submit" value="Submit"  /&gt;
    </dd>
    </dl>
    </fieldset>
    <fieldset>
        <legend>Add phone to user</legend>
        
        <dl><dt><label for="phoneMaker">Maker:</label></dt>
            <dd><select size="4" name="maker" id="maker">
                &lt;?php
                echo $maker;
                if($maker == FALSE)
                {
                    echo '<option value="0">No maker in DB</option>';
                } else {
                foreach($maker as $row)
                {
                    echo "<option value=" . $row['id'] . ">". $row['maker'] ."</option>";
                }
            }
                ?&gt;
        </select>
        </dd>
        </dl>
    
            <dl><dt><label for="phoneModel">Model:</label></dt>
                <dd><select size="4" name="model" id="model">
                <option value="none">Select maker first</option>
            </select>
            </dd>
            </dl>
            <dl>
                <dt><label for="imei">Imei:</label></dt>
                <dd>&lt;input type="text" name="imei" id="imei" size="32" maxlength="32" /&gt;&lt;/dd>
            </dl>
            <dl>
                <dt><label for="number">Number:</label></dt>
                <dd>&lt;input type="text" name="number" id="number" size="32" maxlength="32" /&gt;&lt;/dd>
            </dl>
                <dl>
                    <dt><label for="extra">Extra:</label></dt>
                    <dd>&lt;input type="text" name="extra" id="extra" size="32" maxlength="32" /&gt;&lt;/dd>
                </dl>
    </fieldset>
    </div>

Controller: I get an offset error, and if i do not get an offset error it does not get anything. I have tried $_REQUEST, $_POST and the codeingiter way €this->input->get_post('form');
Code:
function addNewUserAction()
            {
                
                
                
                
                $fields = explode("&", $this->input->get_post('form'));
                foreach($fields as $field)
                {
                    $field_key_value = explode("=", $field);
                    $key = urldecode($field_key_value[0]);
                    $value = urldecode($field_key_value[1]);
                }
}
Error:
Code:
<p>Severity: Notice</p>
<p>Message:  Undefined offset:  1</p>
<p>Filename: controllers/mob.php</p>
<p>Line Number: 174</p> EDIT: Thats the: $value = urldecode(field_key_value[1]);

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Use of undefined constant field_key_value - assumed 'field_key_value'</p>
<p>Filename: controllers/mob.php</p>
<p>Line Number: 179</p>
I know its my fault but i can't se whats wrong after trying so many times.

Sorry if this is a jquery question, but i dont know...

Thank you very very much.
Best regards
Audun
#2

[eluser]pistolPete[/eluser]
I didn't look through all the code, but you could use parse_str() to parse the parameters.

The error message says that you are missing a dollar sign:
Code:
// missing $ before field_key_value
$value = urldecode(field_key_value[1]);

When you serialize the form, the generated query string will look like this:
Code:
firstname=somename&lastname=somename&...
I guess jquery does not send a variable called "form".

Why don't you use server-side validation? Have a look at the form_validation class: User guide
#3

[eluser]Phil Sturgeon[/eluser]
Just to add to that, instead of splitting the string up yourself you can just use:

Code:
parse_str( $this->input->get_post('form') );

That will return an array with the same key names they had in the query string.
#4

[eluser]audunfr[/eluser]
Thanks so much for the answers :-).

After looking and testing the code you provided and managed to get the values out, I was thinking "This is to hard, so it must be simplere".

I deleted all my code exept the javascript and did it all again.
Now it works with
Code:
$this->input->post(fieldname);

I do not know why its working now, becauce it dident work earlier.

Ok, thanks to both of you.
I am learning for every problem that is showing and thats the way i like it.

You have just teached me some more hehe..

Tanks again.




Theme © iAndrew 2016 - Forum software by © MyBB