要麼使用jQuery.load(),jQuery.post(), jQuery.get()或jQuery.ajax()
<script type="text/javascript">
var script = window.innerWidth > 992 ? "medium.php" : "mobile.php";
var postVars = {} // Use this to provide additional data to you PHP script, or ommit this parameter if you don't need it
var dataType = 'json'; // The type of data your PHP script will return, could be html, json, text. ommit this if you don't have a particular use for it.
jQuery.post(script, postVars, function(response) {
// This is your callback function, you can do whatever you please with response here.
}, dataType)
</script>
安全通知 永遠不要相信最終用戶!由於這是客戶端腳本,這些變量可能會被操縱爲config.php。
爲了解決這個問題,您應該不是請求多個腳本,而是請求一個腳本,提供一個包含文件的參數,然後檢查是否允許包含該文件。
<script type="text/javascript">
var script = window.innerWidth > 992 ? "medium.php" : "mobile.php";
var postVars = {file_to_include: script}
var dataType = 'json'; // The type of data your PHP script will return, could be html, json, text. ommit this if you don't have a particular use for it.
jQuery.post("include.php", postVars, function(response) {
// This is your callback function, you can do whatever you please with response here.
}, dataType)
</script>
然後在你的PHP腳本
<?php
if(in_array($_POST["file_to_include"], array("medium.php", "mobile.php"))) {
include $_POST["file_to_include"];
}
?>
[只在特定的屏幕分辨率包括PHP文件(的可能重複http://stackoverflow.com/questions/16172806/include-php-file - 僅在某些屏幕分辨率) – kayess
這是如此錯誤,或者只是對我來說似乎不對 – madalinivascu
更好的去與AJAX請求 – madalinivascu