javascript 使用 Ajax 执行 php 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24405486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute php file with Ajax
提问by Tharaka Nuwan
When I click on "Register Now" Button, I want to execute 'input.php' in which I have code to insert my data to the database and show a success message. I don't want to leave current page.
当我单击“立即注册”按钮时,我想执行“input.php”,其中我有将数据插入数据库并显示成功消息的代码。我不想离开当前页面。
<input type="button" id="confirm" value="Register Now" class="button">
<script type="text/javascript">
$(document).ready(function() {
$("#confirm").click(function() {
<?php
include 'input.php';
?>
alert ("data Added successfully.");
});
});
</script>
My code is giving me "data Added successfully" message but PHP file hasn't executed and no data is added to the database. All necessary data is in session variables.
我的代码给了我“数据添加成功”的消息,但 PHP 文件没有执行,也没有数据添加到数据库中。所有必要的数据都在会话变量中。
回答by Luke Peterson
Suggest you try something like the below. You shouldn't be trying to execute PHP inside of a jQuery script. Do an AJAX call and pass the data through and process it in the PHP rather than relying on the session variables. For example:
建议您尝试以下操作。您不应该尝试在 jQuery 脚本中执行 PHP。执行 AJAX 调用并传递数据并在 PHP 中处理它,而不是依赖于会话变量。例如:
<script type="text/javascript">
$(document).ready(function () {
$("#confirm").click(function () {
$.ajax({
type: "POST",
url: "index.php",
data: {
firstname: "Bob",
lastname: "Jones"
}
})
.done(function (msg) {
alert("Data Saved: " + msg);
});
});
});
</script>
Where firstname, lastname would be your normal session data.
其中名字,姓氏将是您的正常会话数据。
You can learn more about the jQuery.ajax() function in the jQuery API Documentation.
您可以在jQuery API 文档 中了解有关 jQuery.ajax() 函数的更多信息。
回答by Ballantine
To execute a Php script from javascript, you have to use Ajax.
要从 javascript 执行 Php 脚本,您必须使用 Ajax。
the following code :
以下代码:
$("#confirm").click(function() {
<?php
include 'input.php';
?>
alert ("data Added successfully.");
});
will not work
不管用
回答by Valentin Mercier
You need to use AJAX. Ajax is the concept of calling php files from javascript from inside the page. You then get the php page output in a variable and you can choose wether you will display it or not. An example of this technology is the show more posts of Facebook. This can be easily done with jQuery.
您需要使用 AJAX。Ajax 是从页面内部从 javascript 调用 php 文件的概念。然后您在一个变量中获得 php 页面输出,您可以选择是否显示它。这种技术的一个例子是显示更多 Facebook 的帖子。这可以使用 jQuery 轻松完成。
$.post( PHP_FILE, { field1: 'value', field2: 'value'}).done(function( data )
{alert("this function will be run when the request is over and the variable data
will have the output : " + data);});
回答by Darshan
you can do this by ajax post method..
你可以通过ajax post方法来做到这一点..
$.ready(function(){
$("#confirm").click(function() {
$.ajax({
type: "POST",
url: "give url to the input.php file ",
data:,
success:function(data)
{
alert('data');// data is the return value from input.php
}
});
});
});
});
回答by hard2hack
I'm not quite sure what you've tried to do there. Anyway here's a snippet of js that should do for you (I'm pretty sure in the new jQuery release there's a better way to do this though):
我不太确定你在那里尝试过什么。无论如何,这里有一段 js 应该为你做(我很确定在新的 jQuery 版本中有更好的方法来做到这一点):
$.ajax({
url: "input.php",
success:
function(data)
{
// here, for example, you load the data received from input.php into
// an html element with id #content and show an alert message
$("#content").html(data);
alert("Success")
}
});
回答by Sanjay Kumar N S
Try this:
试试这个:
<input type="button" id="confirm" value="Register Now" class="button">
<script type="text/javascript">
$(document).ready(function() {
$("#confirm").click(function() {
$.get('input.php').
success(function(){
alert ("data Added successfully.");
});
});
});
</script>