1

Cannot find solution to this simple problem, i just do some voting app, and if users votes I would like to show simple alert like "Thanks" , but I cannot find the way to do it from the controller? I have tried the simple jQuery code like this but it's not working.

echo "<script>alert('You have already voted');</script>"; 

Any simple solutions?

public function voting()
{
    $ip=$_SERVER['REMOTE_ADDR'];
    $id = Input::get('msg_id');
    $vote = Input::get('vote');
    $count = Voting::wheremes_id_fk($id)->whereip_add($ip)->count();

    if($count==0)
    {
        if($vote == "up")
        {
            Project::whereid($id)->increment('up');

            Voting::insert(
                array('mes_id_fk' => $id, 'ip_add' => $ip )
            );
        }
        else if ($vote=="down") {

            Project::whereid($id)->decrement('down');

            Voting::insert(
                array('mes_id_fk' => $id, 'ip_add' => $ip )
            );
        }
    } 
    else {
        // How to popup "thanks" alert??
    }
}

1 Answer 1

1

If you are using ajax to do the vote, return a json response and use JavaScript to respond to the json response.

example:

public function vote()
{
  //your voting code
  return Response::json(['success'=>true]);
}

and in your JavaScript, check the json

$.post('/vote/', { id: 123   }, function(data){
    if(data.success== true)
    {
      alert('Thanks for voting);
    }
}

If you are not using ajax, simply use the Session flash.

i.e., from your controller,

public function vote()
{
  //your voting code
  Session::flash('msg', 'Thanks for voting');
 return Redirect::back();
}

and from View,

@if(Session::has('msg'))
  {{Session::get('msg')}}
@endif
Sign up to request clarification or add additional context in comments.

2 Comments

i am trying to setup Session::flash message from controller but it does not firing. I am not using any redirection, i don't want to reload page , just want to make popup message with ok button
posted.. can you check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.