简体   繁体   中英

How to solve error Undefined index in php (laravel)

I am sending arrays from View to Controller using Post Method. This is my code.

Controller: Session::put('isCheck', $isCheck);

this is how i am assigning value to isChecked array.

   var isChecked = [
    <?php
    $isCheck = "";
    if (Session::has('isCheck')) {
        $isCheck = Session::get('isCheck');
    }
    foreach ($isCheck as $isCheck) {
        $status = $isCheck;
        ?>
        <?php echo $status; ?>,
    <?php } ?>
   ];

View:

  $("#target").click(function () {
                    var postTo = '<?php echo action('sample@postView'); ?>';
                    var data = {
                        isChecked: isChecked,
                        duplicateIsChecked: duplicateIsChecked
                    };
                    jQuery.post(postTo, data,
                            function (data) {
                                alert(data);

                            });
                });

isChecked and DuplicateIsChecked are my 2arrays.

In controller I am writing this code:

 $duplicateIsChecked = $_POST['duplicateIsChecked'];
 $isCheck = $_POST['isChecked'];

But i am getting Undefined index duplicateIsChecked error. Help me

Have you checked the header of your Ajax call to make sure its sending any data? In the code you posted, you're assigning variables to the two values in data but you didn't show where you are declaring those variable values.

You can't pass a javascript array to php like that, you have to convert the array into JSON and send it.

so it should be

   var data = JSON.stringify({
                        isChecked: isChecked,
                        duplicateIsChecked: duplicateIsChecked
                    });

To get the JSON payload in controller, you can use

$payload = Input::json()->all();

then to access the attribute, you can reference it as $payload['duplicateIsChecked'] & $payload['isChecked']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM