[英]Need a little help learning AJAX & PHP?
Firstly please forgive me if this is a dumb question! 首先,如果这是一个愚蠢的问题,请原谅我! I've just started trying to understand what this Ajax stuff is all about with PHP by following a few online tutorials, but I dont get how you can call a javascript function which will start your ajax. 通过遵循一些在线教程,我刚刚开始尝试了解PHP的Ajax内容是什么,但是我不知道如何调用将启动Ajax的javascript函数。
So I have a file index.html and in the body I have a function to initate an ajax request 所以我有一个文件index.html,并且在主体中,我有一个函数来初始化ajax请求
<body>
<script language="javascript" type="text/javascript">
function ajaxFunction(){
var ajaxRequest;
try{
ajaxRequest = new XMLHttpRequest();
}
catch (e){
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Your browser broke!");
return false;
}
}
}
Then I define something to handle changes in the readystate: 然后,我定义一些东西来处理ready状态中的更改:
ajaxRequest.onreadystatechange = function(){
if (ajaxRequest.readyState==4) {
document.getElementById("requestStatus").innerHTML=ajaxRequest.responseText;
}
}
Next I have a Javascript function to call ajax: 接下来,我有一个Javascript函数来调用ajax:
function archiveEvent(eventId) {
var queryString = "?date=" + eventId;
ajaxRequest.open("GET", "archive.php" + queryString,true);
ajaxRequest.send(null);
}
Then in my HTML I have a couple of divs: 然后在我的HTML中,我有几个div:
<div id="outer">
<input type="button" id="archive" value="Archive" onclick="WHAT DO I PUT HERE">
</div>
<div>
<p id="requestStatus"><p>
</div>
The question is what do I put for the onClick value? 问题是我应该为onClick值付出什么? I want to call the function archiveEvent with a parameter of say "09122014". 我想使用参数“ 09122014”调用函数archiveEvent。 In the tutorials ive read it says to put ajaxFunction here. 在我读过的教程中,它说将ajaxFunction放在这里。
When I omitted archiveEvent function and just used hard coded values, this worked fine, but obviously I can't used hard coded values. 当我省略archiveEvent函数并仅使用硬编码值时,此方法工作正常,但显然我无法使用硬编码值。
onclick="javascript:archiveEvent(parameters)"
确保添加函数期望的任何数据。
use this: 用这个:
<input type="button" id="archive" value="Archive" onclick='ajaxFunction()'>
but i'll suggest to try using jquery ajax
. 但我建议尝试使用jquery ajax
。
like this on click event or any: 像这样的点击事件或其他任何事件:
$.ajax({
type="post",
url: archive.php,
data:{date:eventId},
success:function(){
//response here,
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.