i want to put two button in my view.and two button send value to one controller. how to check which button press?! according this link Link
but in controller not get value of button.any idea?
asked Jan 26, 2017 at 19:18
user2254798
5852 gold badges7 silver badges17 bronze badges
-
Not an expert but if you want to identify that which button is pressed then you can give that button name and get that name in the input post array.always-a-learner– always-a-learner2017年01月27日 03:37:03 +00:00Commented Jan 27, 2017 at 3:37
2 Answers 2
Use the same approach, however in CodeIgniter your code will look a bit clearer :
<form action="TheController/PostHandler" method="POST">
<input type="submit" name="button1" id="button1" value="Button 1" />
<input type="submit" name="button2" id="button2" value="Button 2" />
</form>
Because in CodeIgniter, this is :
$something = $this->input->post('something');
equivalent to :
$something = isset($_POST['something']) ? $_POST['something'] : NULL;
Simply check for null values with a code like this :
public function PostHandler(){
if (!is_null($this->input->post('button1'))){
// code for button 1
}
if (!is_null($this->input->post('button2'))){
// code for button 2
}
}
Sign up to request clarification or add additional context in comments.
3 Comments
user2254798
i put only this code "if (!is_null($this->input->post('button1'))){}". but i press button 2 this works.i really confuse.sorry
I think form helper better codeigniter.com/user_guide/helpers/form_helper.html#form_open
mrbm
yes, I had a type under button 2 name, just fixed it
$this->input->post('some_data'); // The function returns FALSE (boolean) if some_data not isset
Use
if($this->input->post('button1')){
$button1 = $this->input->post('some_data',true) //for xss
} else { $bouton1 = false;}
answered Jan 27, 2017 at 6:56
demenvil
1,1391 gold badge12 silver badges25 bronze badges
Comments
lang-php