Skip to content
Advertisement

Laravel – Javascript doesn’t work after I passed a parameter through route

I’m trying to create a website that showcases all search results from various e-marketplace. I’m using javascript to show the result, and it works fine before I try to pass parameter through route. But once I did, the result stopped showing. I assume it’s something to do with the Javascript. Here’s the code:

<?php
    use SunraPhpSimpleHtmlDomParser;


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.tokopedia.com/search?st=product&q=hp%2024mh');
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
    curl_setopt($ch, CURLOPT_TIMEOUT, 300);
    $response = curl_exec($ch);
    curl_close($ch);
    
    file_put_contents("text.html", $response);

    echo $keyword;

    // $url = 'https://www.tokopedia.com/search?st=product&q=24g2';
    // $content = file_get_contents($url);
    // $first_step = explode('<div class="css-1d1aa4" data-testid="imgSRPProdMain">' , $content );
    // $second_step = explode("</div>" , $first_step[1] );

    // echo $second_step[0];
?>

@extends('layout.app')

@section('title')
<title>안녕</title>
@endsection

<!-- Custom CSS -->
<link href="/css/card.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

@section('body')

<div class="container-fluid bg-trasparent my-4 p-3" style="position: relative;">
    <div class="row py-5">
        <div class="col-sm d-flex justify-content-center">
            <h3>Search Result</h3>
        </div>
    </div>
    <div class="row row-cols-1 row-cols-xs-2 row-cols-sm-2 row-cols-lg-4 g-3" id="carda">
    </div>
</div>

@endsection

<script>
    $(document).ready(function(){
        var img;

        $( "#carda" ).load( "text.html .css-1d1aa4 > img", function() {
            var ancestor = document.getElementById('carda');
            var child = ancestor.getElementsByTagName('*');

            var i;
            for (i = 0; i < 3; i++) {
                child[0].remove();
            }

            $( " #carda > img " ).removeClass("success fade").addClass("productImage");
            $( " .productImage ").wrap("<div class='col'><div class='card h-100 shadow-sm'></div></div>");
            $( " .card ").append("<div class='card-body'><div class='clearfix mb-3'> <span class='float-start badge rounded-pill bg-success'>Rp. 1,000,000.00</span></div><h5 class='card-title'>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Veniam quidem eaque ut eveniet aut quis rerum. Asperiores accusamus harum ducimus velit odit ut. Saepe, iste optio laudantium sed aliquam sequi.</h5><div class='text-center my-4'> <a href='#' class='btn btn-warning'>View Product</a> </div></div>");
        });

    });

</script>

And here’s the code for the web.php:

<?php

use IlluminateSupportFacadesRoute;
use AppHttpControllersSearchController;

Route::get('/', function () {
    return view('main');
});

Route::post('/search', [SearchController::class, 'search'])->name('search');
Route::get('/search/{keyword}', [SearchController::class, 'index'])->name('result');

Route::get('/result', function() {
    return view('result');
});

Route::get('/test', [SearchController::class, 'test'])->name('test');
?>

And the SearchController code:

<?php

    namespace AppHttpControllers;
    use IlluminateHttpRequest;


    class SearchController extends Controller
    {
        public function index($a)
        {
            return view('result', [
                'keyword' => $a
            ]);
        }

        public function search(Request $request)
        {
            return redirect()->route('result', [$request->keyword]);
        }

        public function test(){
            return view('result');
        }

    }

?>

Can anyone tell me what’s wrong with this? Any help would be appreciated! Thanks 😀

Advertisement

Answer

I don’t know how nor do I know why, but the path to “text.html” doesn’t work when I passed a parameter through route. I’ve done some tests and found out that the path it needs is “yourweb.com/{path here}” instead of the directory. I solved it by adding a new route for text.html in web.php.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement