SGouws SGouws - 2 months ago 7
PHP Question

Issue with redirect() when using conditional to evaluate multiple form buttons

So I've built a small conditional to evaluate which button is pressed in my form (as there are 2). This works fine and fires off the correct method and writes the appropriate data to the DB, however my redirect is not working. It

saves()
to the DB and then simply stays on the page designated as the POST route.

I suspect the problem has something to do with my conditional and the use of
$this
.

Here is my check_submit method:

public function check_submit()
{
if(!is_null(Input::get('add_to_invoice'))){
$this->invoice_add_item();
} elseif(!is_null(Input::get('complete_invoice'))) {
$this->invoice_complete();
}
}


Here is one of the 2 methods which I am currently testing:

public function invoice_add_item()
{
$input = Request::all();
$invoice_items = new Expense;
$invoice_items->item_id = $input['item_id'];
$invoice_items->category_id = $input['category'];
$invoice_items->price = $input['price'];
$invoice_items->store_id = $input['store'];

if(Input::has('business_expense'))
{
$invoice_items->business_expense = 1;
}
else{
$invoice_items->business_expense = 0;
}
$invoice_items->save();
return redirect('/');
}


Perhaps there is a better way of handling this in my routes(web) file, but I'm not sure how to go about this.

Answer

You should add the return to the check_submit() method. Something like

public function check_submit()
{
    if(!is_null(Input::get('add_to_invoice'))){
        return $this->invoice_add_item();
    } elseif(!is_null(Input::get('complete_invoice'))) {
        return $this->invoice_complete();
    }
}

Better yet, you should probably return a boolean on invoice_add_item() and based on that, redirect the user to the correct place (or with some session flash variable with an error message)

Comments