1

So, I created a report system, but in the last step, I have a little problem...In JavaScript I'm using a variable from database for Ajax, but when a post has no comments, I'm getting Undefined variable: key.

Here is my script

if (isset($key)){
    $('#yourFormIdComment{{$key+1}}').submit(function(e){
    e.preventDefault();
    var data = $(this).serialize();
    $.ajax({
    method:'POST',
    url:'/career_report_comment',
    data:data,
    success: function (result) {
            // do something with result
        }
    });
    });
}


Here is my view with variable

@foreach($opinionComment as $key => $comments)
    <div class="news-v3 bg-color-white">

<h4 style="font-size: 13px">
        {{ $comments->user->username }}</h4>
    <p>{{ $comments->comments }}</p>
        <ul class="post-shares post-shares-lg">
<li @if ($key === 0) class="active" @endif  style="float: right;left: 20px;bottom: 30px"><a data-toggle="modal" data-target="#exampleModalComment{{$key+1}}" href="javascript:void(0)" ><i class="rounded-x icon-flag"></i></a></li>
</ul>

        </div>
@endforeach

My question is...how can I pass the variable {{$key+1}}from script even if a post has no reviews/comments?Because this is happening only when I have comments on a post.

7
  • You cant pass data you dont have. You can do a check if the data isset or empty so you are sure you pass on data you actually have. If the data is empty redirect them to the page were the data is created Commented Sep 18, 2019 at 8:53
  • Yes, I want to make a check, if variable isn't empty, run this code...if ( $key == null( { code }, but this isn't working. Commented Sep 18, 2019 at 8:55
  • if (isset($key)) {code}, this checks if the var is set, or you can do you check but instead of using == use != Commented Sep 18, 2019 at 8:55
  • @Collin , please check my updated script, because now I have save undefined key :( Commented Sep 18, 2019 at 9:00
  • What do you get when you dd($key) within your if(isset(key)) statement Commented Sep 18, 2019 at 9:03

1 Answer 1

1

Andrew i after talking in the comments and checking your code i might have found a solution to your problem.

You get a error:

Undefined variable: key

You get that error because the key isnt defined.

if (isset($key)){
    $('#yourFormIdComment{{$key+1}}').submit(function(e){
    e.preventDefault();
    var data = $(this).serialize();
    $.ajax({
    method:'POST',
    url:'/career_report_comment',
    data:data,
    success: function (result) {
            // do something with result
        }
    });
    });
}

In the first part of your code you use the variable $key, but the key is defined within the foreach in the second part of your code. That's why you get the error.

Sign up to request clarification or add additional context in comments.

2 Comments

The problems is that in view I'm using that variable, like here <li @if ($key === 0) class="active" @endif style="float: right;left: 20px;bottom: 30px"><a data-toggle="modal" data-target="#exampleModalComment{{$key+1}}" href="javascript:void(0)" ><i class="rounded-x icon-flag"></i></a></li> </ul>, and I only have undefined variable on scripts section
You get that error on the scripts section because you didnt set the variable there. You only declare the variable in de foreach that is found in your views seciton. That why you cant use that variable. You could declare it with a foreach around the script.

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.