[英]Include php file via Ajax and display on page
我想知道是否有任何方法可以在AJAX調用后包括php文件,並將包含的文件顯示在我的頁面中作為響應。
例如,這是一個ajax調用:
$('#id').change(function(){
var selected = $(this).val();
$.ajax({
url: "ajax.php",
type: "POST",
data: "select="+selected,
success: function (data) {
$("#here").html(data);
}
});
});
這就是我在php中嘗試過的方法,但是我的html上沒有顯示結果:
if(isset($_POST['select'])){
$post= $_POST['select'];
$class = ClassName::find_by_id($post);
$sql = " sp_SQLStoredproc {$class->id} ";
if($class->hasRows($sql)){
include("include.php");
}
}
有什么辦法可以在我的html中顯示我包含的文件?
我試圖從Ajax $("#here").html(data);
更改成功響應$("#here").html(data);
到$("#here").load(data);
但它返回了整個頁面。
任何建議都會有所幫助。
更新:include.php文件中存在一個長長的html代碼和一些類方法
PS。 請不要提及腳本是不安全的,我知道這只是示例腳本。
先感謝您
為了獲得成功數據,您需要返回要包含的數據。 並且請記住, php
不能在html
工作,除非它是動作調用。 您可以為此使用php
文件,因為它也具有html
支持。
另外,您還需要記住,在將成功數據設置為元素之前,最好先控制該值並確保獲取正確的數據。
if(isset($_POST['select'])){
$post= $_POST['select'];
$class = ClassName::find_by_id($post);
$sql = " sp_SQLStoredproc {$class->id} ";
if($class->hasRows($sql)){
return include("include.php");
}
}
您可以在緩沖區中設置所有的“ include.php” html,然后需要將內容回顯到控制台。
<?php
if(isset($_POST['select'])){
$post= $_POST['select'];
$class = ClassName::find_by_id($post);
$sql = " sp_SQLStoredproc {$class->id} ";
if($class->hasRows($sql)){
ob_starts();
include("include.php");
$include = ob_get_contents();
echo $include;
}
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.