[英]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.