Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zxywvu's avatar

How to solve "undefined" in JavaScript?

I have a block of Published Yes and No.

<div class="card mb-3">
    <div class="card-header">{{ __('Published') }}</div>
    <div class="card-body">
        <div class="form-check">
            <input class="form-check-input published_filter" type="radio" name="published" id="yes" value="1">
            <label class="form-check-label" for="yes">
                Yes
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input published_filter" type="radio" name="published" id="no" value="0">
            <label class="form-check-label" for="no">
                No
            </label>
        </div>
    </div>
</div>

When I click Yes and No, I see this alert undefined.

<script>
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    $(".published_filter").change(function(){

        var publish = $(this).val();
        $.ajax({
            type:'POST',
            url:"{{ route('category-filter') }}",
            data:{publish: publish},
            success:function(data){
                alert(data.success);
            }
        });
    });
</script>
0 likes
16 replies
Sinnbeck's avatar

@esfer like this?

success:function(data){
    alert(data.status === 'success' ? 1 :0);
} 
Sinnbeck's avatar

@esfer you are trying to get ->published on a collection. But without seeing your code, it's hard to say more

1 like
Sinnbeck's avatar

@esfer ok. You get all products

$products = $products->get();

And try to get the published property as if you only got one (->first() or - >find())

'data' => $products->published,

But I'm not sure how this is related to the question?

Akash_kushwaha's avatar

@esfer change your code back to as it was before.

	return response()->json([
   				 'status' => 'success',
    			'data' => $products,
	]);
Akash_kushwaha's avatar

@esfer here use this

		public function filter(Request $request)
		{
			$products = Product::query();

		$success = 'error';
if($request->has('publish') && $request->publish = 1) {
    $products->where('published', 1);
    $success = 'success';
}

$products = $products->get();

return response()->json([
    'status' => $success,
    'data' => $products,
]);

}

1 like
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@esfer yes status is always success? You are setting it yourself?

Maybe you want

return response()->json([
        'status' => $request->boolean('publish') ? 1 :0, 
        'data' => $products,
    ]);
success:function(data){
    alert(data.status);
}
1 like
Sinnbeck's avatar

And this can be simplified

if($request->boolean('publish')) {
    $products->where('published', 1);
    $success = 'success';
} 
Akash_kushwaha's avatar

@esfer Change your controller

			public function filter(Request $request)
			{
			$products = Product::query();

			if($request->has('publish') && $request->publish = 1) {
    			$products->where('published', 1);
			}

			$products = $products->get();

			return response()->json([
    					'status' => 'success',
   				 '		data' => $products->published,
			]);
		}

and in Ajax request change

	success:function(data){
				alert(data.data);
	}
Akash_kushwaha's avatar

@esfer Sorry, I didn't saw your code properly you are using get() the get all the values. remove my code and just add condition in your javascript as @sinnbeck said.

	success:function(data){
		alert(data.status === 'success' ? 1 :0);
	} 

Please or to participate in this conversation.