Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I am passing an ajax call to delete some user data, and this data is passed in an hidden input, I want to convert it to a PHP Array to be able to do a foreach loop around it. How can I achieve that?

The values it output in Javascript,

""49,51,53,54""

The PHP code,

public function usersDelete(){

    $data = Input::all();
    $checkedUsers = Input::get('checkedUsers'); // data is an array

    foreach ($checkedUsers as $checkedUser) {

        User::where('id', '=', $checkedUser)->first()->delete();
    }
}

the problem is that it loops only one the first array value in the set of arrays.

share|improve this question
up vote 1 down vote accepted

If you use inputs, $checkedUsers is a string, an you must use explode(',', $checkedUsers) to convert it to array. Before deleting verify that user has appropriate rights to delete specified users. It is very easy to modify JavaScript and request data.

share|improve this answer
    
laravel has a mandatory token for every request but thanks i will implement the auth part too. – udemethegrtman Jul 4 at 3:45
    
Thanks @everyone for your support in this issue i really appreciate it. – udemethegrtman Jul 4 at 4:07
    
CSRF toke protects from request being forged outside your website, but it is not sufficient here, neither is user auth, it will make sure that only registered users can delete User. Keep in mind that client can manipulate any data before submitting form. e.g. form has set of ids [1,2,3] user can manipulate that form and send [1,2,90]. Therefore requesting deletion of User 90 even though it wasn't on the original set. – exabyssus Jul 5 at 5:01

Why not use

User::whereIn('id',$checkedUsers)->delete();
share|improve this answer

You can either send the data as array from JS using valueFromInput.split(',') or you can convert it in php using explode(',', $jsData);

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.