[英]PHP, AJAX, JQuery Get Return Value
我試圖在返回到PHP頁面的AJAX調用中獲得div中的值。
我嘗試了以下方法:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(document).ready(function(e) {
$.ajax({
type: "get",
url: "returneye.php",
success: function (data, textStatus) {
alert(data);
var eye = $('#valueeye').html(data);
alert(eye);
}
});
});
</script>
而returneye.php具有以下內容:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test PHP Return</title>
</head>
<?php
for ($i = 0; $i <= 20; $i++)
{
echo $i;
}
?>
<div id="valueeye" hidden><?php echo $i; ?></div>
<body>
</body>
</html>
但是我沒有得到i
的價值。
我嘗試過
$('#valueeye').html(data).val();
我在警報框中看到undefined
。
我該如何實現。
似乎您嘗試測試使用Ajax ..
在index.php中
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(document).ready(function(e) {
$.ajax({
type: "get", // you may no need this line because ajax is set type get by default
url: "returneye.php",
success: function (data) {
alert(data);
$('#valueeye').html(data);
}
});
});
</script>
<div id="valueeye"></div>
在returneye.php中
<?php
for ($i = 0; $i <= 20; $i++)
{
echo $i;
}
?>
請注意,請確保您的index.php和returneye.php在同一路徑中
此代碼輸出
01234567891011121314151617181920
最后 ,工作代碼是
$(document).ready(function(){
// run a function to get new content onload
getnewContent(0);
// window scroll event
$(window).on('scroll' , function(){
var windowscrollTop = $(this).scrollTop(); // window scrollTop
var windowHeight = $(this).height(); // window height
var documentHeight = $(document).outerHeight(true); // document height
// if scroll reach to the bottom
if(windowscrollTop == documentHeight - windowHeight){
// get last element
var lastElement = getlastelementdataattr();
alert(lastElement);
// run function to append new content and pass the last element
getnewContent(lastElement);
}
});
});
// function to get new content (in your case you will get this from ajax so each div you create in php should have data-getContent attribute )
function getnewContent(lastNum){
for(var i = lastNum ; i <= lastNum + 30 ; i++){
$('#showContent').append('<div class="" data-getContent="'+i+'">Content '+i+'</div>');
}
// your ajax should be like this
/*
$.ajax({
type: "post", // use post method here
url: "returneye.php",
data : {lastElement : lastNum},
success: function (data) {
alert(data);
$('#valueeye').html(data);
}
});
// and in php get lastnum with
<?php
if(isset($_POST['lastElement'])){ // from data : {lastElement : lastNum}, in js
echo (data);
// then get the data from data base here
}
?>
*/
}
// function to get the last element number from #showContent in my html above .. change #showContent with your div id or class you append the data inside it
function getlastelementdataattr(){
return parseInt($('#showContent div:last').attr('data-getContent'), 10);
}
如果您將請求從returneye.php發送到returneye.php,則使用以下代碼
<?php
if(!empty($_GET['target'])){
for ($i = 0; $i <= 20; $i++)
{
echo $i;
}
die();
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test PHP Return</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(document).ready(function(e) {
$.ajax({
type: "get",
url: "returneye.php?target=getdata",
success: function (data, textStatus) {
alert(data);
var eye = $('#valueeye').html(data);
alert(eye);
}
});
});
</script>
</head>
<body>
<div id="valueeye" ></div>
</body>
</html>
根據Maanish的評論進行編輯
js
$.ajax({
type: "get",
url: "some_page.php",
success: function (data, textStatus) {
alert(data);
}
});
的PHP
some_page.php
<?php
$some_var = 1245;
echo $some_var;
die();
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.