Javascript 如何使用ajax从js调用php函数?
我有一个当我做某事时触发的js函数,我想调用一个php函数。我知道我需要使用ajax来实现这一点,但具体如何实现呢?我学习ajax,但没有找到它的参考资料。你能告诉我它是怎么工作的吗。这是我希望它如何工作的基础:Javascript 如何使用ajax从js调用php函数?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有一个当我做某事时触发的js函数,我想调用一个php函数。我知道我需要使用ajax来实现这一点,但具体如何实现呢?我学习ajax,但没有找到它的参考资料。你能告诉我它是怎么工作的吗。这是我希望它如何工作的基础: JS:call php function when JS triggered. PHP:echo(or do) something. 编辑:我想得到的是无限滚动,当我到达页面底部时,它会给我新的php数据。我关注这一集,我想要一组蓝色方块来显示数据。 这是这一集的代码。当页面移到底
JS:call php function when JS triggered.
PHP:echo(or do) something.
编辑:我想得到的是无限滚动,当我到达页面底部时,它会给我新的php数据。我关注这一集,我想要一组蓝色方块来显示数据。
这是这一集的代码。当页面移到底部时,它将显示蓝色方形
<!DOCTYPE html>
<html>
<head>
<script>
function yHandler(){
// Watch video for line by line explanation of the code
// http://www.youtube.com/watch?v=eziREnZPml4
var wrap = document.getElementById('wrap');
var contentHeight = wrap.offsetHeight;
var yOffset = window.pageYOffset;
var y = yOffset + window.innerHeight;
if(y >= contentHeight){
// Ajax call to get more dynamic data goes here
wrap.innerHTML += '<div class="newData"></div>';
}
var status = document.getElementById('status');
status.innerHTML = contentHeight+" | "+y;
}
window.onscroll = yHandler;
</script>
<style>
div#status{position:fixed; font-size:24px;}
div#wrap{width:800px; margin:0px auto;}
div.newData{height:1000px; background:#09F; margin:10px 0px;}
</style>
</head>
<body>
<div id="status">0 | 0</div>
<div id="wrap"><img src="temp9.jpg"></div>
</body>
</html>
函数yHandler(){
//观看视频,逐行解释代码
// http://www.youtube.com/watch?v=eziREnZPml4
var wrap=document.getElementById('wrap');
var contentHeight=wrap.offsetHeight;
var yOffset=window.pageYOffset;
变量y=yOffset+window.innerHeight;
如果(y>=内容高度){
//这里是获取更多动态数据的Ajax调用
wrap.innerHTML+='';
}
var status=document.getElementById('status');
status.innerHTML=contentHeight+“|”+y;
}
window.onscroll=yHandler;
div#status{位置:固定;字体大小:24px;}
div#wrap{宽度:800px;边距:0px自动;}
div.newData{高度:1000px;背景:09F;边距:10px 0px;}
0 | 0
点击我!
$(“#fooid”)。单击(函数(){
var term=“一些字符串foo”;
//使用post发送数据,在PHP中,您将获得$\u post['foo']
var posting=$.post('index.php',{foo:term});
//警告结果(在php脚本中:echo“test performed!”;)
过帐.完成(功能(数据){
警报(“数据加载:”+数据);
});
});
需要更详细地解释您在实现ajax时遇到的期望和问题。还向我们展示了一些代码。您知道php只在服务器上运行,javascript在浏览器中运行,对吗?可能是重复的是,我理解。我会更新我的question@AhmedShamel在您链接的这个问题中,“new user.php”是否是同一文件夹中的不同页面。我的意思是他们是否制作了一个html js页面并链接到php页面?
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="fooid">click me!</div>
<script>
$( "#fooid" ).click(function() {
var term = "some string foo";
// Send the data using post, in PHP you will get $_POST['foo']
var posting = $.post('index.php', { foo: term } );
// alert the results (in php script: echo "test performed!";)
posting.done(function( data ) {
alert( "Data Loaded: " + data );
});
});
</script>
</body>
</html>