Support the ongoing development of Laravel.io →
Input Database Forms

Hey guys,

I'm trying to save some form fields to my DB that I am creating dynamically with a button using jquery.

When I try to save the values to my database, it gives me this message:

preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

Which is because im trying to save an array apparently. But how do I then save the correct single values to my database?

// Workout controller
$workout->exercise_id = Input::get('exercise_id');
// Create view
    <div class="form-group">
        {{ Form::label('exercise_id', 'Øvelse') }}
        {{ Form::text('exercise_id[][exercise_id]', null, ['class'=>'form-control']) }}
        {{ $errors->first('exercise_id') }}
    </div>

    <button class="add-exercise">Add exercise to workout</button>
// Jquery, if it even matters 
   var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".form-container"); //Fields wrapper
    var add_button      = $(".add-exercise"); //Add button ID

    var x = 1; //initial text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div class="form-group">' +
            '<label name="exercise_id[]">Øvelse</label>' +
            '<input type="text" name="exercise_id[]"/>' +
                '<a href="#" class="remove_field">Remove</a>' +
            '</div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })

Any help is appreciated :)

Last updated 2 years ago.
0

You should be fine if you change the name of the text form field to "exercise_id" instead of 'exercise_id[][exercise_id]'.

Last updated 2 years ago.
0

Hey Elena, thanks for getting back to me!

Yea it does work, but only for my pregenerated input field. How can I do so that my dynamic fields are also saved to the database?

Thanks!

Last updated 2 years ago.
0

The general idea is $workout->update(Input::all()) (http://laravel.com/docs/4.2/queries#updates) , but you'd need to apply some security checks to that.

If you already have the dynamically generated fields (from a pre-defined set, I guess) on the table then you should just set the $fillable property on your model, but if the fields are truly dynamic, added in a separate table, then you should probably implement your own logic.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Reached reached Joined 27 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.