Welcome Guest, Not a member yet? Register   Sign In
multiple submit buttons that execute different processing scripts
#1

[eluser]hidden_within[/eluser]
I have an application form designed for students to apply at a school. Since the form is rather long I would like to give the user the option to save the form or submit it once it has been completed. I have 2 submit buttons and I want to call one function called "save" from the controller and a function called "apply" based on what the user chooses. What I have right now might not be the route I should take. Neither method gets called and the testing method that I have that does get called which is the index function does not output anything either. Any suggestions would be awesome. Thanks in advance.

Here's my controller
Code:
<?php
    class Application extends Controller {
        
        function Application()
        {
            parent::Controller();
            $this->load->model->('application_model');
        }//end application
        
        function index()
        {
            $this->load->view('dts_application');
        }
        function validate()
        {
            //validate the form values
            //validation will be based on whether the user is saving the form or applying
            //must at least have an email address in order to save the form otherwise store nothing
            
        }//end validate
        
        function apply()
        {
            //send the application in for review
            $this->load->view('apply');
        }//end apply
        
        function save()
        {
            //store data to the database
            $this->load->view('save');
        }//end save
    }//end class
?>

Here's my view
Code:
<body>
<?php
  //form attributes    
  //may need to change the class for styling if the class isn't needed then the attribute can be removed
  $app_attr = array('class' => 'some_class','id' => 'dts_form');    
?>
<body>
  <div id="dts_app">
      &lt;?php
        $path = '/application/index';  
    ?&gt;
    <h1>Register</h1>
    &lt;?php echo form_open($path, $app_attr);?&gt;
    <label class="className" for="email">Email:</label>
    &lt;input class="className" type="text" name="email" id="email" value="" size="23" /&gt;
    <label class="className" for="password">Password:</label>
    &lt;input class="className" type="password" name="password" id="password" size="23" /&gt;
    <label class="className" for="conf_password">Confirm Password:</label>
    &lt;input class="className" type="password" name="conf_password" id="conf_password" size="23" /&gt;
    &lt;input type="submit" name="submit" value="APPLY" /&gt;
    &lt;input type="submit" name="submit" value="SAVE" /&gt;
    &lt;/form&gt;
  </div>
  &lt;?php
    switch($_POST['submit'])
    {
        case 'APPLY':
            $path = "/application/apply";
            echo 'apply';
            break;
        
        case 'SAVE':
            $path = "/application/save";
            echo 'save';
            break;
    }
   ?&gt;
&lt;/body&gt;
#2

[eluser]smilie[/eluser]
Hi,

I am missing - based on what does student chooses to click 'apply' or 'save'?
My best guess is that because form is long, he can 'apply' changes from time to time and once done - 'save' it permanently?

I think you may not have 2 submit buttons for same form.
What you could do is 1 form button and in the controller check:
- if value == submit -> perform one action;
- if value != submit -> perform some other action.

tho' not sure how to 'catch' the value of submit :-)

Hope this helps somewhat...

Cheers,
Smilie
#3

[eluser]hidden_within[/eluser]
So i tried performing a check within the controller to check the value of the submit button and execute the associated function based on that value but it still doesn't called either function

The Controller:
Code:
&lt;?php
    class Application extends Controller {
        
        function Application()
        {
            parent::Controller();
            $this->load->model->('application_model');
        }//end application
        
        function index()
        {
            if($this->input->post('submit') == 'APPLY')// && validate())
            {
                $this->application_model->apply();
                //for testing
                echo 'apply';
            }
            else
            {
                $this->application_model->save();
                //for testing
                echo 'save';
            }
        }
        function validate()
        {
            //validate the form values
            //validation will be based on whether the user is saving the form or applying
            //must at least have an email address in order to save the form otherwise store nothing
            
        }//end validate
        

    }//end class
?&gt;

The View:
Code:
&lt;body&gt;
  <div id="dts_app">

    <h1>DTS Application</h1>
    &lt;?php echo form_open('/application/index', $app_attr);
    
    $school_options = array(
                              'NULL'        => 'Choose One',
                              'Sep_11'      => 'September 2011',
                              'April_12'    => 'April 2012',
                              'Sep_12'       => 'September 2012',
                              );

    echo form_dropdown('school', $school_options, 'NULL');
    echo 'Have you previously attended a DTS?';
    echo 'Yes';
    echo form_checkbox('previously_attended', 'n', TRUE);
    echo 'No';
    echo form_checkbox('previously_attended', 'y', FALSE);
    echo 'If yes, when and where?';?&gt;
    
    <label class="className" for="prev_location_date">Date of previous DTS</label>
    &lt;input class="className" type="text" name="prev_location_date" id="prev_location_date" value="" size="23" /&gt;
    
    <label class="className" for="prev_location">Location of previous DTS</label>
    &lt;input class="className" type="text" name="prev_location" id="prev_location" value="" size="23" /&gt;

    <label class="className" for="email">Email:</label>
    &lt;input class="className" type="text" name="email" id="email" value="" size="23" /&gt;
    
    &lt;?php
    echo 'Title: ';
    $title_options = array(
                              'NULL'        => 'Choose One',
                              'Mr.'      => 'Mr.',
                              'Mrs.'    => 'Mrs.',
                              'Ms.'       => 'Ms.',
                              'Miss'    => 'Miss'
                              );

    echo form_dropdown('title', $title_options, 'NULL');?&gt;
    
    <label class="className" for="first_name">First Name:</label>
    &lt;input class="className" type="text" name="first_name" id="first_name" value="" size="23" /&gt;
    <label class="className" for="last_name">Last Name:</label>
    &lt;input class="className" type="text" name="last_name" id="last_name" value="" size="23" /&gt;
    <label class="className" for="nickname">Nickname:</label>
    &lt;input class="className" type="text" name="nickname" id="nickname" value="" size="23" /&gt;

    <h2>Current Address</h2>
    <label class="className" for="cur_street">Street/P.O. Box:</label>
    &lt;input class="className" type="text" name="cur_street_address" id="cur_street_address" value="" size="50"/&gt;
    <label class="className" for="cur_city">City:</label>
    &lt;input class="className" type="text" name="cur_city" id="cur_city" value="" size="23"/&gt;
    <label class="className" for="cur_state">State:</label>
    &lt;input class="className" type="text" name="cur_state" id="cur_state" value="" size="23"/&gt;
    <label class="className" for="cur_zip">Zip:</label>
    &lt;input class="className" type="text" name="cur_zip" id="cur_zip" value="" size="23"/&gt;
    <label class="className" for="cur_country">Country:</label>
    &lt;input class="className" type="text" name="cur_country" id="cur_country" value="" size="23"/&gt;

    <h2>Personal Information</h2>
    <label class="className" for="age">Age:</label>
    &lt;?php numeric_dropdown("age","Choose One", 18, 90, TRUE, FALSE);
    echo 'Birthday';
          $month_options = array(
                              'NULL'        => 'Month',
                              'January'      => 'January',
                              'February'    => 'February',
                              'March'       => 'March',
                              'April'        => 'April',
                              'May'            => 'May',
                              'June'        => 'June',
                              'July'        => 'July',
                              'August'        => 'August',
                              'September'    => 'September',
                              'October'        => 'October',
                              'November'    => 'November',
                              'December'    => 'December'
                              );

    echo form_dropdown('month', $month_options, 'NULL');
    numeric_dropdown("day", "Day", 1, 31, TRUE, FALSE);
    numeric_dropdown("year", "Year", 1992, 1920, TRUE, TRUE);
    
    ?&gt;

    
    &lt;input type="submit" name="submit" value="APPLY" /&gt;
    &lt;input type="submit" name="submit" value="SAVE" /&gt;
    &lt;/form&gt;
  </div>
&lt;/body&gt;
#4

[eluser]hidden_within[/eluser]
ok I was able to figure this out. The solution was quite simple after all.

The Controller:

Code:
&lt;?php
    class Application extends Controller {

        function index()
        {
            $this->load->model('application_model');
            if($this->input->post('submit') == 'Apply')
            {
                //$this->load->view('apply');
                $this->application_model->apply();
            }
            else
            {
                //$this->load->view('save');
                $this->application_model->save();
            }

        }//end index
        function validate()
        {
            //validate the form values
            //validation will be based on whether the user is saving the form or applying
            //must at least have an email address in order to save the form otherwise store nothing
        }//end validate
    }//end class
?&gt;

The View:

Code:
&lt;body&gt;
&lt;?php
  //form attributes    
  //may need to change the class for styling if the class isn't needed then the attribute can be removed
  $app_attr = array('class' => 'some_class','id' => 'dts_form');    
