Welcome Guest, Not a member yet? Register   Sign In
One form with two buttons
#1

[eluser]chaotiq[/eluser]
Hello, I am new to CI and a MVC framework in general.

I am not sure how to ask this and I have hit a wall.

I have one form and two buttons. If a user clicks the first button data gets posted to a method in a controller. This method has a parameter with a default value of FALSE. If the user clicks the second button I want the data to get posted to the same method, but change the parameter to TRUE.

For the first button, I set the type as submit and the form action is set to the controller. This posts the data and every thing works fine. The second button has an onClick event and calls a javascript function. I want the javascript function to post the data and change the parameter to TRUE.

From reading I know you can set the value of parameters of methods in the uri like ./controller/method/value


Can you specify boolean variables in the url? Such as, http://mysite.com/index.php/controller/method/TRUE


So what I need is to post data to ./controller/method/TRUE via javascript.

Can this be done? Thank you for any help anyone can give me.
#2

[eluser]JHackamack[/eluser]
Why not have two submit buttons in one form with both having a name and a value attribute? Would that solve your issue?
#3

[eluser]chaotiq[/eluser]
What would I do with the value attribute?
#4

[eluser]JanDoToDo[/eluser]
Th value attr just sets what is displayed on the button.. so have on set as "normal submit" and nother as "alt submit". In your code just check to see which button is set
#5

[eluser]chaotiq[/eluser]
Thanks for the help. That, plus my lack of understanding of the Input class put me through a loop.

My code is working now, but I am checking if the button is set directly with PHP's post array and not with CI's Input class.
Code:
if (isset($_POST('button2')))
{
    //do stuff
}


When I try to use CI's Input class nothing gets displayed on the page. No errors, just a blank page.
Code:
if (isset($this->input->post('button2')))
{
    //do stuff
}


I will read over the class in the user guide and try to figure out why this happens. Thank you both for helping me.
#6

[eluser]33cent[/eluser]
I've done some similar thing.. my submit form has two buttons, one for submit, and one for cancel submitting.. example code is here.
Hope it will be helpful Wink

EXAMPLE CONTROLLER:

Code:
function new_entry()
{
    if ($this->input->post('submit'))
    {

    $this->entry_model->add_entry();
    $this->session->set_flashdata('success','success message..');
    redirect('entry/list');
    }
    
    if ($this->input->post('cancel'))
    {
        $this->session->set_flashdata('warning','cancel message..');
    redirect('entry/list');
    }    
    
    else
        {    
      ...
    }

    $data['title'] = 'New entry';
    $data['page'] = 'entry_new';
    $this->load->vars($data);
    $this->load->view('template');
}

EXAMPLE VIEW:

Code:
echo form_open('entry/new_entry');

echo "<p><label>Input field:</label><br />";
$data = array('name'=>'fieldname','id'=>'fieldname', 'size'=>15);
echo form_input($data) ."</p>";

echo form_submit('submit','Add entry');
echo form_submit('cancel','Cancel action');
echo form_close();
#7

[eluser]chaotiq[/eluser]
Thank you so much! That is exactly what I needed.

I was checking if the CI class post method is set like this:
Code:
if (isset($this->input->post('button2')
{
    //do stuff
}

When I should have been checking it like this:
Code:
if ($this->input->post('button2')
{
    //do stuff
}




Theme © iAndrew 2016 - Forum software by © MyBB