Welcome Guest, Not a member yet? Register   Sign In
Multiple form submission
#1

[eluser]nickifrandsen[/eluser]
Hi there fellow CodeIgniter peeps Smile

This is properly quite easy, but i'm stuck, trying to figure out how i can submit multiple forms based on checkboxes and one submit button.

I want to add every form that's checked to the db, but i only want to press submit once...

Anyone who can help Smile I'll appreciate it Smile

Thanks
#2

[eluser]summery[/eluser]
Hi,

There are two ways.

1 - Put your <form> </form> tags around ALL of your different "forms" on the page - that way one submit button will submit everything.

2 - Use javascript to submit all the forms when the submit button is pressed, like this:

Code:
<form name="form1" action="handle-data.php">
  <input name="form1-field" type="text" />
</form>

<form name="form2" action="handle-data.php">
  <input name="form2-field" type="text" />
<a href="[removed] submitform()">Submit</a>
&lt;/form&gt;

// Open script tags
function submitform()
{
  document.form1.submit();
  document.form2.submit();
  return false;
}
// End script tags
#3

[eluser]nickifrandsen[/eluser]
You got any idea how it would be using the codeigniter syntax at the moment my code look like this....

The foreach is because the default value are retrieve from an xml document.

Code:
&lt;?php echo form_open('admin/movies/add'); ?&gt;
&lt;?php foreach($xml->movies->movie as $row) : ?&gt;
<br>
    &lt;?php echo form_label('Movie ID' , 'movie_id'); ?&gt;
    &lt;?php echo form_input('movie_id' , $row['movie_id']); ?&gt;
<br>
    &lt;?php echo form_label('IMDB ID' , 'imdb_id'); ?&gt;
    &lt;?php echo form_input('imdb_id' , $row['imdb_id']); ?&gt;
<br>    
    &lt;?php echo form_label('Original Titel' , 'title'); ?&gt;
    &lt;?php echo form_input('title' , $row->original_title); ?&gt;
<br>    
    &lt;?php echo form_label('Produktions År' , 'production_year'); ?&gt;
    &lt;?php echo form_input('production_year' , $row->production_year); ?&gt;
<br>    
    &lt;?php echo form_label('Filmens Officielle Webside' , 'official_website'); ?&gt;
    &lt;?php echo form_input('official_website' , $row->official_website); ?&gt;
<br>    
    &lt;?php echo form_label('Skuespillere' , 'actors'); ?&gt;
    &lt;?php foreach($row->actors->actor as $actor) : ?&gt;
    &lt;?php echo form_input('actors' , $actor); ?&gt;
    &lt;?php endforeach; ?&gt;
<br>    
    &lt;?php echo form_label('Manuskriptforfattere' , 'writers'); ?&gt;
    &lt;?php foreach($row->writers->writer as $writer) : ?&gt;
    &lt;?php echo form_input('writers' , $writer); ?&gt;
    &lt;?php endforeach; ?&gt;
<br>    
    &lt;?php echo form_label('Instruktører' , 'directors'); ?&gt;
    &lt;?php echo form_input('directors'); ?&gt;
<br>    
    &lt;?php echo form_label('Premiere Dato' , 'premiere'); ?&gt;
    &lt;?php echo form_input('premiere'); ?&gt;
<br>    
    &lt;?php echo form_label('Beskrivelse' , 'description'); ?&gt;
    &lt;?php echo form_textarea('description' , $row->regions->region->products->product->description); ?&gt;
<br>
    &lt;?php echo form_label('Thumbnail' , 'thumbnail'); ?&gt;
    &lt;?php echo form_input('thumbnail'); ?&gt;
<br><br><br>
    &lt;?php echo form_label('Tilføj denne film' , 'movie')?&gt;
    &lt;?php echo form_checkbox('movie' , 'accept'); ?&gt;
<hr>
&lt;?php endforeach; ?&gt;
&lt;?php echo form_submit('submit' , 'Tilføj Film'); ?&gt;
&lt;?php echo form_close(); ?&gt;

But this means that it only process the last form...
#4