?&gt;
&lt;body&gt;
  <div id="dts_app">

    <h1>DTS Application</h1>
    &lt;?php echo form_open('/application/index', $app_attr);
    
    $school_options = array(
                              'NULL'        => 'Choose One',
                              'Sep_11'      => 'September 2011',
                              'April_12'    => 'April 2012',
                              'Sep_12'       => 'September 2012',
                              );

    echo form_dropdown('school', $school_options, 'NULL');
    echo 'Have you previously attended a DTS?';
    echo 'Yes';
    echo form_checkbox('previously_attended', 'n', TRUE);
    echo 'No';
    echo form_checkbox('previously_attended', 'y', FALSE);
    echo 'If yes, when and where?';?&gt;
    
    <label class="className" for="prev_location_date">Date of previous DTS</label>
    &lt;input class="className" type="text" name="prev_location_date" id="prev_location_date" value="" size="23" /&gt;
    
    <label class="className" for="prev_location">Location of previous DTS</label>
    &lt;input class="className" type="text" name="prev_location" id="prev_location" value="" size="23" /&gt;

    <label class="className" for="email">Email:</label>
    &lt;input class="className" type="text" name="email" id="email" value="" size="23" /&gt;
    
    &lt;?php
    echo 'Title: ';
    $title_options = array(
                              'NULL'        => 'Choose One',
                              'Mr.'      => 'Mr.',
                              'Mrs.'    => 'Mrs.',
                              'Ms.'       => 'Ms.',
                              'Miss'    => 'Miss'
                              );

    echo form_dropdown('title', $title_options, 'NULL');?&gt;
    
    <label class="className" for="first_name">First Name:</label>
    &lt;input class="className" type="text" name="first_name" id="first_name" value="" size="23" /&gt;
    <label class="className" for="last_name">Last Name:</label>
    &lt;input class="className" type="text" name="last_name" id="last_name" value="" size="23" /&gt;
    <label class="className" for="nickname">Nickname:</label>
    &lt;input class="className" type="text" name="nickname" id="nickname" value="" size="23" /&gt;

    <h2>Current Address</h2>
    <label class="className" for="cur_street">Street/P.O. Box:</label>
    &lt;input class="className" type="text" name="cur_street_address" id="cur_street_address" value="" size="50"/&gt;
    <label class="className" for="cur_city">City:</label>
    &lt;input class="className" type="text" name="cur_city" id="cur_city" value="" size="23"/&gt;
    <label class="className" for="cur_state">State:</label>
    &lt;input class="className" type="text" name="cur_state" id="cur_state" value="" size="23"/&gt;
    <label class="className" for="cur_zip">Zip:</label>
    &lt;input class="className" type="text" name="cur_zip" id="cur_zip" value="" size="23"/&gt;
    <label class="className" for="cur_country">Country:</label>
    &lt;input class="className" type="text" name="cur_country" id="cur_country" value="" size="23"/&gt;

    <h2>Personal Information</h2>
    <label class="className" for="age">Age:</label>
    &lt;?php numeric_dropdown("age","Choose One", 18, 90, TRUE, FALSE);
    echo 'Birthday';
          $month_options = array(
                              'NULL'        => 'Month',
                              'January'      => 'January',
                              'February'    => 'February',
                              'March'       => 'March',
                              'April'        => 'April',
                              'May'            => 'May',
                              'June'        => 'June',
                              'July'        => 'July',
                              'August'        => 'August',
                              'September'    => 'September',
                              'October'        => 'October',
                              'November'    => 'November',
                              'December'    => 'December'
                              );

    echo form_dropdown('month', $month_options, 'NULL');
    numeric_dropdown("day", "Day", 1, 31, TRUE, FALSE);
    numeric_dropdown("year", "Year", 1992, 1920, TRUE, TRUE);
    
    ?&gt;
    &lt;input type="submit" name="submit" value="Apply" /&gt;
    &lt;input type="submit" name="submit" value="Save" /&gt;
   &lt;/form&gt;
  </div>
&lt;/body&gt;
#5

[eluser]smilie[/eluser]
That is what I thought - tho' did not know if you could read which button was pressed.

Glad you solved it (and even more glad you shared it with community).

Cheers,
Smilie




Theme © iAndrew 2016 - Forum software by © MyBB