skip to Main Content

Ok, so I have a text field in which I type a string and I have a button next to it.

<div class="sidebar-search">

    <div class="input-group custom-search-form">
        <<label for="riot-summoner-input">Search a Summoner</label><br>
        <input type="text" id="riot-summoner-input" class="form-control" placeholder="Type summoner name..." style="margin-bottom: 20px">
        <button type="button" id="valid-summoner">Search</button>
    </div>
</div>

By Clicking on this button, the following script gets executed

        let res = {{ summoner.summonerLevel }}
        $(document).ready(function() {
            // Get value on button click and pass it back to controller
            $("#valid-summoner").click(function () {
                const summoner_input = $("#riot-summoner-input").val();
                console.log(summoner_input)
                let url = `/coach/?summonerName=${summoner_input}`

                history.replaceState(summoner_input, 'Coach Index', url);
                console.log(url)
                function loadXMLDoc()
                {
                    document.getElementById("display-summonerLevel").innerHTML = `Summoner Level: <h2>${res}</h2>`
                }
                loadXMLDoc();
            });
        });

Now as far as I can understand this will change my page url to include the value inserted in the text field and will send it back to my controller without refreshing the page, which it does.

Now in my Controller I’m using that value to do some logic with it

/**
 * @Route("/", name="app_coach_index", methods={"GET"})
 */
public function index(CoachRepository $coachRepository, riotApi $callRiot, Request $request): ?Response
{
    $value = $request->request->get('summoner_input');


    if($value != null){
    $this->debug_to_console($value . "Hi");
    return $this->render('coach/index.html.twig', [
        'coaches' => $coachRepository->findAll(), 'summoner'=> $this->showSummoner("$value")
    ]);}
    else{
        $this->debug_to_console($value);
        return $this->render('coach/index.html.twig', [
            'coaches' => $coachRepository->findAll()
        ]);
    }
}

Now it’s interesting to note that I’m doing this in the index function.

Here’s the function I’m calling within the index function which is actually the one that gets the value from the script

/**
 * @Route("/?summonerName={summoner_input}", name="show_summoner", methods={"GET"})
 */
public function showSummoner($summoner_input)
{
    $call = new ApiClient(ApiClient::REGION_EUW, 'API-KEY-HERE');
    return $call->getSummonerApi()->getSummonerBySummonerName($summoner_input)->getResult();
}

Now that I’m seeing this I can see that the issue is I’m getting the value in the showSummoner() function but trying to use it in the index function. Which is why I’m not getting a value when I print it to console and the variable is undefined.

Honestly I can’t think of any logic I can do to overcome this issue.

EDIT!!!!!!!

Okay, so I know where the problem is arising, the issue is when I’m calling showSummoner($value) within index function. I’m using $value = $request->query->get('summoner_input');

I thought I was getting that value in the index function when in fact I’m getting it in the showSummoner() function. You can tell by the annotations

For index I don’t have a parameter in its url, whereas in showSummoner() I have a parameter in the annotations as such.

/**
 * @Route("/?summonerName={summoner_input}", name="show_summoner", methods={"GET"})
 */

This is indeed the fact because I’m using that url in the script as such

let url = `/coach/?summonerName=${summoner_input}`

The reason for this is I can’t use the parameter in the index url because then I would have to provide the parameter in all the other places I’m using index in even when I don’t have a parameter meaning I didn’t search for anything.
I hope this gives more clarification

2

Answers


  1. You’re trying to get a value from $_GET global, not $_POST.

    You can replace :

    $value = $request->request->get('summoner_input');
    

    by:

    $value = $request->query->get('summoner_input');
    
    Login or Signup to reply.
  2. You are trying to access the GET parameter using the wrong name ('summoner_input').

    $value = $request->request->get('summoner_input');
    

    When you are setting it as summonerName here:

    let url = `/coach/?summonerName=${summoner_input}`
    

    You will also want to pass a default value to check for, as the second parameter.
    Try this:

    $value = $request->request->get('summonerName', false);
    if(false !== $value){
      /* the parameter is in the url */
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search