[英]Get php variable from url on reload without refresh
我正在從事一個業余項目(通常以設計師的身份工作,所以對php並不是那么熟悉–請對所有或任何冗余代碼進行監督),嘗試學習新知識。 現在,我遇到了一個問題,我似乎不太了解這個問題。 我有一個index.php用來顯示data.php中的隨機句子,這很好用-但是我希望能夠根據需要為不同的人整理特定類型的句子。 這是通過包含Designer,Illustrator和Developer的下拉列表完成的。
例如,如果您從下拉菜單中選擇“開發人員”,結果頁面將使用URL中的index.php?yrke=developer
重新加載。 一切都很好,符合預期,當我回顯$_GET['yrke'];
在data.php中,它會在第一次加載時很好地顯示文本“ developer”,但在單擊randomizerButton
按鈕后(請注意,從data.php加載內容時,單擊此按鈕時不會刷新瀏覽器中的頁面) $_GET['yrke'];
似乎無法讀取url中的值(將$_GET['yrke'];
放在index.php
顯然可以工作,但是我需要訪問data.php中的url變量)。
如果有辦法做到這一點,同時又保持了“ update-content-without-browser-refresh”功能,那另一種最簡單的解決方案可能就是刪除“ update-content-without-browser-refresh”,然后進行舊的更新,從而解決問題, 但是為什么要那么簡單呢?
index.php (節選)
<button data-href="data.php" class="randomizerButton">Randomize sentences</button>
<form action="index.php" method="get">
<select name="yrke" onchange="this.form.submit()">
<option value="designer"<?=$_GET['yrke'] == 'designer' ? ' selected="selected"' : '';?>>Designer</option>
<option value="illustrator"<?=$_GET['yrke'] == 'illustrator' ? ' selected="selected"' : '';?>>Illustrator</option>
<option value="developer"<?=$_GET['yrke'] == 'developer' ? ' selected="selected"' : '';?>>Developer</option>
</select>
</form>
<?php include('data.php'); ?>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('button.randomizerButton').click(function(){
scriptUrl = $(this).attr('data-href');
$.post(scriptUrl, function(response){
$('#results').html(response);
});
});
});
</script>
data.php (節選)
echo $_GET['yrke'];
如果要在data.php
上使用$_GET['yrke']
, data.php
必須使用$.get()
並僅使用完整的URL,例如:
$.get('data.php?yrke='+encodeURIComponent($('[name=yrke]').val()), function(response){
$('#results').html(response);
}
我會用
$.post('data.php', {yrke:encodeURIComponent($('[name=yrke]').val())}, function(response){
$('#results').html(response);
}
在data.php
上使用$_POST['yrke']
。 通常,我不會以傳統方式提交表格。 這一切都與AJAX有關。
不知道這是否有幫助,但是您可以執行以下操作...
<div id ="content">
<?php
$pages_dir ='pages';
if(!empty($_GET['p'])){
$pages =scandir($pages_dir, 0);
unset($pages[0], $pages[1]);
$p = $_GET['p'];
if (in_array($p.'.inc.php', $pages)){
include($pages_dir.'/'.$p.'.inc.php');
}else {
echo'Page not found';
}
}else{
include($pages_dir.'/home.inc.php');
}
?>
HERE
</div>
創建一個名為pages的文件夾,然后將文件夾“ aboutus.inc.php”中的文件命名為ect。 文件擴展名的重要部分是.inc.php,您可以在.inc.php之前命名任何東西,此處您喜歡的東西是如何編寫導航“關於我們”的示例
如果需要,我可以給您發送一個空白模板
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.