开发者

Submit Button, Execute PHP in CodeIgniter

I am developing a web app but running into a few small snags. I'm using CodeIgniter.

I want to hav开发者_如何学Pythone 2 buttons that will do execute 2 different functions that do different things to the database. I have these functions made in the proper Controller file.

What is the best way to go about making the buttons execute their respective functions? If it requires javascript, I have no problem making it, just need some pointers as I'm a little bit confused here!


If they're making changes to records in the database, you should probably implement them as part of a form (or two). Potentially destructive actions should not be executable just using a simple GET request.

The form(s) can contain a hidden input type to specify what you want to in the controller.

HTML page:

<form action="controller/myfunction" method="POST">
    <input type="hidden" name="action" value="one">
    <input type="submit" value="Do action one">
</form>

<form action="controller/myfunction">
    <input type="hidden" name="action" value="two">
    <input type="submit" value="Do action two">
</form>

Controller:

function myfunction()
{
    // Your form would be submitted to this method...
    // Get action from submitted form ('one' or 'two')
    $action = $this->input->post('action');
    // Decide what to do
    switch ($action)
    {
        case 'one': $this->my_first_action(); break;
        case 'two': $this->my_second_action(); break;
    }
}

function my_first_action()
{
    // Do stuff...
}

It would be good practise to redirect to another page once the form has been submitted - use the 'Post/Redirect/Get' pattern.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