好的,所以我有一個文本欄位,我在其中輸入一個字串,并且旁邊有一個按鈕。
<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>
通過單擊此按鈕,將執行以下腳本
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();
});
});
現在據我所知,這將更改我的頁面 url 以包含插入文本欄位中的值,并將其發送回我的控制器而不重繪 頁面,它確實如此。
現在在我的控制器中,我正在使用該值對其進行一些邏輯處理
/**
* @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()
]);
}
}
現在有趣的是,我在 index 函式中執行此操作。
這是我在 index 函式中呼叫的函式,它實際上是從腳本中獲取值的函式
/**
* @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();
}
現在我看到了這一點,我可以看到問題是我在showSummoner()函式中獲取值但試圖在索引函式中使用它。這就是為什么當我將它列印到控制臺并且變數未定義時我沒有得到一個值。
老實說,我想不出任何可以解決這個問題的邏輯。
編輯!!!!!!!
好的,所以我知道問題出在哪里,問題是當我在索引函式中呼叫 showSummoner($value) 時。我正在使用$value = $request->query->get('summoner_input');
我以為我在 index 函式中獲得了該值,而實際上我在 showSummoner() 函式中獲得了它。你可以通過注釋來判斷
對于索引,我的 url 中沒有引數,而在 showSummoner() 中,我在注釋中有一個引數。
/**
* @Route("/?summonerName={summoner_input}", name="show_summoner", methods={"GET"})
*/
這確實是事實,因為我在腳本中使用了該 url
let url = `/coach/?summonerName=${summoner_input}`
這樣做的原因是我不能在索引 url 中使用引數,因為這樣我就必須在我使用索引的所有其他地方提供引數,即使我沒有引數意味著我沒有搜索任何東西。我希望這能提供更多澄清
uj5u.com熱心網友回復:
您正在嘗試從$_GET全域而不是$_POST.
您可以更換:
$value = $request->request->get('summoner_input');
經過:
$value = $request->query->get('summoner_input');
uj5u.com熱心網友回復:
您正在嘗試使用錯誤的名稱 ( 'summoner_input') 訪問 GET 引數。
$value = $request->request->get('summoner_input');
當您將其設定為summonerName此處時:
let url = `/coach/?summonerName=${summoner_input}`
您還需要傳遞一個默認值作為第二個引數進行檢查。
試試這個:
$value = $request->request->get('summonerName', false);
if(false !== $value){
/* the parameter is in the url */
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/475567.html
標籤:javascript php jQuery 阿贾克斯 交响乐
