在某人有一个去我或标记下来,我期待所有在互联网上找出如何做到这一点(包括计算器同样的问题)。我是新的,而且我发现它非常努力地学习新的概念,所以请宽容我。
我想要做的就是调用PHP脚本/功能上的一个按钮的点击。我有WAMP这个运行有没有什么帮助。这是我的code:
< PHP的包括'the_script.php; ?>
<按钮式=按钮的onclick =the_function()>点击我和LT; /按钮>
the_script.php有这样的吧:
the_function(){
回声你赢了;
}
为什么不是这方面的工作?我听说过的按钮是客户端等,并且PHP是服务器端,这意味着你不能链接两者结合起来。我知道,你必须使用AJAX来完成这项工作,但是我有理由完全不知道该怎么做。我试着使用Google等等,但是我无法找到任何东西。我知道如何使用AJAX和调用它的事件,但是,我仍然不知道如何使它调用PHP脚本。
你能不能请你的答案是明确和简单越好,我是新来这个
感谢您的帮助:)
编辑***
由于某种原因,无论我走到哪里大家的code是不同的。我已经教了AJAX的方式看起来完全不同。能否请您写这样,所以我能理解吗?谢谢你,这里有一个例子:
VAR请求;
如果(window.XMLHtt prequest){
要求=新XMLHtt prequest();
} 其他 {
要求=新的ActiveXObject(Microsoft.XMLHTTP);
}
request.open(GET,为file.php,真正的);
request.onreadystatechange =功能(){
如果(request.readyState === 4和&安培; request.status === 200){
做的东西
}
}
request.send();
解决方案
刚刚尝试这一点:
<按钮式=按钮>点击我和LT; /按钮>
&其中p为H.;&所述; / P>
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
$(按钮)。点击(函数(){
$阿贾克斯({
键入:POST,
网址:script.php的,
成功:功能(数据){
警报(数据);
$(P)文本(数据)。
}
});
});
});
< / SCRIPT>
在运行script.php
< PHP
回声你赢了;
?>
Before someone has a go at me or marks this down, I have looked all over the internet to find out how to do this (including the same question on stackoverflow). I'm new, and I am finding it very hard to learn new concepts so please be easy on me.
What I want to do is call a php script/function on a button click. I have this running in WAMP if that helps. Here's my code:
<?php include 'the_script.php'; ?>
<button type="button" onclick="the_function()">Click Me</button>
the_script.php has this in it:
the_function() {
echo "You win";
}
Why isn't this working? I've heard about the button being client side etc. and PHP being server-side, which means that you cannot link the two together. I know that you have to use AJAX to make this work, however I legitimately have absolutely no clue how to do it. I've tried googling it etc., however I can't find anything. I know how to use AJAX and call events with it, however I still have no idea how to make it call a PHP script.
Can you please make your answers as clear and simple as possible, I'm new to this
Thanks for the help :)
EDIT ***
For some reason wherever I go everyone's code is different. The way I have been taught AJAX looks completely different. Can you please write it this way so I can understand? Thanks, here's an example:
var request;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.open('GET', 'file.php', true);
request.onreadystatechange = function() {
if (request.readyState===4 && request.status===200) {
do stuff
}
}
request.send();
解决方案
Just try this:
<button type="button">Click Me</button>
<p></p>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$.ajax({
type: 'POST',
url: 'script.php',
success: function(data) {
alert(data);
$("p").text(data);
}
});
});
});
</script>
In script.php
<?php
echo "You win";
?>