Welcome Guest, Not a member yet? Register   Sign In
Submitting an Unknown Number of Field Elements
#1

[eluser]JoeLongstreet[/eluser]
I have a form that uses jquery to build up a certain number of input fields. When the form is submitted, I need a new row in my database for each input field. Right now I have this:

Controller:
Code:
$i = 0;
            foreach($this->input->post('email') as $email) {
                $email[i];
                $i++;
            }
            $data = array($email);
            $this->load->model('schedule_model');
            $this->schedule_model->schedule_meeting($data);
            $this->load->view('success');

I'm new to CodeIgniter and I'm definitely open to any suggestions to make this better. Currently it's generating this error: "Invalid argument supplied for foreach()"

Thanks for any help you can provide,

Joe
#2

[eluser]jedd[/eluser]
[quote author="JoeLongstreet" date="1260931137"]
I have a form that uses jquery to build up a certain number of input fields.
[/quote]

Input field names need to be unique - it looks like you're assuming you can have multiple fields with the same name.

Once you resolve this, you may want to read the one question in the Form section of the [url="/wiki/FAQ"]FAQ[/url]
#3

[eluser]JoeLongstreet[/eluser]
Thank you very much, FAQ helped me out a lot and I got the problem solved. Now I'm not sure how to put all of my results into a database.

Updated controller, which works well:

Code:
foreach($_POST['email'] as $key=>$value) {
$email_array[$key] = $this->input->post($key);
            }
$this->load->model('schedule_model');
$this->schedule_model->schedule_meeting($email_array);

View (sample):
Code:
<input type='text' name='email[1]' />
<input type='text' name='email[2]' />
<input type='text' name='email[3]' />

I'm trying to insert all of my input values using
Code:
$this->db->insert('mytable', $data);
but I'm not able to make it work. Is this the best way to do it? I probably have to parse through the data somehow first, but I'm unclear on how to do that.

Any help is much appreciated. Thank you again.

Joe
#4

[eluser]jedd[/eluser]
[quote author="JoeLongstreet" date="1260938443"]
Code:
<input type='text' name='email[1]' />
<input type='text' name='email[2]' />
<input type='text' name='email[3]' />
[/quote]

Oh, I see what you're doing. Neat.

Quote:Is this the best way to do it? I probably have to parse through the data somehow first, but I'm unclear on how to do that.

It's dependent on your schema - so you might want to share that. I suspect (using my unsettlingly accurate crystal ball) that I'm going to suggest you normalise your schema in a minute.
#5

[eluser]JoeLongstreet[/eluser]
Thank you very much for your help. I've found a way to insert the data I need into my database.

What does 'normalise your schema' mean? Right now this is just a test application so the columns in my database are id and guest

I'm still building on this same problem, but how would I test multiple input fields for validation. Right now, I have the following in my controller. It kind of works - only if all of the input fields are blank.

Controller:
Code:
foreach($_POST['email'] as $key=>$value) {
            $this->input->xss_clean($_POST['email']);
            $this->form_validation->set_rules($_POST['email'], 'Email', 'required|valid_email');
        }
        
        if($this->form_validation->run()== FALSE) {
            $this->index();
        } else {
            $this->load->model('schedule_model');
            $this->schedule_model->schedule_meeting($_POST['email']);
        }

Model:
Code:
foreach($data as $key=>$value) {
        $this->db->insert('mytable', array('guest' => $value));
        }

View is the same.

Thanks again.

Joe
#6

[eluser]jedd[/eluser]
[quote author="JoeLongstreet" date="1261002898"]
What does 'normalise your schema' mean?
[/quote]

[url="http://tinyurl.com/yjjdvla"]http://tinyurl.com/yjjdvla[/url]


Quote:Right now this is just a test application so the columns in my database are id and guest

How many tables do you have? Or rather, how are you storing multiple email addresses per person?

Quote:I'm still building on this same problem, but how would I test multiple input fields for validation.

It's not that there are multiple, but that they are unknown numbers of these items, albeit with predictable names.

I have no idea - but I'd guess one way you could do it would be to loop through a ->set_rules() call, passing the email[x] parameters that you had extracted from $post already.

There may well be a neater way though.

What do you mean by 'it only works if the input fields are blank' ?
#7

[eluser]JoeLongstreet[/eluser]
Ha ha. Fine, I deserved the lmgtfy.

The application stores each email address on a new row. At this point it's not necessary for these email addresses to be related.

Tried to cycle through the items with something like this -
Code:
foreach($_POST['email'] as $item) {
                $this->input->xss_clean($item);
                $this->form_validation->set_rules($item, 'Email', 'required|valid_email');
            }

but no dice. I'm really new to php too.

Thanks,

Joe
#8

[eluser]intractve[/eluser]
Hi,
Correct me if I am reading you wrong,
you wish to collect x number of email addresses from your vistors and insert them into the database each as a separate row,

Database Schema is table name:emails (id, guest)

you have already got the view part right, except you do not need to number your array in the HTML
Code:
<input type='text' name='email[]' />
<input type='text' name='email[]' />
<input type='text' name='email[]' />
<in......
That will still work (since its undetermined its best to not number it, just a tip)

Model (model_email.php)
Code:
class Model_email extends Model
{
   function Model_email()
   {
     parent::Model();
   }

   function insert_email($email)
   {
     $this->db->set('guest',$email);
     $this->db->insert('emails');
   }
}
In your controller
Code:
$this->load->model('model_email');
$emails = $this->input->post('email');

foreach($emails as $email)
{
$this->model_email->insert_email($email);
}

I am not validating the emails you should do that too. and enabling Global XSS in config.php is a good idea too(if you have a lot of form interaction on your site).
#9

[eluser]JoeLongstreet[/eluser]
Nice! Thanks a lot for your help. I didn't realize that I could use just 'email[]' in my view instead of numbering them. This allows for much easier validation. Works perfectly now.

Thanks for everyone's help!

Joe




Theme © iAndrew 2016 - Forum software by © MyBB