Welcome Guest, Not a member yet? Register   Sign In
Two forms on one page
#1

[eluser]Bramme[/eluser]
Hey all

I have the following situation:
I have a page with a drop down list to select an item, after the user has selected an item the page is reloaded and a second form is added (unique to the selected page) with a textarea to add stuff to the selected item.

I have validation for my textarea (trim|required), but the problem is: when I submit the first form (selecting the item from the dropdown), the validation for the textarea gets triggered and shows an error. If I enter something and submit the textarea, everything works.

Is there any way of preventing this early triggering?
#2

[eluser]TheFuzzy0ne[/eluser]
Give your submit buttons different names, and the validate the form that's being submitted:
Code:
if ($this->input->post('form1'))
{
    // Set the validation rules for the first form
    if ($this->form_validation->run())
    {
        // Do something
    }
}
if ($this->input->post('form2'))
{
    // Set the validation rules for the second form
    if ($this->form_validation->run())
    {
        // Do something
    }
}
#3

[eluser]Bramme[/eluser]
Very clever solution, so simple, but didn't think of it myself. Works like a charm! Thanks!
#4

[eluser]jedd[/eluser]
Fuzzy - curiously I was about to work through the same problem the day you posted that hint, so I took it and played with it a bit. I thought there might be a problem with having a variable submit / form name, insofar as I'll be having a handful of forms on some pages, and thought they'd fit better in a switch structure than a bunch of if's. My question is whether you went down this path before, and discounted it for some reason? I've used a hidden field in each form, and test on that, as below. I can see pros and cons but since it's all new to me ...

View snippet
Code:
// Form: search on varietal name
    echo form_open('organism/search');
    echo "Search for variety:   ";
    echo form_input('search_var');
    echo " ";
    echo form_hidden('search_type', 'var');
    echo form_submit('submit_var', 'Go Looking!');
    echo form_close ();
    
    echo "<hr>";

    // Form: search on genus / species
    echo form_open('organism/search');
    echo "Search on scientific name (enter genus and species): &nbsp; ";
    echo form_input('search_sci');
    echo "&nbsp;";
    echo form_hidden('search_type', 'sci');
    echo form_submit('submit_sci', 'Go Looking!');
    echo form_close ();


Controller Organism, function search() snippet
Code:
switch ($this->input->post('search_type'))  {
            case "sci" :    echo "Searching on scientific name";
                            break;
            case "cn" :     echo "Searching on common-name";
                            break;
            case "var" :    echo "Searching on varietal name";
                            break;
            }
#5

[eluser]TheFuzzy0ne[/eluser]
There's no reason I chose not to use a switch other than personal preference. I just feel that if statments are cleaner for larger blocks of code that contain further logic. I tend to use switches for more simple blocks of code (like the above for example).




Theme © iAndrew 2016 - Forum software by © MyBB