[eluser]summery[/eluser]
Hi,

It seems to me that your &lt;form&gt; &lt;/form&gt; tags do surround all of your form elements, so all elements should be submitted with the form.

Perhaps the bug is somewhere else? Try to loop through and print the entire $_POST array in the controller that your form is submitted to, before doing anything else. I bet you'll find that all the values you're looking for are there.

Best,
~S
#5

[eluser]nickifrandsen[/eluser]
The function inside my controller looks like this:

Code:
function add()
    {
        $xml_local = 'xml_local/movies.xml';
        $data['xml'] = simplexml_load_file($xml_local);
        if($data)
        {
            $this->movies_model->addMovies($data);
        }
    }

And the model function looks like this. I'm sure it's a little mistake or lack of code somewhere, but I can figure out the bug.

Code:
function addMovies($data)
    {
        $data = array(
                'movie_id' => $this->input->post('movie_id') ,
                'imdb_id' => $this->input->post('imdb_id') ,
                'title' => $this->input->post('title') ,
                'production_year' => $this->input->post('production_year') ,
                'official_website' => $this->input->post('official_website') ,
                'actors' => $this->input->post('actors') ,
                'writers' => $this->input->post('writers') ,
                'directors' => $this->input->post('directors') ,
                'premiere' => $this->input->post('premiere') ,
                 'description' => $this->input->post('description') ,
                'thumbnail' => $this->input->post('thumbnail')
                ) ;
        if($this->input->post('movie') == TRUE)
        {
            $this->db->insert('movies' , $data);
            print 'The selected movies has successfully been added';
        }
    }


By the way thank you very much for the help so far. I really appreciate it Smile
#6

[eluser]danmontgomery[/eluser]
It's only processing the last one because each loop names the inputs the same, so you have X inputs named "thumbnail", that value will get overwritten each time the loop is iterated, and only the last value will remain. You need to indicate that the inputs are arrays:

Code:
echo form_input('movie_id[]', $row['movie_id']);
...
echo form_input('imdb_id[]', $row['imdb_id']);

etc.

When you submit, this will give you an array of values for each input, not just a single value.
#7

[eluser]nickifrandsen[/eluser]
Ahh okay know it start make sense...

But how would you suggest the model to process each input as array. Cause i keeps getting an mysql error that it doesn't decode the arrays. Err 1054...

I'm sorry if I'm not that fast...

And again thanks a lot for the help so far...
#8

[eluser]danmontgomery[/eluser]
I would probably change the structure of the array:

Code:
echo form_input('data[][movie_id]', $row['movie_id']);
...
echo form_input('data[][imdb_id]', $row['imdb_id']);

That way, you could:

Code:
$temp = $this->input->post("data");
foreach($temp as $row) {
    $data = array();
    foreach($row as $field => $value) {
        $data[$field] = $value;
    }
    $this->db->insert('movies' , $data);
}
#9

[eluser]nickifrandsen[/eluser]
It seems that something wrong with the second foreach i keep getting an error about "invalid foreach".
Also it seems I cannot access the individual objects. It's possible to retrieve the $temp array but the actual objects give me an "Uninitialized string offset" error...

Don't quite know how to fix this one Smile

Thank you for your patience with me Wink
#10

[eluser]nickifrandsen[/eluser]
Hey thereSmile Know it's possible to retrieve the individual names both I still can't figure out how to access the object in my model.

The array looks like this.
Code:
Array ( [0] => Array ( [movie_id] => 2417 ) )
Array ( [1] => Array ( [imdb_id] => tt1023441 ) )
Array ( [2] => Array ( [title] => Coco Chanel & Igor Stravinsky ) )
.....

And my model looks like this at the moment just as long as i figure out how to access the object so i can pass the on to the db.
Code:
function addMovies($data)
{        
            $temp = $this->input->post('data');
            foreach($temp as $row => $value)
            {
                $data = array();
                $data[$row] = $value;    
                print_r($data);
                print '<br>';
            }
}




Theme © iAndrew 2016 - Forum software by © MyBB